1#!/usr/bin/env python3
2r"""
3Generate an inventory variable file containing a list of properties
4fields from the YAML phosphor-dbus-interfaces repository.
5"""
6import json
7import os
8import sys
9
10import yaml
11
12lib_path = sys.path[0] + "/../lib"
13sys.path.insert(0, lib_path)
14from gen_print import *  # NOQA
15
16# This list will be longer when more development codes are available.
17inventory_items = ["fru", "core", "fan", "fan_wc", "gpu"]
18print_var(inventory_items)
19fru_inventory_file_path = "inventory.py"
20print_var(fru_inventory_file_path)
21
22# Properties inventory list
23yaml_inventory_list = []
24
25# Clone the phosphor-dbus-interfaces repository
26cmd_buf = "git clone https://github.com/openbmc/phosphor-dbus-interfaces"
27os.system(cmd_buf)
28
29repo_subdir_path = "/phosphor-dbus-interfaces/xyz/openbmc_project/"
30base_dir_path = os.getcwd() + repo_subdir_path
31
32# yaml file paths for FRU
33yaml_fru_list = [
34    "Inventory/Item.interface.yaml",
35    "Inventory/Decorator/Asset.interface.yaml",
36    "Inventory/Decorator/Revision.interface.yaml",
37    "Inventory/Decorator/Replaceable.interface.yaml",
38    "Inventory/Decorator/Cacheable.interface.yaml",
39    "State/Decorator/OperationalStatus.interface.yaml",
40]
41
42# yaml file paths for CORE.
43yaml_core_list = [
44    "Inventory/Item.interface.yaml",
45    "State/Decorator/OperationalStatus.interface.yaml",
46]
47
48# yaml file paths for fan.
49yaml_fan_list = [
50    "Inventory/Item.interface.yaml",
51    "Inventory/Decorator/MeetsMinimumShipLevel.interface.yaml",
52    "State/Decorator/OperationalStatus.interface.yaml",
53]
54
55# yaml file paths for fan_wc (fans in water-cooled system).
56yaml_fan_wc_list = [
57    "Inventory/Item.interface.yaml",
58    "Inventory/Decorator/MeetsMinimumShipLevel.interface.yaml",
59]
60
61# yaml file paths for GPU.
62yaml_gpu_list = [
63    "Inventory/Item.interface.yaml",
64    "Inventory/Decorator/Replaceable.interface.yaml",
65    "State/Decorator/OperationalStatus.interface.yaml",
66]
67
68# Append to inventory list
69yaml_inventory_list.append(yaml_fru_list)
70yaml_inventory_list.append(yaml_core_list)
71yaml_inventory_list.append(yaml_fan_list)
72yaml_inventory_list.append(yaml_fan_wc_list)
73yaml_inventory_list.append(yaml_gpu_list)
74
75print_var(yaml_inventory_list)
76
77# Populate Inventory data
78inventory_dict = {}
79
80for inv_index in range(len(yaml_inventory_list)):
81    print_var(inv_index)
82    inventory_dict[str(inventory_items[inv_index])] = []
83    for rel_yaml_file_path in yaml_inventory_list[inv_index]:
84        yaml_file_path = base_dir_path + rel_yaml_file_path
85
86        # Get the yaml dictionary data
87        print_timen("Loading " + yaml_file_path)
88        f = open(yaml_file_path)
89        yaml_data = yaml.safe_load(f)
90        f.close()
91        for item in range(0, len(yaml_data["properties"])):
92            tmp_data = yaml_data["properties"][item]["name"]
93            inventory_dict[str(inventory_items[inv_index])].append(tmp_data)
94
95# Pretty print json formatter
96data = json.dumps(
97    inventory_dict,
98    indent=4,
99    sort_keys=True,
100    default=str,
101    separators=(",", ":"),
102)
103
104# Check if there is mismatch in data vs expect list
105if len(inventory_dict) != len(inventory_items):
106    print_error("The generated list doesn't match Inventory List.\n")
107    print(data)
108    print_var(inventory_items)
109    sys.exit()
110
111# Write dictionary data to inventory file
112print("\nGenerated Inventory item json format\n")
113print(data)
114out = open(fru_inventory_file_path, "w")
115out.write("inventory_dict = ")
116out.write(data)
117
118out.close()
119print("\nGenerated Inventory File: %s " % fru_inventory_file_path)
120