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