blob: 0c144546c0e14be7913eaec2a955a51c43e8f344 [file] [log] [blame]
George Keishinge7e91712021-09-03 11:28:44 -05001#!/usr/bin/env python3
George Keishingae7c8202017-02-09 10:33:45 -06002r"""
3Generate an inventory variable file containing a list of properties
4fields from the YAML phosphor-dbus-interfaces repository.
5"""
Sridevi Ramesh47375aa2022-12-08 05:05:31 -06006
George Keishingae7c8202017-02-09 10:33:45 -06007import json
Patrick Williams57318182022-12-08 06:18:26 -06008import os
9import sys
10
11import yaml
12from gen_print import *
George Keishingae7c8202017-02-09 10:33:45 -060013
14lib_path = sys.path[0] + "/../lib"
15sys.path.insert(0, lib_path)
George Keishingae7c8202017-02-09 10:33:45 -060016
17# This list will be longer when more development codes are available.
Patrick Williams57318182022-12-08 06:18:26 -060018inventory_items = ["fru", "core", "fan", "fan_wc", "gpu"]
George Keishingae7c8202017-02-09 10:33:45 -060019print_var(inventory_items)
Patrick Williams57318182022-12-08 06:18:26 -060020fru_inventory_file_path = "inventory.py"
George Keishingae7c8202017-02-09 10:33:45 -060021print_var(fru_inventory_file_path)
22
George Keishing1eab8832020-07-09 06:30:57 -050023# Properties inventory list
24yaml_inventory_list = []
George Keishingae7c8202017-02-09 10:33:45 -060025
26# Clone the phosphor-dbus-interfaces repository
Patrick Williams57318182022-12-08 06:18:26 -060027cmd_buf = "git clone https://github.com/openbmc/phosphor-dbus-interfaces"
George Keishingae7c8202017-02-09 10:33:45 -060028os.system(cmd_buf)
29
Patrick Williams57318182022-12-08 06:18:26 -060030repo_subdir_path = "/phosphor-dbus-interfaces/xyz/openbmc_project/"
George Keishingae7c8202017-02-09 10:33:45 -060031base_dir_path = os.getcwd() + repo_subdir_path
32
George Keishing9ef1fd42017-03-10 14:52:38 -060033# yaml file paths for FRU
Patrick Williams57318182022-12-08 06:18:26 -060034yaml_fru_list = [
35 "Inventory/Item.interface.yaml",
36 "Inventory/Decorator/Asset.interface.yaml",
37 "Inventory/Decorator/Revision.interface.yaml",
38 "Inventory/Decorator/Replaceable.interface.yaml",
39 "Inventory/Decorator/Cacheable.interface.yaml",
40 "State/Decorator/OperationalStatus.interface.yaml",
41]
George Keishingae7c8202017-02-09 10:33:45 -060042
Sweta Potthuri1da4c242018-01-09 04:42:51 -060043# yaml file paths for CORE.
Patrick Williams57318182022-12-08 06:18:26 -060044yaml_core_list = [
45 "Inventory/Item.interface.yaml",
46 "State/Decorator/OperationalStatus.interface.yaml",
47]
George Keishingdc68eff2017-06-12 22:43:18 -050048
49# yaml file paths for fan.
Patrick Williams57318182022-12-08 06:18:26 -060050yaml_fan_list = [
51 "Inventory/Item.interface.yaml",
52 "Inventory/Decorator/MeetsMinimumShipLevel.interface.yaml",
53 "State/Decorator/OperationalStatus.interface.yaml",
54]
Steven Sombar5c006d32018-10-08 09:35:38 -050055
56# yaml file paths for fan_wc (fans in water-cooled system).
Patrick Williams57318182022-12-08 06:18:26 -060057yaml_fan_wc_list = [
58 "Inventory/Item.interface.yaml",
59 "Inventory/Decorator/MeetsMinimumShipLevel.interface.yaml",
60]
Steven Sombar5c006d32018-10-08 09:35:38 -050061
George Keishing19305202017-11-17 12:07:28 -060062# yaml file paths for GPU.
Patrick Williams57318182022-12-08 06:18:26 -060063yaml_gpu_list = [
64 "Inventory/Item.interface.yaml",
65 "Inventory/Decorator/Replaceable.interface.yaml",
66 "State/Decorator/OperationalStatus.interface.yaml",
67]
Steven Sombar5c006d32018-10-08 09:35:38 -050068
George Keishing1eab8832020-07-09 06:30:57 -050069# Append to inventory list
70yaml_inventory_list.append(yaml_fru_list)
71yaml_inventory_list.append(yaml_core_list)
72yaml_inventory_list.append(yaml_fan_list)
73yaml_inventory_list.append(yaml_fan_wc_list)
74yaml_inventory_list.append(yaml_gpu_list)
George Keishingae7c8202017-02-09 10:33:45 -060075
George Keishing1eab8832020-07-09 06:30:57 -050076print_var(yaml_inventory_list)
George Keishing9ef1fd42017-03-10 14:52:38 -060077
George Keishingae7c8202017-02-09 10:33:45 -060078# Populate Inventory data
79inventory_dict = {}
80
George Keishing1eab8832020-07-09 06:30:57 -050081for inv_index in range(len(yaml_inventory_list)):
82 print_var(inv_index)
83 inventory_dict[str(inventory_items[inv_index])] = []
84 for rel_yaml_file_path in yaml_inventory_list[inv_index]:
George Keishing9ef1fd42017-03-10 14:52:38 -060085 yaml_file_path = base_dir_path + rel_yaml_file_path
George Keishingae7c8202017-02-09 10:33:45 -060086
87 # Get the yaml dictionary data
George Keishing9ef1fd42017-03-10 14:52:38 -060088 print_timen("Loading " + yaml_file_path)
89 f = open(yaml_file_path)
Yunyun Linf87cc0a2022-06-08 16:57:04 -070090 yaml_data = yaml.safe_load(f)
George Keishingae7c8202017-02-09 10:33:45 -060091 f.close()
Patrick Williams57318182022-12-08 06:18:26 -060092 for item in range(0, len(yaml_data["properties"])):
93 tmp_data = yaml_data["properties"][item]["name"]
George Keishing1eab8832020-07-09 06:30:57 -050094 inventory_dict[str(inventory_items[inv_index])].append(tmp_data)
George Keishingae7c8202017-02-09 10:33:45 -060095
96# Pretty print json formatter
Patrick Williams57318182022-12-08 06:18:26 -060097data = json.dumps(
98 inventory_dict,
99 indent=4,
100 sort_keys=True,
101 default=str,
102 separators=(",", ":"),
103)
George Keishingae7c8202017-02-09 10:33:45 -0600104
105# Check if there is mismatch in data vs expect list
106if len(inventory_dict) != len(inventory_items):
George Keishing178d9bf2020-07-09 08:29:29 -0500107 print_error("The generated list doesn't match Inventory List.\n")
Patrick Williamsa57fef42022-12-03 07:00:14 -0600108 print(data)
George Keishingae7c8202017-02-09 10:33:45 -0600109 print_var(inventory_items)
110 sys.exit()
111
112# Write dictionary data to inventory file
Patrick Williamsa57fef42022-12-03 07:00:14 -0600113print("\nGenerated Inventory item json format\n")
114print(data)
Patrick Williams57318182022-12-08 06:18:26 -0600115out = open(fru_inventory_file_path, "w")
116out.write("inventory_dict = ")
George Keishingae7c8202017-02-09 10:33:45 -0600117out.write(data)
118
119out.close()
Patrick Williamsa57fef42022-12-03 07:00:14 -0600120print("\nGenerated Inventory File: %s " % fru_inventory_file_path)