| 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 | 1930520 | 2017-11-17 12:07:28 -0600 | [diff] [blame] | 16 | inventory_items = ['fru', 'core', 'fan', '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 |  | 
|  | 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 |  | 
| 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 | 
| George Keishing | 36fedf2 | 2017-03-21 06:30:27 -0500 | [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 | 9ef1fd4 | 2017-03-10 14:52:38 -0600 | [diff] [blame] | 38 | ] | 
| George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 39 |  | 
| George Keishing | 36fedf2 | 2017-03-21 06:30:27 -0500 | [diff] [blame] | 40 | # yaml file paths for CORE | 
|  | 41 | yaml_core_list = [ 'Inventory/Item.interface.yaml', | 
|  | 42 | 'State/Decorator/OperationalStatus.interface.yaml', | 
|  | 43 | ] | 
| George Keishing | dc68eff | 2017-06-12 22:43:18 -0500 | [diff] [blame] | 44 |  | 
|  | 45 | # yaml file paths for fan. | 
|  | 46 | yaml_fan_list = [ 'Inventory/Item.interface.yaml', | 
|  | 47 | 'State/Decorator/OperationalStatus.interface.yaml', | 
|  | 48 | ] | 
| George Keishing | 1930520 | 2017-11-17 12:07:28 -0600 | [diff] [blame] | 49 | # yaml file paths for GPU. | 
|  | 50 | yaml_gpu_list = [ 'Inventory/Item.interface.yaml', | 
|  | 51 | 'State/Decorator/OperationalStatus.interface.yaml', | 
|  | 52 | ] | 
| George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 53 | # Append to master list | 
|  | 54 | yaml_master_list.append(yaml_fru_list) | 
| George Keishing | 36fedf2 | 2017-03-21 06:30:27 -0500 | [diff] [blame] | 55 | yaml_master_list.append(yaml_core_list) | 
| George Keishing | dc68eff | 2017-06-12 22:43:18 -0500 | [diff] [blame] | 56 | yaml_master_list.append(yaml_fan_list) | 
| George Keishing | 1930520 | 2017-11-17 12:07:28 -0600 | [diff] [blame] | 57 | yaml_master_list.append(yaml_gpu_list) | 
| George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 58 |  | 
| George Keishing | 9ef1fd4 | 2017-03-10 14:52:38 -0600 | [diff] [blame] | 59 | print_var(yaml_master_list) | 
|  | 60 |  | 
| George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 61 | # Populate Inventory data | 
|  | 62 | inventory_dict = {} | 
|  | 63 |  | 
| George Keishing | 9ef1fd4 | 2017-03-10 14:52:38 -0600 | [diff] [blame] | 64 | for master_index in range(len(yaml_master_list)): | 
|  | 65 | print_var(master_index) | 
| George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 66 | inventory_dict[str(inventory_items[master_index])] = [] | 
| George Keishing | 9ef1fd4 | 2017-03-10 14:52:38 -0600 | [diff] [blame] | 67 | for rel_yaml_file_path in yaml_master_list[master_index]: | 
|  | 68 | yaml_file_path = base_dir_path + rel_yaml_file_path | 
| George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 69 |  | 
|  | 70 | # Get the yaml dictionary data | 
| George Keishing | 9ef1fd4 | 2017-03-10 14:52:38 -0600 | [diff] [blame] | 71 | print_timen("Loading " + yaml_file_path) | 
|  | 72 | f = open(yaml_file_path) | 
| George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 73 | yaml_data = yaml.load(f) | 
|  | 74 | f.close() | 
|  | 75 | for item in range(0, len(yaml_data['properties'])): | 
|  | 76 | tmp_data = yaml_data['properties'][item]['name'] | 
|  | 77 | inventory_dict[str(inventory_items[master_index])].append(tmp_data) | 
|  | 78 |  | 
|  | 79 | # Pretty print json formatter | 
| George Keishing | 9ef1fd4 | 2017-03-10 14:52:38 -0600 | [diff] [blame] | 80 | data = json.dumps(inventory_dict, | 
|  | 81 | indent=4, | 
|  | 82 | sort_keys=True, | 
|  | 83 | default=str, | 
|  | 84 | separators=(',', ':')) | 
| George Keishing | ae7c820 | 2017-02-09 10:33:45 -0600 | [diff] [blame] | 85 |  | 
|  | 86 | # Check if there is mismatch in data vs expect list | 
|  | 87 | if len(inventory_dict) != len(inventory_items): | 
|  | 88 | print_error("The generated list doesn't match Master Inventory List.\n") | 
|  | 89 | print data | 
|  | 90 | print_var(inventory_items) | 
|  | 91 | sys.exit() | 
|  | 92 |  | 
|  | 93 | # Write dictionary data to inventory file | 
|  | 94 | print "\nGenerated Inventory item json format\n" | 
|  | 95 | print data | 
|  | 96 | out = open(fru_inventory_file_path, 'w') | 
|  | 97 | out.write('inventory_dict = ') | 
|  | 98 | out.write(data) | 
|  | 99 |  | 
|  | 100 | out.close() | 
|  | 101 | print "\nGenerated Inventory File: ", fru_inventory_file_path |