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 |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 4 | import zipfile |
| 5 | from collections import OrderedDict, defaultdict |
| 6 | from io import BytesIO |
| 7 | |
Ed Tanous | 0ec8b83 | 2022-03-14 14:56:47 -0700 | [diff] [blame] | 8 | import generate_schema_enums |
Patrick Williams | fd06b30 | 2022-12-12 10:39:42 -0600 | [diff] [blame] | 9 | import requests |
Carson Labrado | 9e03140 | 2022-07-08 20:56:52 +0000 | [diff] [blame] | 10 | from generate_schema_collections import generate_top_collections |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 11 | |
Gunnar Mills | f2a8e57 | 2024-06-18 09:31:34 -0500 | [diff] [blame] | 12 | VERSION = "DSP8010_2024.1" |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 13 | |
Ed Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 14 | WARNING = """/**************************************************************** |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 15 | * 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 Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 23 | ***************************************************************/""" |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 24 | |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 25 | SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 26 | |
Ed Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 27 | proxies = {"https": os.environ.get("https_proxy", None)} |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 28 | |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 29 | r = requests.get( |
Ed Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 30 | "https://www.dmtf.org/sites/default/files/standards/documents/" |
| 31 | + VERSION |
| 32 | + ".zip", |
| 33 | proxies=proxies, |
| 34 | ) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 35 | |
| 36 | r.raise_for_status() |
| 37 | |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 38 | |
Ed Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 39 | static_path = os.path.realpath( |
| 40 | os.path.join(SCRIPT_DIR, "..", "static", "redfish", "v1") |
| 41 | ) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 42 | |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 43 | |
Ed Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 44 | cpp_path = os.path.realpath( |
| 45 | os.path.join(SCRIPT_DIR, "..", "redfish-core", "include") |
| 46 | ) |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 47 | |
| 48 | |
Ed Tanous | 720c989 | 2024-05-11 07:28:09 -0700 | [diff] [blame] | 49 | schema_path = os.path.join( |
| 50 | SCRIPT_DIR, "..", "redfish-core", "schema", "dmtf", "csdl" |
| 51 | ) |
| 52 | json_schema_path = os.path.join( |
| 53 | SCRIPT_DIR, "..", "redfish-core", "schema", "dmtf", "json-schema" |
| 54 | ) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 55 | |
| 56 | zipBytesIO = BytesIO(r.content) |
| 57 | zip_ref = zipfile.ZipFile(zipBytesIO) |
| 58 | |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 59 | |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 60 | class SchemaVersion: |
Patrick Williams | fd06b30 | 2022-12-12 10:39:42 -0600 | [diff] [blame] | 61 | """ |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 62 | 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 Williams | fd06b30 | 2022-12-12 10:39:42 -0600 | [diff] [blame] | 66 | """ |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 67 | |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 68 | def __init__(self, key): |
| 69 | key = str.casefold(key) |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 70 | |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 71 | 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 Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 76 | |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 77 | if version.startswith("v"): |
| 78 | version = version[1:] |
| 79 | if any(char.isdigit() for char in version): |
Patrick Williams | fd06b30 | 2022-12-12 10:39:42 -0600 | [diff] [blame] | 80 | self.version_pieces.extend([int(x) for x in version.split("_")]) |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 81 | |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 82 | def __lt__(self, other): |
| 83 | return self.version_pieces < other.version_pieces |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 84 | |
| 85 | |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 86 | # Remove the old files |
Ed Tanous | 5b5574a | 2022-09-26 19:53:36 -0700 | [diff] [blame] | 87 | skip_prefixes = ["Oem", "OpenBMC"] |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 88 | if os.path.exists(schema_path): |
Ed Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 89 | files = [ |
| 90 | os.path.join(schema_path, f) |
| 91 | for f in os.listdir(schema_path) |
Ed Tanous | 5b5574a | 2022-09-26 19:53:36 -0700 | [diff] [blame] | 92 | if not any([f.startswith(prefix) for prefix in skip_prefixes]) |
Ed Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 93 | ] |
James Feist | aee8d84 | 2018-09-10 16:07:40 -0700 | [diff] [blame] | 94 | for f in files: |
| 95 | os.remove(f) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 96 | if os.path.exists(json_schema_path): |
Ed Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 97 | files = [ |
| 98 | os.path.join(json_schema_path, f) |
| 99 | for f in os.listdir(json_schema_path) |
Ed Tanous | 5b5574a | 2022-09-26 19:53:36 -0700 | [diff] [blame] | 100 | if not any([f.startswith(prefix) for prefix in skip_prefixes]) |
Ed Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 101 | ] |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 102 | for f in files: |
Ed Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 103 | if os.path.isfile(f): |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 104 | os.remove(f) |
| 105 | else: |
| 106 | shutil.rmtree(f) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 107 | |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 108 | if not os.path.exists(schema_path): |
| 109 | os.makedirs(schema_path) |
| 110 | if not os.path.exists(json_schema_path): |
| 111 | os.makedirs(json_schema_path) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 112 | |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 113 | csdl_filenames = [] |
| 114 | json_schema_files = defaultdict(list) |
| 115 | |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 116 | for 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 Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 123 | filenamesplit = filename.split(".") |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 124 | json_schema_files[filenamesplit[0]].append(filename) |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 125 | elif zip_file.filename.startswith("openapi/"): |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 126 | pass |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 127 | elif zip_file.filename.startswith("dictionaries/"): |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 128 | pass |
| 129 | |
| 130 | # sort the json files by version |
| 131 | for key, value in json_schema_files.items(): |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 132 | value.sort(key=SchemaVersion, reverse=True) |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 133 | |
| 134 | # Create a dictionary ordered by schema name |
| 135 | json_schema_files = OrderedDict( |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 136 | sorted(json_schema_files.items(), key=lambda x: SchemaVersion(x[0])) |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 137 | ) |
Ed Tanous | 720c989 | 2024-05-11 07:28:09 -0700 | [diff] [blame] | 138 | for 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 Bae | 480662d | 2023-10-04 07:19:38 -0700 | [diff] [blame] | 143 | |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 144 | for schema, version in json_schema_files.items(): |
| 145 | zip_filepath = os.path.join("json-schema", version[0]) |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 146 | |
Ed Tanous | 720c989 | 2024-05-11 07:28:09 -0700 | [diff] [blame] | 147 | 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] | 148 | 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] | 149 | |
Ed Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 150 | 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] | 151 | 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 Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 158 | hpp_file.write( |
| 159 | "#pragma once\n" |
| 160 | "{WARNING}\n" |
| 161 | "// clang-format off\n" |
Ed Tanous | 3d69fed | 2022-09-26 20:10:42 -0700 | [diff] [blame] | 162 | "#include <array>\n" |
Myung Bae | 3e73742 | 2024-04-17 14:33:03 -0500 | [diff] [blame] | 163 | "#include <string_view>\n" |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 164 | "\n" |
| 165 | "namespace redfish\n" |
| 166 | "{{\n" |
Myung Bae | 3e73742 | 2024-04-17 14:33:03 -0500 | [diff] [blame] | 167 | " constexpr std::array<std::string_view,{SIZE}> schemas {{\n".format( |
| 168 | WARNING=WARNING, |
Ed Tanous | 720c989 | 2024-05-11 07:28:09 -0700 | [diff] [blame] | 169 | SIZE=len(schemas), |
Myung Bae | 3e73742 | 2024-04-17 14:33:03 -0500 | [diff] [blame] | 170 | ) |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 171 | ) |
Ed Tanous | 720c989 | 2024-05-11 07:28:09 -0700 | [diff] [blame] | 172 | for schema in schemas: |
| 173 | hpp_file.write(' "{}",\n'.format(schema)) |
Myung Bae | 480662d | 2023-10-04 07:19:38 -0700 | [diff] [blame] | 174 | |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 175 | hpp_file.write(" };\n}\n") |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 176 | |
| 177 | zip_ref.close() |
Ed Tanous | 0ec8b83 | 2022-03-14 14:56:47 -0700 | [diff] [blame] | 178 | |
| 179 | generate_schema_enums.main() |
Carson Labrado | 9e03140 | 2022-07-08 20:56:52 +0000 | [diff] [blame] | 180 | generate_top_collections() |