blob: febaf393fb9bc36dad3292ed774e7594b0e3e409 [file] [log] [blame]
Nan Zhou313c1b72022-03-25 11:47:55 -07001#!/usr/bin/env python3
Ed Tanous683f7272018-07-26 12:47:19 -07002import os
Ed Tanous683f7272018-07-26 12:47:19 -07003import shutil
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -06004import zipfile
5from collections import OrderedDict, defaultdict
6from io import BytesIO
7
Ed Tanous0ec8b832022-03-14 14:56:47 -07008import generate_schema_enums
Patrick Williamsfd06b302022-12-12 10:39:42 -06009import requests
Carson Labrado9e031402022-07-08 20:56:52 +000010from generate_schema_collections import generate_top_collections
Ed Tanous118b1c72018-09-13 13:45:51 -070011
Gunnar Millsf2a8e572024-06-18 09:31:34 -050012VERSION = "DSP8010_2024.1"
Ed Tanouscb103132019-10-08 11:34:22 -070013
Ed Tanous27747912022-09-23 12:50:08 -070014WARNING = """/****************************************************************
Ed Tanous81d523a2022-05-25 12:00:51 -070015 * READ THIS WARNING FIRST
16 * This is an auto-generated header which contains definitions
17 * for Redfish DMTF defined schemas.
18 * DO NOT modify this registry outside of running the
19 * update_schemas.py script. The definitions contained within
20 * this file are owned by DMTF. Any modifications to these files
21 * should be first pushed to the relevant registry in the DMTF
22 * github organization.
Ed Tanous27747912022-09-23 12:50:08 -070023 ***************************************************************/"""
Ed Tanous81d523a2022-05-25 12:00:51 -070024
Ed Tanous683f7272018-07-26 12:47:19 -070025SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
26
Ed Tanous27747912022-09-23 12:50:08 -070027proxies = {"https": os.environ.get("https_proxy", None)}
Ed Tanous683f7272018-07-26 12:47:19 -070028
Ed Tanouscb103132019-10-08 11:34:22 -070029r = requests.get(
Ed Tanous27747912022-09-23 12:50:08 -070030 "https://www.dmtf.org/sites/default/files/standards/documents/"
31 + VERSION
32 + ".zip",
33 proxies=proxies,
34)
Ed Tanous683f7272018-07-26 12:47:19 -070035
36r.raise_for_status()
37
Ed Tanous81d523a2022-05-25 12:00:51 -070038
Ed Tanous27747912022-09-23 12:50:08 -070039static_path = os.path.realpath(
40 os.path.join(SCRIPT_DIR, "..", "static", "redfish", "v1")
41)
Ed Tanous683f7272018-07-26 12:47:19 -070042
Ed Tanous81d523a2022-05-25 12:00:51 -070043
Ed Tanous27747912022-09-23 12:50:08 -070044cpp_path = os.path.realpath(
45 os.path.join(SCRIPT_DIR, "..", "redfish-core", "include")
46)
Ed Tanous81d523a2022-05-25 12:00:51 -070047
48
Ed Tanous720c9892024-05-11 07:28:09 -070049schema_path = os.path.join(
50 SCRIPT_DIR, "..", "redfish-core", "schema", "dmtf", "csdl"
51)
52json_schema_path = os.path.join(
53 SCRIPT_DIR, "..", "redfish-core", "schema", "dmtf", "json-schema"
54)
Ed Tanous683f7272018-07-26 12:47:19 -070055
56zipBytesIO = BytesIO(r.content)
57zip_ref = zipfile.ZipFile(zipBytesIO)
58
Ed Tanous8b564552022-09-23 12:03:18 -070059
Ed Tanous204c3762022-12-12 09:50:09 -080060class SchemaVersion:
Patrick Williamsfd06b302022-12-12 10:39:42 -060061 """
Ed Tanous204c3762022-12-12 09:50:09 -080062 A Python class for sorting Redfish schema versions. Allows sorting Redfish
63 versions in the way humans expect, by comparing version strings as lists
64 (ie 0_2_0 comes before 0_10_0) in the way humans expect. It does case
65 insensitive schema name comparisons
Patrick Williamsfd06b302022-12-12 10:39:42 -060066 """
Ed Tanous8b564552022-09-23 12:03:18 -070067
Ed Tanous204c3762022-12-12 09:50:09 -080068 def __init__(self, key):
69 key = str.casefold(key)
Ed Tanous8b564552022-09-23 12:03:18 -070070
Ed Tanous204c3762022-12-12 09:50:09 -080071 split_tup = key.split(".")
72 self.version_pieces = [split_tup[0]]
73 if len(split_tup) < 2:
74 return
75 version = split_tup[1]
Ed Tanous8b564552022-09-23 12:03:18 -070076
Ed Tanous204c3762022-12-12 09:50:09 -080077 if version.startswith("v"):
78 version = version[1:]
79 if any(char.isdigit() for char in version):
Patrick Williamsfd06b302022-12-12 10:39:42 -060080 self.version_pieces.extend([int(x) for x in version.split("_")])
Ed Tanous8b564552022-09-23 12:03:18 -070081
Ed Tanous204c3762022-12-12 09:50:09 -080082 def __lt__(self, other):
83 return self.version_pieces < other.version_pieces
Ed Tanous8b564552022-09-23 12:03:18 -070084
85
Ed Tanous683f7272018-07-26 12:47:19 -070086# Remove the old files
Ed Tanous5b5574a2022-09-26 19:53:36 -070087skip_prefixes = ["Oem", "OpenBMC"]
Ed Tanous683f7272018-07-26 12:47:19 -070088if os.path.exists(schema_path):
Ed Tanous27747912022-09-23 12:50:08 -070089 files = [
90 os.path.join(schema_path, f)
91 for f in os.listdir(schema_path)
Ed Tanous5b5574a2022-09-26 19:53:36 -070092 if not any([f.startswith(prefix) for prefix in skip_prefixes])
Ed Tanous27747912022-09-23 12:50:08 -070093 ]
James Feistaee8d842018-09-10 16:07:40 -070094 for f in files:
95 os.remove(f)
Ed Tanous683f7272018-07-26 12:47:19 -070096if os.path.exists(json_schema_path):
Ed Tanous27747912022-09-23 12:50:08 -070097 files = [
98 os.path.join(json_schema_path, f)
99 for f in os.listdir(json_schema_path)
Ed Tanous5b5574a2022-09-26 19:53:36 -0700100 if not any([f.startswith(prefix) for prefix in skip_prefixes])
Ed Tanous27747912022-09-23 12:50:08 -0700101 ]
Ed Tanous118b1c72018-09-13 13:45:51 -0700102 for f in files:
Ed Tanous27747912022-09-23 12:50:08 -0700103 if os.path.isfile(f):
Ed Tanous118b1c72018-09-13 13:45:51 -0700104 os.remove(f)
105 else:
106 shutil.rmtree(f)
Ed Tanous683f7272018-07-26 12:47:19 -0700107
Ed Tanous118b1c72018-09-13 13:45:51 -0700108if not os.path.exists(schema_path):
109 os.makedirs(schema_path)
110if not os.path.exists(json_schema_path):
111 os.makedirs(json_schema_path)
Ed Tanous683f7272018-07-26 12:47:19 -0700112
Ed Tanous8b564552022-09-23 12:03:18 -0700113csdl_filenames = []
114json_schema_files = defaultdict(list)
115
Ed Tanous204c3762022-12-12 09:50:09 -0800116for zip_file in zip_ref.infolist():
117 if zip_file.is_dir():
118 continue
119 if zip_file.filename.startswith("csdl/"):
120 csdl_filenames.append(os.path.basename(zip_file.filename))
121 elif zip_file.filename.startswith("json-schema/"):
122 filename = os.path.basename(zip_file.filename)
Ed Tanous8b564552022-09-23 12:03:18 -0700123 filenamesplit = filename.split(".")
Ed Tanous8b564552022-09-23 12:03:18 -0700124 json_schema_files[filenamesplit[0]].append(filename)
Ed Tanous204c3762022-12-12 09:50:09 -0800125 elif zip_file.filename.startswith("openapi/"):
Ed Tanous8b564552022-09-23 12:03:18 -0700126 pass
Ed Tanous204c3762022-12-12 09:50:09 -0800127 elif zip_file.filename.startswith("dictionaries/"):
Ed Tanous8b564552022-09-23 12:03:18 -0700128 pass
129
130# sort the json files by version
131for key, value in json_schema_files.items():
Ed Tanous204c3762022-12-12 09:50:09 -0800132 value.sort(key=SchemaVersion, reverse=True)
Ed Tanous8b564552022-09-23 12:03:18 -0700133
134# Create a dictionary ordered by schema name
135json_schema_files = OrderedDict(
Ed Tanous204c3762022-12-12 09:50:09 -0800136 sorted(json_schema_files.items(), key=lambda x: SchemaVersion(x[0]))
Ed Tanous8b564552022-09-23 12:03:18 -0700137)
Ed Tanous720c9892024-05-11 07:28:09 -0700138for csdl_file in csdl_filenames:
139 with open(os.path.join(schema_path, csdl_file), "wb") as schema_out:
140 content = zip_ref.read(os.path.join("csdl", csdl_file))
141 content = content.replace(b"\r\n", b"\n")
142 schema_out.write(content)
Myung Bae480662d2023-10-04 07:19:38 -0700143
Ed Tanous8b564552022-09-23 12:03:18 -0700144for schema, version in json_schema_files.items():
145 zip_filepath = os.path.join("json-schema", version[0])
Ed Tanous118b1c72018-09-13 13:45:51 -0700146
Ed Tanous720c9892024-05-11 07:28:09 -0700147 with open(os.path.join(json_schema_path, version[0]), "wb") as schema_file:
Ed Tanous27747912022-09-23 12:50:08 -0700148 schema_file.write(zip_ref.read(zip_filepath).replace(b"\r\n", b"\n"))
Ed Tanous683f7272018-07-26 12:47:19 -0700149
Ed Tanous27747912022-09-23 12:50:08 -0700150with open(os.path.join(cpp_path, "schemas.hpp"), "w") as hpp_file:
Ed Tanous720c9892024-05-11 07:28:09 -0700151 schemas = []
152 for root, dirs, files in os.walk(
153 os.path.join(SCRIPT_DIR, "..", "static", "redfish", "v1", "schema")
154 ):
155 for csdl_file in sorted(files, key=SchemaVersion):
156 if csdl_file.endswith(".xml"):
157 schemas.append(csdl_file.replace("_v1.xml", ""))
Ed Tanous81d523a2022-05-25 12:00:51 -0700158 hpp_file.write(
159 "#pragma once\n"
160 "{WARNING}\n"
161 "// clang-format off\n"
Ed Tanous3d69fed2022-09-26 20:10:42 -0700162 "#include <array>\n"
Myung Bae3e737422024-04-17 14:33:03 -0500163 "#include <string_view>\n"
Ed Tanous81d523a2022-05-25 12:00:51 -0700164 "\n"
165 "namespace redfish\n"
166 "{{\n"
Myung Bae3e737422024-04-17 14:33:03 -0500167 " constexpr std::array<std::string_view,{SIZE}> schemas {{\n".format(
168 WARNING=WARNING,
Ed Tanous720c9892024-05-11 07:28:09 -0700169 SIZE=len(schemas),
Myung Bae3e737422024-04-17 14:33:03 -0500170 )
Ed Tanous81d523a2022-05-25 12:00:51 -0700171 )
Ed Tanous720c9892024-05-11 07:28:09 -0700172 for schema in schemas:
173 hpp_file.write(' "{}",\n'.format(schema))
Myung Bae480662d2023-10-04 07:19:38 -0700174
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600175 hpp_file.write(" };\n}\n")
Ed Tanous683f7272018-07-26 12:47:19 -0700176
177zip_ref.close()
Ed Tanous0ec8b832022-03-14 14:56:47 -0700178
179generate_schema_enums.main()
Carson Labrado9e031402022-07-08 20:56:52 +0000180generate_top_collections()