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