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 | dd5c81e | 2024-09-17 13:19:31 -0500 | [diff] [blame] | 12 | VERSION = "DSP8010_2024.3" |
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 | |
| 36 | zipBytesIO = BytesIO(r.content) |
| 37 | zip_ref = zipfile.ZipFile(zipBytesIO) |
| 38 | |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 39 | |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 40 | class SchemaVersion: |
Patrick Williams | fd06b30 | 2022-12-12 10:39:42 -0600 | [diff] [blame] | 41 | """ |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 42 | A Python class for sorting Redfish schema versions. Allows sorting Redfish |
| 43 | versions in the way humans expect, by comparing version strings as lists |
| 44 | (ie 0_2_0 comes before 0_10_0) in the way humans expect. It does case |
| 45 | insensitive schema name comparisons |
Patrick Williams | fd06b30 | 2022-12-12 10:39:42 -0600 | [diff] [blame] | 46 | """ |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 47 | |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 48 | def __init__(self, key): |
| 49 | key = str.casefold(key) |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 50 | |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 51 | split_tup = key.split(".") |
| 52 | self.version_pieces = [split_tup[0]] |
| 53 | if len(split_tup) < 2: |
| 54 | return |
| 55 | version = split_tup[1] |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 56 | |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 57 | if version.startswith("v"): |
| 58 | version = version[1:] |
| 59 | if any(char.isdigit() for char in version): |
Patrick Williams | fd06b30 | 2022-12-12 10:39:42 -0600 | [diff] [blame] | 60 | self.version_pieces.extend([int(x) for x in version.split("_")]) |
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 | def __lt__(self, other): |
| 63 | return self.version_pieces < other.version_pieces |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 64 | |
| 65 | |
Ed Tanous | a529a6a | 2024-05-29 16:51:37 -0700 | [diff] [blame] | 66 | shutil.rmtree(schema_path) |
| 67 | os.makedirs(schema_path) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 68 | |
Ed Tanous | a529a6a | 2024-05-29 16:51:37 -0700 | [diff] [blame] | 69 | shutil.rmtree(json_schema_path) |
| 70 | os.makedirs(json_schema_path) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 71 | |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 72 | csdl_filenames = [] |
| 73 | json_schema_files = defaultdict(list) |
| 74 | |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 75 | for zip_file in zip_ref.infolist(): |
| 76 | if zip_file.is_dir(): |
| 77 | continue |
Gunnar Mills | 28cfceb | 2024-08-22 15:12:11 -0500 | [diff] [blame] | 78 | if zip_file.filename.startswith(VERSION + "/csdl/"): |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 79 | csdl_filenames.append(os.path.basename(zip_file.filename)) |
Gunnar Mills | 28cfceb | 2024-08-22 15:12:11 -0500 | [diff] [blame] | 80 | elif zip_file.filename.startswith(VERSION + "/json-schema/"): |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 81 | filename = os.path.basename(zip_file.filename) |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 82 | filenamesplit = filename.split(".") |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 83 | json_schema_files[filenamesplit[0]].append(filename) |
Gunnar Mills | 28cfceb | 2024-08-22 15:12:11 -0500 | [diff] [blame] | 84 | elif zip_file.filename.startswith(VERSION + "/openapi/"): |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 85 | pass |
Gunnar Mills | 28cfceb | 2024-08-22 15:12:11 -0500 | [diff] [blame] | 86 | elif zip_file.filename.startswith(VERSION + "/dictionaries/"): |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 87 | pass |
| 88 | |
| 89 | # sort the json files by version |
| 90 | for key, value in json_schema_files.items(): |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 91 | value.sort(key=SchemaVersion, reverse=True) |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 92 | |
| 93 | # Create a dictionary ordered by schema name |
| 94 | json_schema_files = OrderedDict( |
Ed Tanous | 204c376 | 2022-12-12 09:50:09 -0800 | [diff] [blame] | 95 | sorted(json_schema_files.items(), key=lambda x: SchemaVersion(x[0])) |
Ed Tanous | 8b56455 | 2022-09-23 12:03:18 -0700 | [diff] [blame] | 96 | ) |
Ed Tanous | a529a6a | 2024-05-29 16:51:37 -0700 | [diff] [blame] | 97 | |
| 98 | |
Ed Tanous | 720c989 | 2024-05-11 07:28:09 -0700 | [diff] [blame] | 99 | for csdl_file in csdl_filenames: |
| 100 | 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] | 101 | content = zip_ref.read(os.path.join(VERSION + "/csdl", csdl_file)) |
Ed Tanous | 720c989 | 2024-05-11 07:28:09 -0700 | [diff] [blame] | 102 | content = content.replace(b"\r\n", b"\n") |
| 103 | schema_out.write(content) |
Myung Bae | 480662d | 2023-10-04 07:19:38 -0700 | [diff] [blame] | 104 | |
Ed Tanous | a529a6a | 2024-05-29 16:51:37 -0700 | [diff] [blame] | 105 | for schema_filename, versions in json_schema_files.items(): |
Gunnar Mills | 28cfceb | 2024-08-22 15:12:11 -0500 | [diff] [blame] | 106 | zip_filepath = os.path.join(VERSION + "/json-schema", versions[0]) |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 107 | |
Ed Tanous | a529a6a | 2024-05-29 16:51:37 -0700 | [diff] [blame] | 108 | with open( |
| 109 | os.path.join(json_schema_path, versions[0]), "wb" |
| 110 | ) as schema_file: |
| 111 | content = zip_ref.read(zip_filepath) |
| 112 | content = content.replace(b"\r\n", b"\n") |
| 113 | schema_file.write(content) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 114 | |
| 115 | zip_ref.close() |
Ed Tanous | 0ec8b83 | 2022-03-14 14:56:47 -0700 | [diff] [blame] | 116 | |
| 117 | generate_schema_enums.main() |
Carson Labrado | 9e03140 | 2022-07-08 20:56:52 +0000 | [diff] [blame] | 118 | generate_top_collections() |