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