1#!/usr/bin/env python
2r"""
3Generate an inventory variable file containing a list of properties
4fields from the YAML phosphor-dbus-interfaces repository.
5"""
6import sys
7import os
8import yaml
9import json
10
11lib_path = sys.path[0] + "/../lib"
12sys.path.insert(0, lib_path)
13from gen_print import *
14
15# This list will be longer when more development codes are available.
16inventory_items = ['fru', 'core', 'fan', 'fan_wc', 'gpu']
17print_var(inventory_items)
18fru_inventory_file_path = 'inventory.py'
19print_var(fru_inventory_file_path)
20
21# Properties inventory list
22yaml_inventory_list = []
23
24# Clone the phosphor-dbus-interfaces repository
25cmd_buf = 'git clone https://github.com/openbmc/phosphor-dbus-interfaces'
26os.system(cmd_buf)
27
28repo_subdir_path = '/phosphor-dbus-interfaces/xyz/openbmc_project/'
29base_dir_path = os.getcwd() + repo_subdir_path
30
31# yaml file paths for FRU
32yaml_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', ]
38
39# yaml file paths for CORE.
40yaml_core_list = ['Inventory/Item.interface.yaml',
41                  'State/Decorator/OperationalStatus.interface.yaml', ]
42
43# yaml file paths for fan.
44yaml_fan_list = ['Inventory/Item.interface.yaml',
45                 'Inventory/Decorator/MeetsMinimumShipLevel.interface.yaml',
46                 'State/Decorator/OperationalStatus.interface.yaml', ]
47
48# yaml file paths for fan_wc (fans in water-cooled system).
49yaml_fan_wc_list = ['Inventory/Item.interface.yaml',
50                    'Inventory/Decorator/MeetsMinimumShipLevel.interface.yaml', ]
51
52# yaml file paths for GPU.
53yaml_gpu_list = ['Inventory/Item.interface.yaml',
54                 'Inventory/Decorator/Replaceable.interface.yaml',
55                 'State/Decorator/OperationalStatus.interface.yaml', ]
56
57# Append to inventory list
58yaml_inventory_list.append(yaml_fru_list)
59yaml_inventory_list.append(yaml_core_list)
60yaml_inventory_list.append(yaml_fan_list)
61yaml_inventory_list.append(yaml_fan_wc_list)
62yaml_inventory_list.append(yaml_gpu_list)
63
64print_var(yaml_inventory_list)
65
66# Populate Inventory data
67inventory_dict = {}
68
69for 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]:
73        yaml_file_path = base_dir_path + rel_yaml_file_path
74
75        # Get the yaml dictionary data
76        print_timen("Loading " + yaml_file_path)
77        f = open(yaml_file_path)
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']
82            inventory_dict[str(inventory_items[inv_index])].append(tmp_data)
83
84# Pretty print json formatter
85data = json.dumps(inventory_dict,
86                  indent=4,
87                  sort_keys=True,
88                  default=str,
89                  separators=(',', ':'))
90
91# Check if there is mismatch in data vs expect list
92if len(inventory_dict) != len(inventory_items):
93    print_error("The generated list doesn't match Inventory List.\n")
94    print (data)
95    print_var(inventory_items)
96    sys.exit()
97
98# Write dictionary data to inventory file
99print ("\nGenerated Inventory item json format\n")
100print (data)
101out = open(fru_inventory_file_path, 'w')
102out.write('inventory_dict = ')
103out.write(data)
104
105out.close()
106print ("\nGenerated Inventory File: %s " % fru_inventory_file_path)
107