blob: 20dfefa6aa6662cf483df95d3ff6e75333c5d562 [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 Millsc6d7a452025-06-17 10:23:44 -050012VERSION = "DSP8010_2025.2"
Ed Tanouscb103132019-10-08 11:34:22 -070013
Ed Tanous683f7272018-07-26 12:47:19 -070014SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
15
Ed Tanous27747912022-09-23 12:50:08 -070016proxies = {"https": os.environ.get("https_proxy", None)}
Ed Tanous683f7272018-07-26 12:47:19 -070017
Ed Tanouscb103132019-10-08 11:34:22 -070018r = requests.get(
Ed Tanous27747912022-09-23 12:50:08 -070019 "https://www.dmtf.org/sites/default/files/standards/documents/"
20 + VERSION
21 + ".zip",
22 proxies=proxies,
23)
Ed Tanous683f7272018-07-26 12:47:19 -070024
25r.raise_for_status()
26
Ed Tanousa529a6a2024-05-29 16:51:37 -070027redfish_core_path = os.path.join(SCRIPT_DIR, "..", "redfish-core")
Ed Tanous81d523a2022-05-25 12:00:51 -070028
Ed Tanousa529a6a2024-05-29 16:51:37 -070029cpp_path = os.path.realpath(os.path.join(redfish_core_path, "include"))
Ed Tanous683f7272018-07-26 12:47:19 -070030
Ed Tanousa529a6a2024-05-29 16:51:37 -070031schema_path = os.path.join(redfish_core_path, "schema", "dmtf", "csdl")
Ed Tanous720c9892024-05-11 07:28:09 -070032json_schema_path = os.path.join(
Ed Tanousa529a6a2024-05-29 16:51:37 -070033 redfish_core_path, "schema", "dmtf", "json-schema"
Ed Tanous720c9892024-05-11 07:28:09 -070034)
Ed Tanous683f7272018-07-26 12:47:19 -070035
Myung Bae9a70ffc2025-06-21 13:47:46 -050036# installed csdl/json symlinks
37schema_installed_path = os.path.join(schema_path, "..", "installed")
38json_schema_installed_path = json_schema_path + "-installed"
39
Ed Tanous683f7272018-07-26 12:47:19 -070040zipBytesIO = BytesIO(r.content)
41zip_ref = zipfile.ZipFile(zipBytesIO)
42
Ed Tanous8b564552022-09-23 12:03:18 -070043
Ed Tanous204c3762022-12-12 09:50:09 -080044class SchemaVersion:
Patrick Williamsfd06b302022-12-12 10:39:42 -060045 """
Ed Tanous204c3762022-12-12 09:50:09 -080046 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 Williamsfd06b302022-12-12 10:39:42 -060050 """
Ed Tanous8b564552022-09-23 12:03:18 -070051
Ed Tanous204c3762022-12-12 09:50:09 -080052 def __init__(self, key):
53 key = str.casefold(key)
Ed Tanous8b564552022-09-23 12:03:18 -070054
Ed Tanous204c3762022-12-12 09:50:09 -080055 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 Tanous8b564552022-09-23 12:03:18 -070060
Ed Tanous204c3762022-12-12 09:50:09 -080061 if version.startswith("v"):
62 version = version[1:]
63 if any(char.isdigit() for char in version):
Patrick Williamsfd06b302022-12-12 10:39:42 -060064 self.version_pieces.extend([int(x) for x in version.split("_")])
Ed Tanous8b564552022-09-23 12:03:18 -070065
Ed Tanous204c3762022-12-12 09:50:09 -080066 def __lt__(self, other):
67 return self.version_pieces < other.version_pieces
Ed Tanous8b564552022-09-23 12:03:18 -070068
69
Ed Tanousa529a6a2024-05-29 16:51:37 -070070shutil.rmtree(schema_path)
71os.makedirs(schema_path)
Ed Tanous683f7272018-07-26 12:47:19 -070072
Ed Tanousa529a6a2024-05-29 16:51:37 -070073shutil.rmtree(json_schema_path)
74os.makedirs(json_schema_path)
Ed Tanous683f7272018-07-26 12:47:19 -070075
Ed Tanous8b564552022-09-23 12:03:18 -070076csdl_filenames = []
77json_schema_files = defaultdict(list)
78
Ed Tanous204c3762022-12-12 09:50:09 -080079for zip_file in zip_ref.infolist():
80 if zip_file.is_dir():
81 continue
Gunnar Mills28cfceb2024-08-22 15:12:11 -050082 if zip_file.filename.startswith(VERSION + "/csdl/"):
Ed Tanous204c3762022-12-12 09:50:09 -080083 csdl_filenames.append(os.path.basename(zip_file.filename))
Gunnar Mills28cfceb2024-08-22 15:12:11 -050084 elif zip_file.filename.startswith(VERSION + "/json-schema/"):
Ed Tanous204c3762022-12-12 09:50:09 -080085 filename = os.path.basename(zip_file.filename)
Ed Tanous8b564552022-09-23 12:03:18 -070086 filenamesplit = filename.split(".")
Ed Tanous8b564552022-09-23 12:03:18 -070087 json_schema_files[filenamesplit[0]].append(filename)
Gunnar Mills28cfceb2024-08-22 15:12:11 -050088 elif zip_file.filename.startswith(VERSION + "/openapi/"):
Ed Tanous8b564552022-09-23 12:03:18 -070089 pass
Gunnar Mills28cfceb2024-08-22 15:12:11 -050090 elif zip_file.filename.startswith(VERSION + "/dictionaries/"):
Ed Tanous8b564552022-09-23 12:03:18 -070091 pass
92
93# sort the json files by version
94for key, value in json_schema_files.items():
Ed Tanous204c3762022-12-12 09:50:09 -080095 value.sort(key=SchemaVersion, reverse=True)
Ed Tanous8b564552022-09-23 12:03:18 -070096
97# Create a dictionary ordered by schema name
98json_schema_files = OrderedDict(
Ed Tanous204c3762022-12-12 09:50:09 -080099 sorted(json_schema_files.items(), key=lambda x: SchemaVersion(x[0]))
Ed Tanous8b564552022-09-23 12:03:18 -0700100)
Ed Tanousa529a6a2024-05-29 16:51:37 -0700101
Myung Bae9a70ffc2025-06-21 13:47:46 -0500102# Get the currently-installed csdl
103csdl_installed_symlinks = set()
104for csdl_symlink in os.listdir(schema_installed_path):
105 csdl_installed_symlinks.add(csdl_symlink)
Ed Tanousa529a6a2024-05-29 16:51:37 -0700106
Myung Bae9a70ffc2025-06-21 13:47:46 -0500107shutil.rmtree(schema_installed_path)
108os.makedirs(schema_installed_path)
109
110# Create csdl files & keep the updated csdl-installed symlinks
Ed Tanous720c9892024-05-11 07:28:09 -0700111for csdl_file in csdl_filenames:
112 with open(os.path.join(schema_path, csdl_file), "wb") as schema_out:
Gunnar Mills28cfceb2024-08-22 15:12:11 -0500113 content = zip_ref.read(os.path.join(VERSION + "/csdl", csdl_file))
Ed Tanous720c9892024-05-11 07:28:09 -0700114 content = content.replace(b"\r\n", b"\n")
115 schema_out.write(content)
Myung Bae480662d2023-10-04 07:19:38 -0700116
Myung Bae9a70ffc2025-06-21 13:47:46 -0500117 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
124json_base_symlinks = defaultdict(list)
125for 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
129shutil.rmtree(json_schema_installed_path)
130os.makedirs(json_schema_installed_path)
131
132# Create json files & keep the installed json symlinks
Ed Tanousa529a6a2024-05-29 16:51:37 -0700133for schema_filename, versions in json_schema_files.items():
Gunnar Mills28cfceb2024-08-22 15:12:11 -0500134 zip_filepath = os.path.join(VERSION + "/json-schema", versions[0])
Ed Tanous118b1c72018-09-13 13:45:51 -0700135
Ed Tanousa529a6a2024-05-29 16:51:37 -0700136 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 Tanous683f7272018-07-26 12:47:19 -0700142
Myung Bae9a70ffc2025-06-21 13:47:46 -0500143 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 Tanous683f7272018-07-26 12:47:19 -0700149zip_ref.close()
Ed Tanous0ec8b832022-03-14 14:56:47 -0700150
151generate_schema_enums.main()
Carson Labrado9e031402022-07-08 20:56:52 +0000152generate_top_collections()