1ae7c820fSGeorge Keishing#!/usr/bin/env python
2ae7c820fSGeorge Keishingr"""
3ae7c820fSGeorge KeishingGenerate an inventory variable file containing a list of properties
4ae7c820fSGeorge Keishingfields from the YAML phosphor-dbus-interfaces repository.
5ae7c820fSGeorge Keishing"""
6ae7c820fSGeorge Keishingimport sys
7ae7c820fSGeorge Keishingimport os
8ae7c820fSGeorge Keishingimport yaml
9ae7c820fSGeorge Keishingimport json
10ae7c820fSGeorge Keishing
11ae7c820fSGeorge Keishinglib_path = sys.path[0] + "/../lib"
12ae7c820fSGeorge Keishingsys.path.insert(0, lib_path)
13ae7c820fSGeorge Keishingfrom gen_print import *
14ae7c820fSGeorge Keishing
15ae7c820fSGeorge Keishing# This list will be longer when more development codes are available.
1619305205SGeorge Keishinginventory_items = ['fru', 'core', 'fan', 'gpu']
17ae7c820fSGeorge Keishingprint_var(inventory_items)
18ae7c820fSGeorge Keishingfru_inventory_file_path = 'inventory.py'
19ae7c820fSGeorge Keishingprint_var(fru_inventory_file_path)
20ae7c820fSGeorge Keishing
21ae7c820fSGeorge Keishing# Properties master list
22ae7c820fSGeorge Keishingyaml_master_list = []
23ae7c820fSGeorge Keishing
24ae7c820fSGeorge Keishing# Clone the phosphor-dbus-interfaces repository
25ae7c820fSGeorge Keishingcmd_buf = 'git clone https://github.com/openbmc/phosphor-dbus-interfaces'
26ae7c820fSGeorge Keishingos.system(cmd_buf)
27ae7c820fSGeorge Keishing
2836fedf23SGeorge Keishingrepo_subdir_path = '/phosphor-dbus-interfaces/xyz/openbmc_project/'
29ae7c820fSGeorge Keishingbase_dir_path = os.getcwd() + repo_subdir_path
30ae7c820fSGeorge Keishing
319ef1fd47SGeorge Keishing# yaml file paths for FRU
3236fedf23SGeorge Keishingyaml_fru_list = ['Inventory/Item.interface.yaml',
3336fedf23SGeorge Keishing                 'Inventory/Decorator/Asset.interface.yaml',
3436fedf23SGeorge Keishing                 'Inventory/Decorator/Revision.interface.yaml',
3536fedf23SGeorge Keishing                 'Inventory/Decorator/Replaceable.interface.yaml',
3636fedf23SGeorge Keishing                 'Inventory/Decorator/Cacheable.interface.yaml',
37*1da4c24dSSweta Potthuri                 'State/Decorator/OperationalStatus.interface.yaml', ]
38ae7c820fSGeorge Keishing
39*1da4c24dSSweta Potthuri# yaml file paths for CORE.
40*1da4c24dSSweta Potthuri
4136fedf23SGeorge Keishingyaml_core_list = ['Inventory/Item.interface.yaml',
42*1da4c24dSSweta Potthuri                  'State/Decorator/OperationalStatus.interface.yaml', ]
43dc68eff6SGeorge Keishing
44dc68eff6SGeorge Keishing# yaml file paths for fan.
45dc68eff6SGeorge Keishingyaml_fan_list = ['Inventory/Item.interface.yaml',
46ca8c61b7SGeorge Keishing                 'Inventory/Decorator/MeetsMinimumShipLevel.interface.yaml',
47*1da4c24dSSweta Potthuri                 'State/Decorator/OperationalStatus.interface.yaml', ]
4819305205SGeorge Keishing# yaml file paths for GPU.
4919305205SGeorge Keishingyaml_gpu_list = ['Inventory/Item.interface.yaml',
50*1da4c24dSSweta Potthuri                 'State/Decorator/OperationalStatus.interface.yaml', ]
51ae7c820fSGeorge Keishing# Append to master list
52ae7c820fSGeorge Keishingyaml_master_list.append(yaml_fru_list)
5336fedf23SGeorge Keishingyaml_master_list.append(yaml_core_list)
54dc68eff6SGeorge Keishingyaml_master_list.append(yaml_fan_list)
5519305205SGeorge Keishingyaml_master_list.append(yaml_gpu_list)
56ae7c820fSGeorge Keishing
579ef1fd47SGeorge Keishingprint_var(yaml_master_list)
589ef1fd47SGeorge Keishing
59ae7c820fSGeorge Keishing# Populate Inventory data
60ae7c820fSGeorge Keishinginventory_dict = {}
61ae7c820fSGeorge Keishing
629ef1fd47SGeorge Keishingfor master_index in range(len(yaml_master_list)):
639ef1fd47SGeorge Keishing    print_var(master_index)
64ae7c820fSGeorge Keishing    inventory_dict[str(inventory_items[master_index])] = []
659ef1fd47SGeorge Keishing    for rel_yaml_file_path in yaml_master_list[master_index]:
669ef1fd47SGeorge Keishing        yaml_file_path = base_dir_path + rel_yaml_file_path
67ae7c820fSGeorge Keishing
68ae7c820fSGeorge Keishing        # Get the yaml dictionary data
699ef1fd47SGeorge Keishing        print_timen("Loading " + yaml_file_path)
709ef1fd47SGeorge Keishing        f = open(yaml_file_path)
71ae7c820fSGeorge Keishing        yaml_data = yaml.load(f)
72ae7c820fSGeorge Keishing        f.close()
73ae7c820fSGeorge Keishing        for item in range(0, len(yaml_data['properties'])):
74ae7c820fSGeorge Keishing            tmp_data = yaml_data['properties'][item]['name']
75ae7c820fSGeorge Keishing            inventory_dict[str(inventory_items[master_index])].append(tmp_data)
76ae7c820fSGeorge Keishing
77ae7c820fSGeorge Keishing# Pretty print json formatter
789ef1fd47SGeorge Keishingdata = json.dumps(inventory_dict,
799ef1fd47SGeorge Keishing                  indent=4,
809ef1fd47SGeorge Keishing                  sort_keys=True,
819ef1fd47SGeorge Keishing                  default=str,
829ef1fd47SGeorge Keishing                  separators=(',', ':'))
83ae7c820fSGeorge Keishing
84ae7c820fSGeorge Keishing# Check if there is mismatch in data vs expect list
85ae7c820fSGeorge Keishingif len(inventory_dict) != len(inventory_items):
86ae7c820fSGeorge Keishing    print_error("The generated list doesn't match Master Inventory List.\n")
87ae7c820fSGeorge Keishing    print data
88ae7c820fSGeorge Keishing    print_var(inventory_items)
89ae7c820fSGeorge Keishing    sys.exit()
90ae7c820fSGeorge Keishing
91ae7c820fSGeorge Keishing# Write dictionary data to inventory file
92ae7c820fSGeorge Keishingprint "\nGenerated Inventory item json format\n"
93ae7c820fSGeorge Keishingprint data
94ae7c820fSGeorge Keishingout = open(fru_inventory_file_path, 'w')
95ae7c820fSGeorge Keishingout.write('inventory_dict = ')
96ae7c820fSGeorge Keishingout.write(data)
97ae7c820fSGeorge Keishing
98ae7c820fSGeorge Keishingout.close()
99ae7c820fSGeorge Keishingprint "\nGenerated Inventory File: ", fru_inventory_file_path
100