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