blob: 4160f1f348b935ef87b969146b0f50aa46b7db0b [file] [log] [blame]
George Keishingae7c8202017-02-09 10:33:45 -06001#!/usr/bin/env python
2r"""
3Generate an inventory variable file containing a list of properties
4fields from the YAML phosphor-dbus-interfaces repository.
5"""
6import sys
7import os
8import yaml
9import json
10
11lib_path = sys.path[0] + "/../lib"
12sys.path.insert(0, lib_path)
13from gen_print import *
14
15# This list will be longer when more development codes are available.
George Keishing05b36ec2017-03-06 05:50:31 -060016inventory_items = ['fru']
George Keishingae7c8202017-02-09 10:33:45 -060017print_var(inventory_items)
18fru_inventory_file_path = 'inventory.py'
19print_var(fru_inventory_file_path)
20
21# Properties master list
22yaml_master_list = []
23
24# Clone the phosphor-dbus-interfaces repository
25cmd_buf = 'git clone https://github.com/openbmc/phosphor-dbus-interfaces'
26os.system(cmd_buf)
27
28repo_subdir_path = '/phosphor-dbus-interfaces/xyz/openbmc_project/Inventory/'
29base_dir_path = os.getcwd() + repo_subdir_path
30
George Keishing9ef1fd42017-03-10 14:52:38 -060031# yaml file paths for FRU
32yaml_fru_list = [ 'Item.interface.yaml',
33 'Decorator/Asset.interface.yaml',
34 'Decorator/Revision.interface.yaml',
35 'Decorator/Replaceable.interface.yaml',
36 'Decorator/Cacheable.interface.yaml',
37 ]
George Keishingae7c8202017-02-09 10:33:45 -060038
39# Append to master list
40yaml_master_list.append(yaml_fru_list)
41
George Keishing9ef1fd42017-03-10 14:52:38 -060042print_var(yaml_master_list)
43
George Keishingae7c8202017-02-09 10:33:45 -060044# Populate Inventory data
45inventory_dict = {}
46
George Keishing9ef1fd42017-03-10 14:52:38 -060047for master_index in range(len(yaml_master_list)):
48 print_var(master_index)
George Keishingae7c8202017-02-09 10:33:45 -060049 inventory_dict[str(inventory_items[master_index])] = []
George Keishing9ef1fd42017-03-10 14:52:38 -060050 for rel_yaml_file_path in yaml_master_list[master_index]:
51 yaml_file_path = base_dir_path + rel_yaml_file_path
George Keishingae7c8202017-02-09 10:33:45 -060052
53 # Get the yaml dictionary data
George Keishing9ef1fd42017-03-10 14:52:38 -060054 print_timen("Loading " + yaml_file_path)
55 f = open(yaml_file_path)
George Keishingae7c8202017-02-09 10:33:45 -060056 yaml_data = yaml.load(f)
57 f.close()
58 for item in range(0, len(yaml_data['properties'])):
59 tmp_data = yaml_data['properties'][item]['name']
60 inventory_dict[str(inventory_items[master_index])].append(tmp_data)
61
62# Pretty print json formatter
George Keishing9ef1fd42017-03-10 14:52:38 -060063data = json.dumps(inventory_dict,
64 indent=4,
65 sort_keys=True,
66 default=str,
67 separators=(',', ':'))
George Keishingae7c8202017-02-09 10:33:45 -060068
69# Check if there is mismatch in data vs expect list
70if len(inventory_dict) != len(inventory_items):
71 print_error("The generated list doesn't match Master Inventory List.\n")
72 print data
73 print_var(inventory_items)
74 sys.exit()
75
76# Write dictionary data to inventory file
77print "\nGenerated Inventory item json format\n"
78print data
79out = open(fru_inventory_file_path, 'w')
80out.write('inventory_dict = ')
81out.write(data)
82
83out.close()
84print "\nGenerated Inventory File: ", fru_inventory_file_path