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. |
| 16 | inventory_items = ['FRU'] |
| 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' |
| 35 | |
| 36 | # FRU list |
| 37 | yaml_fru_list = [] |
| 38 | yaml_fru_list.append(item_yaml_file_path) |
| 39 | yaml_fru_list.append(asset_yaml_file_path) |
| 40 | yaml_fru_list.append(revision_yaml_file_path) |
| 41 | |
| 42 | # Append to master list |
| 43 | yaml_master_list.append(yaml_fru_list) |
| 44 | |
| 45 | # Populate Inventory data |
| 46 | inventory_dict = {} |
| 47 | |
| 48 | for master_index in range(0, len(yaml_master_list)): |
| 49 | inventory_dict[str(inventory_items[master_index])] = [] |
| 50 | for yaml_file in yaml_master_list[master_index]: |
| 51 | |
| 52 | # Get the yaml dictionary data |
| 53 | print_timen("Loading " + yaml_file) |
| 54 | f = open(yaml_file) |
| 55 | yaml_data = yaml.load(f) |
| 56 | f.close() |
| 57 | for item in range(0, len(yaml_data['properties'])): |
| 58 | tmp_data = yaml_data['properties'][item]['name'] |
| 59 | inventory_dict[str(inventory_items[master_index])].append(tmp_data) |
| 60 | |
| 61 | # Pretty print json formatter |
| 62 | data = json.dumps(inventory_dict, indent=4, sort_keys=True, default=str) |
| 63 | |
| 64 | # Check if there is mismatch in data vs expect list |
| 65 | if len(inventory_dict) != len(inventory_items): |
| 66 | print_error("The generated list doesn't match Master Inventory List.\n") |
| 67 | print data |
| 68 | print_var(inventory_items) |
| 69 | sys.exit() |
| 70 | |
| 71 | # Write dictionary data to inventory file |
| 72 | print "\nGenerated Inventory item json format\n" |
| 73 | print data |
| 74 | out = open(fru_inventory_file_path, 'w') |
| 75 | out.write('inventory_dict = ') |
| 76 | out.write(data) |
| 77 | |
| 78 | out.close() |
| 79 | print "\nGenerated Inventory File: ", fru_inventory_file_path |