Nan Zhou | 313c1b7 | 2022-03-25 11:47:55 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 2 | import os |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 3 | import shutil |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 4 | import xml.etree.ElementTree as ET |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 5 | import zipfile |
| 6 | from collections import OrderedDict, defaultdict |
| 7 | from io import BytesIO |
| 8 | |
Ed Tanous | 0ec8b83 | 2022-03-14 14:56:47 -0700 | [diff] [blame] | 9 | import generate_schema_enums |
Patrick Williams | fd06b30 | 2022-12-12 10:39:42 -0600 | [diff] [blame] | 10 | import requests |
Carson Labrado | 9e03140 | 2022-07-08 20:56:52 +0000 | [diff] [blame] | 11 | from generate_schema_collections import generate_top_collections |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 12 | |
Gunnar Mills | 2ae81db | 2024-01-31 14:25:11 -0600 | [diff] [blame] | 13 | VERSION = "DSP8010_2023.3" |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 14 | |
Ed Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 15 | WARNING = """/**************************************************************** |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 16 | * READ THIS WARNING FIRST |
| 17 | * This is an auto-generated header which contains definitions |
| 18 | * for Redfish DMTF defined schemas. |
| 19 | * DO NOT modify this registry outside of running the |
| 20 | * update_schemas.py script. The definitions contained within |
| 21 | * this file are owned by DMTF. Any modifications to these files |
| 22 | * should be first pushed to the relevant registry in the DMTF |
| 23 | * github organization. |
Ed Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 24 | ***************************************************************/""" |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 25 | |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 26 | SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 27 | |
Ed Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 28 | proxies = {"https": os.environ.get("https_proxy", None)} |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 29 | |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 30 | r = requests.get( |
Ed Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 31 | "https://www.dmtf.org/sites/default/files/standards/documents/" |
| 32 | + VERSION |
| 33 | + ".zip", |
| 34 | proxies=proxies, |
| 35 | ) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 36 | |
| 37 | r.raise_for_status() |
| 38 | |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 39 | |
Ed Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 40 | static_path = os.path.realpath( |
| 41 | os.path.join(SCRIPT_DIR, "..", "static", "redfish", "v1") |
| 42 | ) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 43 | |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 44 | |
Ed Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 45 | cpp_path = os.path.realpath( |
| 46 | os.path.join(SCRIPT_DIR, "..", "redfish-core", "include") |
| 47 | ) |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 48 | |
| 49 | |
Ed Tanous | 720c989 | 2024-05-11 07:28:09 -0700 | [diff] [blame] | 50 | schema_path = os.path.join( |
| 51 | SCRIPT_DIR, "..", "redfish-core", "schema", "dmtf", "csdl" |
| 52 | ) |
| 53 | json_schema_path = os.path.join( |
| 54 | SCRIPT_DIR, "..", "redfish-core", "schema", "dmtf", "json-schema" |
| 55 | ) |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 56 | metadata_index_path = os.path.join(static_path, "$metadata", "index.xml") |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 57 | |
| 58 | zipBytesIO = BytesIO(r.content) |
| 59 | zip_ref = zipfile.ZipFile(zipBytesIO) |
| 60 | |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 61 | |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 62 | class SchemaVersion: |
Patrick Williams | fd06b30 | 2022-12-12 10:39:42 -0600 | [diff] [blame] | 63 | """ |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 64 | A Python class for sorting Redfish schema versions. Allows sorting Redfish |
| 65 | versions in the way humans expect, by comparing version strings as lists |
| 66 | (ie 0_2_0 comes before 0_10_0) in the way humans expect. It does case |
| 67 | insensitive schema name comparisons |
Patrick Williams | fd06b30 | 2022-12-12 10:39:42 -0600 | [diff] [blame] | 68 | """ |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 69 | |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 70 | def __init__(self, key): |
| 71 | key = str.casefold(key) |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 72 | |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 73 | split_tup = key.split(".") |
| 74 | self.version_pieces = [split_tup[0]] |
| 75 | if len(split_tup) < 2: |
| 76 | return |
| 77 | version = split_tup[1] |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 78 | |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 79 | if version.startswith("v"): |
| 80 | version = version[1:] |
| 81 | if any(char.isdigit() for char in version): |
Patrick Williams | fd06b30 | 2022-12-12 10:39:42 -0600 | [diff] [blame] | 82 | self.version_pieces.extend([int(x) for x in version.split("_")]) |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 83 | |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 84 | def __lt__(self, other): |
| 85 | return self.version_pieces < other.version_pieces |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 86 | |
| 87 | |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 88 | # Remove the old files |
Ed Tanous | 5b5574a | 2022-09-26 19:53:36 -0700 | [diff] [blame] | 89 | skip_prefixes = ["Oem", "OpenBMC"] |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 90 | if os.path.exists(schema_path): |
Ed Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 91 | files = [ |
| 92 | os.path.join(schema_path, f) |
| 93 | for f in os.listdir(schema_path) |
Ed Tanous | 5b5574a | 2022-09-26 19:53:36 -0700 | [diff] [blame] | 94 | if not any([f.startswith(prefix) for prefix in skip_prefixes]) |
Ed Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 95 | ] |
James Feist | aee8d84 | 2018-09-10 16:07:40 -0700 | [diff] [blame] | 96 | for f in files: |
| 97 | os.remove(f) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 98 | if os.path.exists(json_schema_path): |
Ed Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 99 | files = [ |
| 100 | os.path.join(json_schema_path, f) |
| 101 | for f in os.listdir(json_schema_path) |
Ed Tanous | 5b5574a | 2022-09-26 19:53:36 -0700 | [diff] [blame] | 102 | if not any([f.startswith(prefix) for prefix in skip_prefixes]) |
Ed Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 103 | ] |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 104 | for f in files: |
Ed Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 105 | if os.path.isfile(f): |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 106 | os.remove(f) |
| 107 | else: |
| 108 | shutil.rmtree(f) |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 109 | try: |
| 110 | os.remove(metadata_index_path) |
| 111 | except FileNotFoundError: |
| 112 | pass |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 113 | |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 114 | if not os.path.exists(schema_path): |
| 115 | os.makedirs(schema_path) |
| 116 | if not os.path.exists(json_schema_path): |
| 117 | os.makedirs(json_schema_path) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 118 | |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 119 | csdl_filenames = [] |
| 120 | json_schema_files = defaultdict(list) |
| 121 | |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 122 | for zip_file in zip_ref.infolist(): |
| 123 | if zip_file.is_dir(): |
| 124 | continue |
| 125 | if zip_file.filename.startswith("csdl/"): |
| 126 | csdl_filenames.append(os.path.basename(zip_file.filename)) |
| 127 | elif zip_file.filename.startswith("json-schema/"): |
| 128 | filename = os.path.basename(zip_file.filename) |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 129 | filenamesplit = filename.split(".") |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 130 | json_schema_files[filenamesplit[0]].append(filename) |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 131 | elif zip_file.filename.startswith("openapi/"): |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 132 | pass |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 133 | elif zip_file.filename.startswith("dictionaries/"): |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 134 | pass |
| 135 | |
| 136 | # sort the json files by version |
| 137 | for key, value in json_schema_files.items(): |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 138 | value.sort(key=SchemaVersion, reverse=True) |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 139 | |
| 140 | # Create a dictionary ordered by schema name |
| 141 | json_schema_files = OrderedDict( |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 142 | sorted(json_schema_files.items(), key=lambda x: SchemaVersion(x[0])) |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 143 | ) |
Ed Tanous | 720c989 | 2024-05-11 07:28:09 -0700 | [diff] [blame] | 144 | for csdl_file in csdl_filenames: |
| 145 | with open(os.path.join(schema_path, csdl_file), "wb") as schema_out: |
| 146 | content = zip_ref.read(os.path.join("csdl", csdl_file)) |
| 147 | content = content.replace(b"\r\n", b"\n") |
| 148 | schema_out.write(content) |
Myung Bae | 480662d | 2023-10-04 07:19:38 -0700 | [diff] [blame] | 149 | |
Ed Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 150 | with open(metadata_index_path, "w") as metadata_index: |
Ed Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 151 | metadata_index.write('<?xml version="1.0" encoding="UTF-8"?>\n') |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 152 | metadata_index.write( |
Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 153 | "<edmx:Edmx xmlns:edmx=" |
Ed Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 154 | '"http://docs.oasis-open.org/odata/ns/edmx"' |
| 155 | ' Version="4.0">\n' |
| 156 | ) |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 157 | |
Ed Tanous | 720c989 | 2024-05-11 07:28:09 -0700 | [diff] [blame] | 158 | schema_static_dir = os.path.join( |
| 159 | SCRIPT_DIR, "..", "static", "redfish", "v1", "schema" |
| 160 | ) |
| 161 | for filename in sorted(os.listdir(schema_static_dir), key=SchemaVersion): |
| 162 | if not filename.endswith(".xml"): |
| 163 | continue |
Ed Tanous | 853c0dc | 2022-12-21 13:21:52 -0800 | [diff] [blame] | 164 | |
Myung Bae | 480662d | 2023-10-04 07:19:38 -0700 | [diff] [blame] | 165 | metadata_index.write( |
| 166 | ' <edmx:Reference Uri="/redfish/v1/schema/' + filename + '">\n' |
| 167 | ) |
Ed Tanous | 853c0dc | 2022-12-21 13:21:52 -0800 | [diff] [blame] | 168 | |
Ed Tanous | 720c989 | 2024-05-11 07:28:09 -0700 | [diff] [blame] | 169 | xml_root = ET.parse( |
| 170 | os.path.join(schema_static_dir, filename) |
| 171 | ).getroot() |
Myung Bae | 480662d | 2023-10-04 07:19:38 -0700 | [diff] [blame] | 172 | edmx = "{http://docs.oasis-open.org/odata/ns/edmx}" |
| 173 | edm = "{http://docs.oasis-open.org/odata/ns/edm}" |
| 174 | for edmx_child in xml_root: |
| 175 | if edmx_child.tag == edmx + "DataServices": |
| 176 | for data_child in edmx_child: |
| 177 | if data_child.tag == edm + "Schema": |
| 178 | namespace = data_child.attrib["Namespace"] |
| 179 | if namespace.startswith("RedfishExtensions"): |
| 180 | metadata_index.write( |
| 181 | ' <edmx:Include Namespace="' |
| 182 | + namespace |
| 183 | + '" Alias="Redfish"/>\n' |
| 184 | ) |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 185 | |
Myung Bae | 480662d | 2023-10-04 07:19:38 -0700 | [diff] [blame] | 186 | else: |
| 187 | metadata_index.write( |
| 188 | ' <edmx:Include Namespace="' |
| 189 | + namespace |
| 190 | + '"/>\n' |
| 191 | ) |
| 192 | metadata_index.write(" </edmx:Reference>\n") |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 193 | |
Ed Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 194 | metadata_index.write( |
| 195 | " <edmx:DataServices>\n" |
| 196 | " <Schema " |
| 197 | 'xmlns="http://docs.oasis-open.org/odata/ns/edm" ' |
| 198 | 'Namespace="Service">\n' |
| 199 | ' <EntityContainer Name="Service" ' |
| 200 | 'Extends="ServiceRoot.v1_0_0.ServiceContainer"/>\n' |
| 201 | " </Schema>\n" |
| 202 | " </edmx:DataServices>\n" |
| 203 | ) |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 204 | metadata_index.write("</edmx:Edmx>\n") |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 205 | |
Gunnar Mills | 349a2ac | 2021-01-20 22:29:16 -0600 | [diff] [blame] | 206 | |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 207 | for schema, version in json_schema_files.items(): |
| 208 | zip_filepath = os.path.join("json-schema", version[0]) |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 209 | |
Ed Tanous | 720c989 | 2024-05-11 07:28:09 -0700 | [diff] [blame] | 210 | with open(os.path.join(json_schema_path, version[0]), "wb") as schema_file: |
Ed Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 211 | schema_file.write(zip_ref.read(zip_filepath).replace(b"\r\n", b"\n")) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 212 | |
Ed Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 213 | with open(os.path.join(cpp_path, "schemas.hpp"), "w") as hpp_file: |
Ed Tanous | 720c989 | 2024-05-11 07:28:09 -0700 | [diff] [blame] | 214 | schemas = [] |
| 215 | for root, dirs, files in os.walk( |
| 216 | os.path.join(SCRIPT_DIR, "..", "static", "redfish", "v1", "schema") |
| 217 | ): |
| 218 | for csdl_file in sorted(files, key=SchemaVersion): |
| 219 | if csdl_file.endswith(".xml"): |
| 220 | schemas.append(csdl_file.replace("_v1.xml", "")) |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 221 | hpp_file.write( |
| 222 | "#pragma once\n" |
| 223 | "{WARNING}\n" |
| 224 | "// clang-format off\n" |
Ed Tanous | 3d69fed | 2022-09-26 20:10:42 -0700 | [diff] [blame] | 225 | "#include <array>\n" |
Myung Bae | 3e73742 | 2024-04-17 14:33:03 -0500 | [diff] [blame] | 226 | "#include <string_view>\n" |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 227 | "\n" |
| 228 | "namespace redfish\n" |
| 229 | "{{\n" |
Myung Bae | 3e73742 | 2024-04-17 14:33:03 -0500 | [diff] [blame] | 230 | " constexpr std::array<std::string_view,{SIZE}> schemas {{\n".format( |
| 231 | WARNING=WARNING, |
Ed Tanous | 720c989 | 2024-05-11 07:28:09 -0700 | [diff] [blame] | 232 | SIZE=len(schemas), |
Myung Bae | 3e73742 | 2024-04-17 14:33:03 -0500 | [diff] [blame] | 233 | ) |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 234 | ) |
Ed Tanous | 720c989 | 2024-05-11 07:28:09 -0700 | [diff] [blame] | 235 | for schema in schemas: |
| 236 | hpp_file.write(' "{}",\n'.format(schema)) |
Myung Bae | 480662d | 2023-10-04 07:19:38 -0700 | [diff] [blame] | 237 | |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 238 | hpp_file.write(" };\n}\n") |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 239 | |
| 240 | zip_ref.close() |
Ed Tanous | 0ec8b83 | 2022-03-14 14:56:47 -0700 | [diff] [blame] | 241 | |
| 242 | generate_schema_enums.main() |
Carson Labrado | 9e03140 | 2022-07-08 20:56:52 +0000 | [diff] [blame] | 243 | generate_top_collections() |