1e7e9171eSGeorge Keishing#!/usr/bin/env python3
2ae7c820fSGeorge Keishingr"""
3ae7c820fSGeorge KeishingGenerate an inventory variable file containing a list of properties
4ae7c820fSGeorge Keishingfields from the YAML phosphor-dbus-interfaces repository.
5ae7c820fSGeorge Keishing"""
6e635ddc0SGeorge Keishingimport json
7*20f38712SPatrick Williamsimport os
8*20f38712SPatrick Williamsimport sys
9*20f38712SPatrick Williams
10*20f38712SPatrick Williamsimport yaml
11e635ddc0SGeorge Keishing
12ae7c820fSGeorge Keishinglib_path = sys.path[0] + "/../lib"
13ae7c820fSGeorge Keishingsys.path.insert(0, lib_path)
1409679890SGeorge Keishingfrom gen_print import *  # NOQA
15ae7c820fSGeorge Keishing
16ae7c820fSGeorge Keishing# This list will be longer when more development codes are available.
17*20f38712SPatrick Williamsinventory_items = ["fru", "core", "fan", "fan_wc", "gpu"]
18ae7c820fSGeorge Keishingprint_var(inventory_items)
19*20f38712SPatrick Williamsfru_inventory_file_path = "inventory.py"
20ae7c820fSGeorge Keishingprint_var(fru_inventory_file_path)
21ae7c820fSGeorge Keishing
221eab8838SGeorge Keishing# Properties inventory list
231eab8838SGeorge Keishingyaml_inventory_list = []
24ae7c820fSGeorge Keishing
25ae7c820fSGeorge Keishing# Clone the phosphor-dbus-interfaces repository
26*20f38712SPatrick Williamscmd_buf = "git clone https://github.com/openbmc/phosphor-dbus-interfaces"
27ae7c820fSGeorge Keishingos.system(cmd_buf)
28ae7c820fSGeorge Keishing
29*20f38712SPatrick Williamsrepo_subdir_path = "/phosphor-dbus-interfaces/xyz/openbmc_project/"
30ae7c820fSGeorge Keishingbase_dir_path = os.getcwd() + repo_subdir_path
31ae7c820fSGeorge Keishing
329ef1fd47SGeorge Keishing# yaml file paths for FRU
33*20f38712SPatrick Williamsyaml_fru_list = [
34*20f38712SPatrick Williams    "Inventory/Item.interface.yaml",
35*20f38712SPatrick Williams    "Inventory/Decorator/Asset.interface.yaml",
36*20f38712SPatrick Williams    "Inventory/Decorator/Revision.interface.yaml",
37*20f38712SPatrick Williams    "Inventory/Decorator/Replaceable.interface.yaml",
38*20f38712SPatrick Williams    "Inventory/Decorator/Cacheable.interface.yaml",
39*20f38712SPatrick Williams    "State/Decorator/OperationalStatus.interface.yaml",
40*20f38712SPatrick Williams]
41ae7c820fSGeorge Keishing
421da4c24dSSweta Potthuri# yaml file paths for CORE.
43*20f38712SPatrick Williamsyaml_core_list = [
44*20f38712SPatrick Williams    "Inventory/Item.interface.yaml",
45*20f38712SPatrick Williams    "State/Decorator/OperationalStatus.interface.yaml",
46*20f38712SPatrick Williams]
47dc68eff6SGeorge Keishing
48dc68eff6SGeorge Keishing# yaml file paths for fan.
49*20f38712SPatrick Williamsyaml_fan_list = [
50*20f38712SPatrick Williams    "Inventory/Item.interface.yaml",
51*20f38712SPatrick Williams    "Inventory/Decorator/MeetsMinimumShipLevel.interface.yaml",
52*20f38712SPatrick Williams    "State/Decorator/OperationalStatus.interface.yaml",
53*20f38712SPatrick Williams]
545c006d39SSteven Sombar
555c006d39SSteven Sombar# yaml file paths for fan_wc (fans in water-cooled system).
56*20f38712SPatrick Williamsyaml_fan_wc_list = [
57*20f38712SPatrick Williams    "Inventory/Item.interface.yaml",
58*20f38712SPatrick Williams    "Inventory/Decorator/MeetsMinimumShipLevel.interface.yaml",
59*20f38712SPatrick Williams]
605c006d39SSteven Sombar
6119305205SGeorge Keishing# yaml file paths for GPU.
62*20f38712SPatrick Williamsyaml_gpu_list = [
63*20f38712SPatrick Williams    "Inventory/Item.interface.yaml",
64*20f38712SPatrick Williams    "Inventory/Decorator/Replaceable.interface.yaml",
65*20f38712SPatrick Williams    "State/Decorator/OperationalStatus.interface.yaml",
66*20f38712SPatrick Williams]
675c006d39SSteven Sombar
681eab8838SGeorge Keishing# Append to inventory list
691eab8838SGeorge Keishingyaml_inventory_list.append(yaml_fru_list)
701eab8838SGeorge Keishingyaml_inventory_list.append(yaml_core_list)
711eab8838SGeorge Keishingyaml_inventory_list.append(yaml_fan_list)
721eab8838SGeorge Keishingyaml_inventory_list.append(yaml_fan_wc_list)
731eab8838SGeorge Keishingyaml_inventory_list.append(yaml_gpu_list)
74ae7c820fSGeorge Keishing
751eab8838SGeorge Keishingprint_var(yaml_inventory_list)
769ef1fd47SGeorge Keishing
77ae7c820fSGeorge Keishing# Populate Inventory data
78ae7c820fSGeorge Keishinginventory_dict = {}
79ae7c820fSGeorge Keishing
801eab8838SGeorge Keishingfor inv_index in range(len(yaml_inventory_list)):
811eab8838SGeorge Keishing    print_var(inv_index)
821eab8838SGeorge Keishing    inventory_dict[str(inventory_items[inv_index])] = []
831eab8838SGeorge Keishing    for rel_yaml_file_path in yaml_inventory_list[inv_index]:
849ef1fd47SGeorge Keishing        yaml_file_path = base_dir_path + rel_yaml_file_path
85ae7c820fSGeorge Keishing
86ae7c820fSGeorge Keishing        # Get the yaml dictionary data
879ef1fd47SGeorge Keishing        print_timen("Loading " + yaml_file_path)
889ef1fd47SGeorge Keishing        f = open(yaml_file_path)
89f87cc0afSYunyun Lin        yaml_data = yaml.safe_load(f)
90ae7c820fSGeorge Keishing        f.close()
91*20f38712SPatrick Williams        for item in range(0, len(yaml_data["properties"])):
92*20f38712SPatrick Williams            tmp_data = yaml_data["properties"][item]["name"]
931eab8838SGeorge Keishing            inventory_dict[str(inventory_items[inv_index])].append(tmp_data)
94ae7c820fSGeorge Keishing
95ae7c820fSGeorge Keishing# Pretty print json formatter
96*20f38712SPatrick Williamsdata = json.dumps(
97*20f38712SPatrick Williams    inventory_dict,
989ef1fd47SGeorge Keishing    indent=4,
999ef1fd47SGeorge Keishing    sort_keys=True,
1009ef1fd47SGeorge Keishing    default=str,
101*20f38712SPatrick Williams    separators=(",", ":"),
102*20f38712SPatrick Williams)
103ae7c820fSGeorge Keishing
104ae7c820fSGeorge Keishing# Check if there is mismatch in data vs expect list
105ae7c820fSGeorge Keishingif len(inventory_dict) != len(inventory_items):
106178d9bf2SGeorge Keishing    print_error("The generated list doesn't match Inventory List.\n")
10732c4c9e4SGeorge Keishing    print(data)
108ae7c820fSGeorge Keishing    print_var(inventory_items)
109ae7c820fSGeorge Keishing    sys.exit()
110ae7c820fSGeorge Keishing
111ae7c820fSGeorge Keishing# Write dictionary data to inventory file
11232c4c9e4SGeorge Keishingprint("\nGenerated Inventory item json format\n")
11332c4c9e4SGeorge Keishingprint(data)
114*20f38712SPatrick Williamsout = open(fru_inventory_file_path, "w")
115*20f38712SPatrick Williamsout.write("inventory_dict = ")
116ae7c820fSGeorge Keishingout.write(data)
117ae7c820fSGeorge Keishing
118ae7c820fSGeorge Keishingout.close()
11932c4c9e4SGeorge Keishingprint("\nGenerated Inventory File: %s " % fru_inventory_file_path)
120