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