xref: /openbmc/openbmc-test-automation/bin/generate_inventory (revision ae7c820f51c278ab302e5d5ce3140a38452c8737)
1*ae7c820fSGeorge Keishing#!/usr/bin/env python
2*ae7c820fSGeorge Keishingr"""
3*ae7c820fSGeorge KeishingGenerate an inventory variable file containing a list of properties
4*ae7c820fSGeorge Keishingfields from the YAML phosphor-dbus-interfaces repository.
5*ae7c820fSGeorge Keishing"""
6*ae7c820fSGeorge Keishingimport sys
7*ae7c820fSGeorge Keishingimport os
8*ae7c820fSGeorge Keishingimport yaml
9*ae7c820fSGeorge Keishingimport json
10*ae7c820fSGeorge Keishing
11*ae7c820fSGeorge Keishinglib_path = sys.path[0] + "/../lib"
12*ae7c820fSGeorge Keishingsys.path.insert(0, lib_path)
13*ae7c820fSGeorge Keishingfrom gen_print import *
14*ae7c820fSGeorge Keishing
15*ae7c820fSGeorge Keishing# This list will be longer when more development codes are available.
16*ae7c820fSGeorge Keishinginventory_items = ['FRU']
17*ae7c820fSGeorge Keishingprint_var(inventory_items)
18*ae7c820fSGeorge Keishingfru_inventory_file_path = 'inventory.py'
19*ae7c820fSGeorge Keishingprint_var(fru_inventory_file_path)
20*ae7c820fSGeorge Keishing
21*ae7c820fSGeorge Keishing# Properties master list
22*ae7c820fSGeorge Keishingyaml_master_list = []
23*ae7c820fSGeorge Keishing
24*ae7c820fSGeorge Keishing# Clone the phosphor-dbus-interfaces repository
25*ae7c820fSGeorge Keishingcmd_buf = 'git clone https://github.com/openbmc/phosphor-dbus-interfaces'
26*ae7c820fSGeorge Keishingos.system(cmd_buf)
27*ae7c820fSGeorge Keishing
28*ae7c820fSGeorge Keishingrepo_subdir_path = '/phosphor-dbus-interfaces/xyz/openbmc_project/Inventory/'
29*ae7c820fSGeorge Keishingbase_dir_path = os.getcwd() + repo_subdir_path
30*ae7c820fSGeorge Keishing
31*ae7c820fSGeorge Keishing# yaml paths for FRU
32*ae7c820fSGeorge Keishingitem_yaml_file_path = base_dir_path + 'Item.interface.yaml'
33*ae7c820fSGeorge Keishingasset_yaml_file_path = base_dir_path + 'Decorator/Asset.interface.yaml'
34*ae7c820fSGeorge Keishingrevision_yaml_file_path = base_dir_path + 'Decorator/Revision.interface.yaml'
35*ae7c820fSGeorge Keishing
36*ae7c820fSGeorge Keishing# FRU list
37*ae7c820fSGeorge Keishingyaml_fru_list = []
38*ae7c820fSGeorge Keishingyaml_fru_list.append(item_yaml_file_path)
39*ae7c820fSGeorge Keishingyaml_fru_list.append(asset_yaml_file_path)
40*ae7c820fSGeorge Keishingyaml_fru_list.append(revision_yaml_file_path)
41*ae7c820fSGeorge Keishing
42*ae7c820fSGeorge Keishing# Append to master list
43*ae7c820fSGeorge Keishingyaml_master_list.append(yaml_fru_list)
44*ae7c820fSGeorge Keishing
45*ae7c820fSGeorge Keishing# Populate Inventory data
46*ae7c820fSGeorge Keishinginventory_dict = {}
47*ae7c820fSGeorge Keishing
48*ae7c820fSGeorge Keishingfor master_index in range(0, len(yaml_master_list)):
49*ae7c820fSGeorge Keishing    inventory_dict[str(inventory_items[master_index])] = []
50*ae7c820fSGeorge Keishing    for yaml_file in yaml_master_list[master_index]:
51*ae7c820fSGeorge Keishing
52*ae7c820fSGeorge Keishing        # Get the yaml dictionary data
53*ae7c820fSGeorge Keishing        print_timen("Loading " + yaml_file)
54*ae7c820fSGeorge Keishing        f = open(yaml_file)
55*ae7c820fSGeorge Keishing        yaml_data = yaml.load(f)
56*ae7c820fSGeorge Keishing        f.close()
57*ae7c820fSGeorge Keishing        for item in range(0, len(yaml_data['properties'])):
58*ae7c820fSGeorge Keishing            tmp_data = yaml_data['properties'][item]['name']
59*ae7c820fSGeorge Keishing            inventory_dict[str(inventory_items[master_index])].append(tmp_data)
60*ae7c820fSGeorge Keishing
61*ae7c820fSGeorge Keishing# Pretty print json formatter
62*ae7c820fSGeorge Keishingdata = json.dumps(inventory_dict, indent=4, sort_keys=True, default=str)
63*ae7c820fSGeorge Keishing
64*ae7c820fSGeorge Keishing# Check if there is mismatch in data vs expect list
65*ae7c820fSGeorge Keishingif len(inventory_dict) != len(inventory_items):
66*ae7c820fSGeorge Keishing    print_error("The generated list doesn't match Master Inventory List.\n")
67*ae7c820fSGeorge Keishing    print data
68*ae7c820fSGeorge Keishing    print_var(inventory_items)
69*ae7c820fSGeorge Keishing    sys.exit()
70*ae7c820fSGeorge Keishing
71*ae7c820fSGeorge Keishing# Write dictionary data to inventory file
72*ae7c820fSGeorge Keishingprint "\nGenerated Inventory item json format\n"
73*ae7c820fSGeorge Keishingprint data
74*ae7c820fSGeorge Keishingout = open(fru_inventory_file_path, 'w')
75*ae7c820fSGeorge Keishingout.write('inventory_dict = ')
76*ae7c820fSGeorge Keishingout.write(data)
77*ae7c820fSGeorge Keishing
78*ae7c820fSGeorge Keishingout.close()
79*ae7c820fSGeorge Keishingprint "\nGenerated Inventory File: ", fru_inventory_file_path
80