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.
16*05b36eccSGeorge Keishinginventory_items = ['fru']
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
28ae7c820fSGeorge Keishingrepo_subdir_path = '/phosphor-dbus-interfaces/xyz/openbmc_project/Inventory/'
29ae7c820fSGeorge Keishingbase_dir_path = os.getcwd() + repo_subdir_path
30ae7c820fSGeorge Keishing
31ae7c820fSGeorge Keishing# yaml paths for FRU
32ae7c820fSGeorge Keishingitem_yaml_file_path = base_dir_path + 'Item.interface.yaml'
33ae7c820fSGeorge Keishingasset_yaml_file_path = base_dir_path + 'Decorator/Asset.interface.yaml'
34ae7c820fSGeorge Keishingrevision_yaml_file_path = base_dir_path + 'Decorator/Revision.interface.yaml'
35*05b36eccSGeorge Keishingreplaceable_yaml_file_path = base_dir_path + 'Decorator/Replaceable.interface.yaml'
36ae7c820fSGeorge Keishing
37ae7c820fSGeorge Keishing# FRU list
38ae7c820fSGeorge Keishingyaml_fru_list = []
39ae7c820fSGeorge Keishingyaml_fru_list.append(item_yaml_file_path)
40ae7c820fSGeorge Keishingyaml_fru_list.append(asset_yaml_file_path)
41ae7c820fSGeorge Keishingyaml_fru_list.append(revision_yaml_file_path)
42*05b36eccSGeorge Keishingyaml_fru_list.append(replaceable_yaml_file_path)
43ae7c820fSGeorge Keishing
44ae7c820fSGeorge Keishing# Append to master list
45ae7c820fSGeorge Keishingyaml_master_list.append(yaml_fru_list)
46ae7c820fSGeorge Keishing
47ae7c820fSGeorge Keishing# Populate Inventory data
48ae7c820fSGeorge Keishinginventory_dict = {}
49ae7c820fSGeorge Keishing
50ae7c820fSGeorge Keishingfor master_index in range(0, len(yaml_master_list)):
51ae7c820fSGeorge Keishing    inventory_dict[str(inventory_items[master_index])] = []
52ae7c820fSGeorge Keishing    for yaml_file in yaml_master_list[master_index]:
53ae7c820fSGeorge Keishing
54ae7c820fSGeorge Keishing        # Get the yaml dictionary data
55ae7c820fSGeorge Keishing        print_timen("Loading " + yaml_file)
56ae7c820fSGeorge Keishing        f = open(yaml_file)
57ae7c820fSGeorge Keishing        yaml_data = yaml.load(f)
58ae7c820fSGeorge Keishing        f.close()
59ae7c820fSGeorge Keishing        for item in range(0, len(yaml_data['properties'])):
60ae7c820fSGeorge Keishing            tmp_data = yaml_data['properties'][item]['name']
61ae7c820fSGeorge Keishing            inventory_dict[str(inventory_items[master_index])].append(tmp_data)
62ae7c820fSGeorge Keishing
63ae7c820fSGeorge Keishing# Pretty print json formatter
64ae7c820fSGeorge Keishingdata = json.dumps(inventory_dict, indent=4, sort_keys=True, default=str)
65ae7c820fSGeorge Keishing
66ae7c820fSGeorge Keishing# Check if there is mismatch in data vs expect list
67ae7c820fSGeorge Keishingif len(inventory_dict) != len(inventory_items):
68ae7c820fSGeorge Keishing    print_error("The generated list doesn't match Master Inventory List.\n")
69ae7c820fSGeorge Keishing    print data
70ae7c820fSGeorge Keishing    print_var(inventory_items)
71ae7c820fSGeorge Keishing    sys.exit()
72ae7c820fSGeorge Keishing
73ae7c820fSGeorge Keishing# Write dictionary data to inventory file
74ae7c820fSGeorge Keishingprint "\nGenerated Inventory item json format\n"
75ae7c820fSGeorge Keishingprint data
76ae7c820fSGeorge Keishingout = open(fru_inventory_file_path, 'w')
77ae7c820fSGeorge Keishingout.write('inventory_dict = ')
78ae7c820fSGeorge Keishingout.write(data)
79ae7c820fSGeorge Keishing
80ae7c820fSGeorge Keishingout.close()
81ae7c820fSGeorge Keishingprint "\nGenerated Inventory File: ", fru_inventory_file_path
82