George Keishing | e7e9171 | 2021-09-03 11:28:44 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 2 | r""" |
| 3 | Generate an inventory variable file containing a list of properties |
| 4 | fields from the YAML phosphor-dbus-interfaces repository. |
| 5 | """ |
Sridevi Ramesh | 47375aa | 2022-12-08 05:05:31 -0600 | [diff] [blame] | 6 | |
| 7 | from gen_print import * |
| 8 | |
George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 9 | import sys |
| 10 | import os |
| 11 | import yaml |
| 12 | import json |
| 13 | |
| 14 | lib_path = sys.path[0] + "/../lib" |
| 15 | sys.path.insert(0, lib_path) |
George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 16 | |
| 17 | # This list will be longer when more development codes are available. |
Steven Sombar | 5c006d3 | 2018-10-08 09:35:38 -0500 | [diff] [blame] | 18 | inventory_items = ['fru', 'core', 'fan', 'fan_wc', 'gpu'] |
George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 19 | print_var(inventory_items) |
| 20 | fru_inventory_file_path = 'inventory.py' |
| 21 | print_var(fru_inventory_file_path) |
| 22 | |
George Keishing | 1eab883 | 2020-07-09 06:30:57 -0500 | [diff] [blame] | 23 | # Properties inventory list |
| 24 | yaml_inventory_list = [] |
George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 25 | |
| 26 | # Clone the phosphor-dbus-interfaces repository |
| 27 | cmd_buf = 'git clone https://github.com/openbmc/phosphor-dbus-interfaces' |
| 28 | os.system(cmd_buf) |
| 29 | |
George Keishing | 36fedf2 | 2017-03-21 06:30:27 -0500 | [diff] [blame] | 30 | repo_subdir_path = '/phosphor-dbus-interfaces/xyz/openbmc_project/' |
George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 31 | base_dir_path = os.getcwd() + repo_subdir_path |
| 32 | |
George Keishing | 9ef1fd4 | 2017-03-10 14:52:38 -0600 | [diff] [blame] | 33 | # yaml file paths for FRU |
Sweta Potthuri | 1da4c24 | 2018-01-09 04:42:51 -0600 | [diff] [blame] | 34 | yaml_fru_list = ['Inventory/Item.interface.yaml', |
| 35 | 'Inventory/Decorator/Asset.interface.yaml', |
| 36 | 'Inventory/Decorator/Revision.interface.yaml', |
| 37 | 'Inventory/Decorator/Replaceable.interface.yaml', |
| 38 | 'Inventory/Decorator/Cacheable.interface.yaml', |
| 39 | 'State/Decorator/OperationalStatus.interface.yaml', ] |
George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 40 | |
Sweta Potthuri | 1da4c24 | 2018-01-09 04:42:51 -0600 | [diff] [blame] | 41 | # yaml file paths for CORE. |
Sweta Potthuri | 1da4c24 | 2018-01-09 04:42:51 -0600 | [diff] [blame] | 42 | yaml_core_list = ['Inventory/Item.interface.yaml', |
| 43 | 'State/Decorator/OperationalStatus.interface.yaml', ] |
George Keishing | dc68eff | 2017-06-12 22:43:18 -0500 | [diff] [blame] | 44 | |
| 45 | # yaml file paths for fan. |
Sweta Potthuri | 1da4c24 | 2018-01-09 04:42:51 -0600 | [diff] [blame] | 46 | yaml_fan_list = ['Inventory/Item.interface.yaml', |
| 47 | 'Inventory/Decorator/MeetsMinimumShipLevel.interface.yaml', |
| 48 | 'State/Decorator/OperationalStatus.interface.yaml', ] |
Steven Sombar | 5c006d3 | 2018-10-08 09:35:38 -0500 | [diff] [blame] | 49 | |
| 50 | # yaml file paths for fan_wc (fans in water-cooled system). |
| 51 | yaml_fan_wc_list = ['Inventory/Item.interface.yaml', |
| 52 | 'Inventory/Decorator/MeetsMinimumShipLevel.interface.yaml', ] |
| 53 | |
George Keishing | 1930520 | 2017-11-17 12:07:28 -0600 | [diff] [blame] | 54 | # yaml file paths for GPU. |
Sweta Potthuri | 1da4c24 | 2018-01-09 04:42:51 -0600 | [diff] [blame] | 55 | yaml_gpu_list = ['Inventory/Item.interface.yaml', |
George Keishing | 0219da6 | 2018-03-26 03:24:40 -0500 | [diff] [blame] | 56 | 'Inventory/Decorator/Replaceable.interface.yaml', |
Sweta Potthuri | 1da4c24 | 2018-01-09 04:42:51 -0600 | [diff] [blame] | 57 | 'State/Decorator/OperationalStatus.interface.yaml', ] |
Steven Sombar | 5c006d3 | 2018-10-08 09:35:38 -0500 | [diff] [blame] | 58 | |
George Keishing | 1eab883 | 2020-07-09 06:30:57 -0500 | [diff] [blame] | 59 | # Append to inventory list |
| 60 | yaml_inventory_list.append(yaml_fru_list) |
| 61 | yaml_inventory_list.append(yaml_core_list) |
| 62 | yaml_inventory_list.append(yaml_fan_list) |
| 63 | yaml_inventory_list.append(yaml_fan_wc_list) |
| 64 | yaml_inventory_list.append(yaml_gpu_list) |
George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 65 | |
George Keishing | 1eab883 | 2020-07-09 06:30:57 -0500 | [diff] [blame] | 66 | print_var(yaml_inventory_list) |
George Keishing | 9ef1fd4 | 2017-03-10 14:52:38 -0600 | [diff] [blame] | 67 | |
George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 68 | # Populate Inventory data |
| 69 | inventory_dict = {} |
| 70 | |
George Keishing | 1eab883 | 2020-07-09 06:30:57 -0500 | [diff] [blame] | 71 | for inv_index in range(len(yaml_inventory_list)): |
| 72 | print_var(inv_index) |
| 73 | inventory_dict[str(inventory_items[inv_index])] = [] |
| 74 | for rel_yaml_file_path in yaml_inventory_list[inv_index]: |
George Keishing | 9ef1fd4 | 2017-03-10 14:52:38 -0600 | [diff] [blame] | 75 | yaml_file_path = base_dir_path + rel_yaml_file_path |
George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 76 | |
| 77 | # Get the yaml dictionary data |
George Keishing | 9ef1fd4 | 2017-03-10 14:52:38 -0600 | [diff] [blame] | 78 | print_timen("Loading " + yaml_file_path) |
| 79 | f = open(yaml_file_path) |
Yunyun Lin | f87cc0a | 2022-06-08 16:57:04 -0700 | [diff] [blame] | 80 | yaml_data = yaml.safe_load(f) |
George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 81 | f.close() |
| 82 | for item in range(0, len(yaml_data['properties'])): |
| 83 | tmp_data = yaml_data['properties'][item]['name'] |
George Keishing | 1eab883 | 2020-07-09 06:30:57 -0500 | [diff] [blame] | 84 | inventory_dict[str(inventory_items[inv_index])].append(tmp_data) |
George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 85 | |
| 86 | # Pretty print json formatter |
George Keishing | 9ef1fd4 | 2017-03-10 14:52:38 -0600 | [diff] [blame] | 87 | data = json.dumps(inventory_dict, |
| 88 | indent=4, |
| 89 | sort_keys=True, |
| 90 | default=str, |
| 91 | separators=(',', ':')) |
George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 92 | |
| 93 | # Check if there is mismatch in data vs expect list |
| 94 | if len(inventory_dict) != len(inventory_items): |
George Keishing | 178d9bf | 2020-07-09 08:29:29 -0500 | [diff] [blame] | 95 | print_error("The generated list doesn't match Inventory List.\n") |
Patrick Williams | a57fef4 | 2022-12-03 07:00:14 -0600 | [diff] [blame] | 96 | print(data) |
George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 97 | print_var(inventory_items) |
| 98 | sys.exit() |
| 99 | |
| 100 | # Write dictionary data to inventory file |
Patrick Williams | a57fef4 | 2022-12-03 07:00:14 -0600 | [diff] [blame] | 101 | print("\nGenerated Inventory item json format\n") |
| 102 | print(data) |
George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 103 | out = open(fru_inventory_file_path, 'w') |
| 104 | out.write('inventory_dict = ') |
| 105 | out.write(data) |
| 106 | |
| 107 | out.close() |
Patrick Williams | a57fef4 | 2022-12-03 07:00:14 -0600 | [diff] [blame] | 108 | print("\nGenerated Inventory File: %s " % fru_inventory_file_path) |