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 | c6d7a45 | 2025-06-17 10:23:44 -0500 | [diff] [blame] | 12 | VERSION = "DSP8010_2025.2" |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 13 | |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 14 | SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 15 | |
Ed Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 16 | proxies = {"https": os.environ.get("https_proxy", None)} |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 17 | |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 18 | r = requests.get( |
Ed Tanous | 2774791 | 2022-09-23 12:50:08 -0700 | [diff] [blame] | 19 | "https://www.dmtf.org/sites/default/files/standards/documents/" |
| 20 | + VERSION |
| 21 | + ".zip", |
| 22 | proxies=proxies, |
| 23 | ) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 24 | |
| 25 | r.raise_for_status() |
| 26 | |
Ed Tanous | a529a6a | 2024-05-29 16:51:37 -0700 | [diff] [blame] | 27 | redfish_core_path = os.path.join(SCRIPT_DIR, "..", "redfish-core") |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 28 | |
Ed Tanous | a529a6a | 2024-05-29 16:51:37 -0700 | [diff] [blame] | 29 | cpp_path = os.path.realpath(os.path.join(redfish_core_path, "include")) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 30 | |
Ed Tanous | a529a6a | 2024-05-29 16:51:37 -0700 | [diff] [blame] | 31 | schema_path = os.path.join(redfish_core_path, "schema", "dmtf", "csdl") |
Ed Tanous | 720c989 | 2024-05-11 07:28:09 -0700 | [diff] [blame] | 32 | json_schema_path = os.path.join( |
Ed Tanous | a529a6a | 2024-05-29 16:51:37 -0700 | [diff] [blame] | 33 | redfish_core_path, "schema", "dmtf", "json-schema" |
Ed Tanous | 720c989 | 2024-05-11 07:28:09 -0700 | [diff] [blame] | 34 | ) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 35 | |
Myung Bae | 9a70ffc | 2025-06-21 13:47:46 -0500 | [diff] [blame^] | 36 | # installed csdl/json symlinks |
| 37 | schema_installed_path = os.path.join(schema_path, "..", "installed") |
| 38 | json_schema_installed_path = json_schema_path + "-installed" |
| 39 | |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 40 | zipBytesIO = BytesIO(r.content) |
| 41 | zip_ref = zipfile.ZipFile(zipBytesIO) |
| 42 | |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 43 | |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 44 | class SchemaVersion: |
Patrick Williams | fd06b30 | 2022-12-12 10:39:42 -0600 | [diff] [blame] | 45 | """ |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 46 | A Python class for sorting Redfish schema versions. Allows sorting Redfish |
| 47 | versions in the way humans expect, by comparing version strings as lists |
| 48 | (ie 0_2_0 comes before 0_10_0) in the way humans expect. It does case |
| 49 | insensitive schema name comparisons |
Patrick Williams | fd06b30 | 2022-12-12 10:39:42 -0600 | [diff] [blame] | 50 | """ |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 51 | |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 52 | def __init__(self, key): |
| 53 | key = str.casefold(key) |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 54 | |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 55 | split_tup = key.split(".") |
| 56 | self.version_pieces = [split_tup[0]] |
| 57 | if len(split_tup) < 2: |
| 58 | return |
| 59 | version = split_tup[1] |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 60 | |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 61 | if version.startswith("v"): |
| 62 | version = version[1:] |
| 63 | if any(char.isdigit() for char in version): |
Patrick Williams | fd06b30 | 2022-12-12 10:39:42 -0600 | [diff] [blame] | 64 | self.version_pieces.extend([int(x) for x in version.split("_")]) |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 65 | |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 66 | def __lt__(self, other): |
| 67 | return self.version_pieces < other.version_pieces |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 68 | |
| 69 | |
Ed Tanous | a529a6a | 2024-05-29 16:51:37 -0700 | [diff] [blame] | 70 | shutil.rmtree(schema_path) |
| 71 | os.makedirs(schema_path) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 72 | |
Ed Tanous | a529a6a | 2024-05-29 16:51:37 -0700 | [diff] [blame] | 73 | shutil.rmtree(json_schema_path) |
| 74 | os.makedirs(json_schema_path) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 75 | |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 76 | csdl_filenames = [] |
| 77 | json_schema_files = defaultdict(list) |
| 78 | |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 79 | for zip_file in zip_ref.infolist(): |
| 80 | if zip_file.is_dir(): |
| 81 | continue |
Gunnar Mills | 28cfceb | 2024-08-22 15:12:11 -0500 | [diff] [blame] | 82 | if zip_file.filename.startswith(VERSION + "/csdl/"): |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 83 | csdl_filenames.append(os.path.basename(zip_file.filename)) |
Gunnar Mills | 28cfceb | 2024-08-22 15:12:11 -0500 | [diff] [blame] | 84 | elif zip_file.filename.startswith(VERSION + "/json-schema/"): |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 85 | filename = os.path.basename(zip_file.filename) |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 86 | filenamesplit = filename.split(".") |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 87 | json_schema_files[filenamesplit[0]].append(filename) |
Gunnar Mills | 28cfceb | 2024-08-22 15:12:11 -0500 | [diff] [blame] | 88 | elif zip_file.filename.startswith(VERSION + "/openapi/"): |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 89 | pass |
Gunnar Mills | 28cfceb | 2024-08-22 15:12:11 -0500 | [diff] [blame] | 90 | elif zip_file.filename.startswith(VERSION + "/dictionaries/"): |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 91 | pass |
| 92 | |
| 93 | # sort the json files by version |
| 94 | for key, value in json_schema_files.items(): |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 95 | value.sort(key=SchemaVersion, reverse=True) |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 96 | |
| 97 | # Create a dictionary ordered by schema name |
| 98 | json_schema_files = OrderedDict( |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 99 | sorted(json_schema_files.items(), key=lambda x: SchemaVersion(x[0])) |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 100 | ) |
Ed Tanous | a529a6a | 2024-05-29 16:51:37 -0700 | [diff] [blame] | 101 | |
Myung Bae | 9a70ffc | 2025-06-21 13:47:46 -0500 | [diff] [blame^] | 102 | # Get the currently-installed csdl |
| 103 | csdl_installed_symlinks = set() |
| 104 | for csdl_symlink in os.listdir(schema_installed_path): |
| 105 | csdl_installed_symlinks.add(csdl_symlink) |
Ed Tanous | a529a6a | 2024-05-29 16:51:37 -0700 | [diff] [blame] | 106 | |
Myung Bae | 9a70ffc | 2025-06-21 13:47:46 -0500 | [diff] [blame^] | 107 | shutil.rmtree(schema_installed_path) |
| 108 | os.makedirs(schema_installed_path) |
| 109 | |
| 110 | # Create csdl files & keep the updated csdl-installed symlinks |
Ed Tanous | 720c989 | 2024-05-11 07:28:09 -0700 | [diff] [blame] | 111 | for csdl_file in csdl_filenames: |
| 112 | with open(os.path.join(schema_path, csdl_file), "wb") as schema_out: |
Gunnar Mills | 28cfceb | 2024-08-22 15:12:11 -0500 | [diff] [blame] | 113 | content = zip_ref.read(os.path.join(VERSION + "/csdl", csdl_file)) |
Ed Tanous | 720c989 | 2024-05-11 07:28:09 -0700 | [diff] [blame] | 114 | content = content.replace(b"\r\n", b"\n") |
| 115 | schema_out.write(content) |
Myung Bae | 480662d | 2023-10-04 07:19:38 -0700 | [diff] [blame] | 116 | |
Myung Bae | 9a70ffc | 2025-06-21 13:47:46 -0500 | [diff] [blame^] | 117 | if csdl_file in csdl_installed_symlinks: |
| 118 | os.symlink( |
| 119 | os.path.join("..", "csdl", csdl_file), |
| 120 | os.path.join(schema_installed_path, csdl_file), |
| 121 | ) |
| 122 | |
| 123 | # Get the currently-installed json symlinks |
| 124 | json_base_symlinks = defaultdict(list) |
| 125 | for json_symlink in os.listdir(json_schema_installed_path): |
| 126 | base_json_name = json_symlink.split(".")[0] |
| 127 | json_base_symlinks[base_json_name].append(json_symlink) |
| 128 | |
| 129 | shutil.rmtree(json_schema_installed_path) |
| 130 | os.makedirs(json_schema_installed_path) |
| 131 | |
| 132 | # Create json files & keep the installed json symlinks |
Ed Tanous | a529a6a | 2024-05-29 16:51:37 -0700 | [diff] [blame] | 133 | for schema_filename, versions in json_schema_files.items(): |
Gunnar Mills | 28cfceb | 2024-08-22 15:12:11 -0500 | [diff] [blame] | 134 | zip_filepath = os.path.join(VERSION + "/json-schema", versions[0]) |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 135 | |
Ed Tanous | a529a6a | 2024-05-29 16:51:37 -0700 | [diff] [blame] | 136 | with open( |
| 137 | os.path.join(json_schema_path, versions[0]), "wb" |
| 138 | ) as schema_file: |
| 139 | content = zip_ref.read(zip_filepath) |
| 140 | content = content.replace(b"\r\n", b"\n") |
| 141 | schema_file.write(content) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 142 | |
Myung Bae | 9a70ffc | 2025-06-21 13:47:46 -0500 | [diff] [blame^] | 143 | if schema_filename in json_base_symlinks: |
| 144 | os.symlink( |
| 145 | os.path.join("..", "json-schema", versions[0]), |
| 146 | os.path.join(json_schema_installed_path, versions[0]), |
| 147 | ) |
| 148 | |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 149 | zip_ref.close() |
Ed Tanous | 0ec8b83 | 2022-03-14 14:56:47 -0700 | [diff] [blame] | 150 | |
| 151 | generate_schema_enums.main() |
Carson Labrado | 9e03140 | 2022-07-08 20:56:52 +0000 | [diff] [blame] | 152 | generate_top_collections() |