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. |
Steven Sombar | 5c006d3 | 2018-10-08 09:35:38 -0500 | [diff] [blame] | 16 | inventory_items = ['fru', 'core', 'fan', 'fan_wc', 'gpu'] |
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 | |
George Keishing | 1eab883 | 2020-07-09 06:30:57 -0500 | [diff] [blame] | 21 | # Properties inventory list |
| 22 | yaml_inventory_list = [] |
George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 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 | |
George Keishing | 36fedf2 | 2017-03-21 06:30:27 -0500 | [diff] [blame] | 28 | repo_subdir_path = '/phosphor-dbus-interfaces/xyz/openbmc_project/' |
George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 29 | base_dir_path = os.getcwd() + repo_subdir_path |
| 30 | |
George Keishing | 9ef1fd4 | 2017-03-10 14:52:38 -0600 | [diff] [blame] | 31 | # yaml file paths for FRU |
Sweta Potthuri | 1da4c24 | 2018-01-09 04:42:51 -0600 | [diff] [blame] | 32 | yaml_fru_list = ['Inventory/Item.interface.yaml', |
| 33 | 'Inventory/Decorator/Asset.interface.yaml', |
| 34 | 'Inventory/Decorator/Revision.interface.yaml', |
| 35 | 'Inventory/Decorator/Replaceable.interface.yaml', |
| 36 | 'Inventory/Decorator/Cacheable.interface.yaml', |
| 37 | 'State/Decorator/OperationalStatus.interface.yaml', ] |
George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 38 | |
Sweta Potthuri | 1da4c24 | 2018-01-09 04:42:51 -0600 | [diff] [blame] | 39 | # yaml file paths for CORE. |
Sweta Potthuri | 1da4c24 | 2018-01-09 04:42:51 -0600 | [diff] [blame] | 40 | yaml_core_list = ['Inventory/Item.interface.yaml', |
| 41 | 'State/Decorator/OperationalStatus.interface.yaml', ] |
George Keishing | dc68eff | 2017-06-12 22:43:18 -0500 | [diff] [blame] | 42 | |
| 43 | # yaml file paths for fan. |
Sweta Potthuri | 1da4c24 | 2018-01-09 04:42:51 -0600 | [diff] [blame] | 44 | yaml_fan_list = ['Inventory/Item.interface.yaml', |
| 45 | 'Inventory/Decorator/MeetsMinimumShipLevel.interface.yaml', |
| 46 | 'State/Decorator/OperationalStatus.interface.yaml', ] |
Steven Sombar | 5c006d3 | 2018-10-08 09:35:38 -0500 | [diff] [blame] | 47 | |
| 48 | # yaml file paths for fan_wc (fans in water-cooled system). |
| 49 | yaml_fan_wc_list = ['Inventory/Item.interface.yaml', |
| 50 | 'Inventory/Decorator/MeetsMinimumShipLevel.interface.yaml', ] |
| 51 | |
George Keishing | 1930520 | 2017-11-17 12:07:28 -0600 | [diff] [blame] | 52 | # yaml file paths for GPU. |
Sweta Potthuri | 1da4c24 | 2018-01-09 04:42:51 -0600 | [diff] [blame] | 53 | yaml_gpu_list = ['Inventory/Item.interface.yaml', |
George Keishing | 0219da6 | 2018-03-26 03:24:40 -0500 | [diff] [blame] | 54 | 'Inventory/Decorator/Replaceable.interface.yaml', |
Sweta Potthuri | 1da4c24 | 2018-01-09 04:42:51 -0600 | [diff] [blame] | 55 | 'State/Decorator/OperationalStatus.interface.yaml', ] |
Steven Sombar | 5c006d3 | 2018-10-08 09:35:38 -0500 | [diff] [blame] | 56 | |
George Keishing | 1eab883 | 2020-07-09 06:30:57 -0500 | [diff] [blame] | 57 | # Append to inventory list |
| 58 | yaml_inventory_list.append(yaml_fru_list) |
| 59 | yaml_inventory_list.append(yaml_core_list) |
| 60 | yaml_inventory_list.append(yaml_fan_list) |
| 61 | yaml_inventory_list.append(yaml_fan_wc_list) |
| 62 | yaml_inventory_list.append(yaml_gpu_list) |
George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 63 | |
George Keishing | 1eab883 | 2020-07-09 06:30:57 -0500 | [diff] [blame] | 64 | print_var(yaml_inventory_list) |
George Keishing | 9ef1fd4 | 2017-03-10 14:52:38 -0600 | [diff] [blame] | 65 | |
George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 66 | # Populate Inventory data |
| 67 | inventory_dict = {} |
| 68 | |
George Keishing | 1eab883 | 2020-07-09 06:30:57 -0500 | [diff] [blame] | 69 | for inv_index in range(len(yaml_inventory_list)): |
| 70 | print_var(inv_index) |
| 71 | inventory_dict[str(inventory_items[inv_index])] = [] |
| 72 | for rel_yaml_file_path in yaml_inventory_list[inv_index]: |
George Keishing | 9ef1fd4 | 2017-03-10 14:52:38 -0600 | [diff] [blame] | 73 | yaml_file_path = base_dir_path + rel_yaml_file_path |
George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 74 | |
| 75 | # Get the yaml dictionary data |
George Keishing | 9ef1fd4 | 2017-03-10 14:52:38 -0600 | [diff] [blame] | 76 | print_timen("Loading " + yaml_file_path) |
| 77 | f = open(yaml_file_path) |
George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 78 | yaml_data = yaml.load(f) |
| 79 | f.close() |
| 80 | for item in range(0, len(yaml_data['properties'])): |
| 81 | tmp_data = yaml_data['properties'][item]['name'] |
George Keishing | 1eab883 | 2020-07-09 06:30:57 -0500 | [diff] [blame] | 82 | inventory_dict[str(inventory_items[inv_index])].append(tmp_data) |
George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 83 | |
| 84 | # Pretty print json formatter |
George Keishing | 9ef1fd4 | 2017-03-10 14:52:38 -0600 | [diff] [blame] | 85 | data = json.dumps(inventory_dict, |
| 86 | indent=4, |
| 87 | sort_keys=True, |
| 88 | default=str, |
| 89 | separators=(',', ':')) |
George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 90 | |
| 91 | # Check if there is mismatch in data vs expect list |
| 92 | if len(inventory_dict) != len(inventory_items): |
George Keishing | 178d9bf | 2020-07-09 08:29:29 -0500 | [diff] [blame] | 93 | print_error("The generated list doesn't match Inventory List.\n") |
George Keishing | 32c4c9e | 2018-08-06 14:00:10 -0500 | [diff] [blame] | 94 | print (data) |
George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 95 | print_var(inventory_items) |
| 96 | sys.exit() |
| 97 | |
| 98 | # Write dictionary data to inventory file |
George Keishing | 32c4c9e | 2018-08-06 14:00:10 -0500 | [diff] [blame] | 99 | print ("\nGenerated Inventory item json format\n") |
| 100 | print (data) |
George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 101 | out = open(fru_inventory_file_path, 'w') |
| 102 | out.write('inventory_dict = ') |
| 103 | out.write(data) |
| 104 | |
| 105 | out.close() |
George Keishing | 32c4c9e | 2018-08-06 14:00:10 -0500 | [diff] [blame] | 106 | print ("\nGenerated Inventory File: %s " % fru_inventory_file_path) |