blob: d0e3060cdbe046fc11b5a30d046edef5a012fd74 [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 Millsdd5c81e2024-09-17 13:19:31 -050012VERSION = "DSP8010_2024.3"
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
36zipBytesIO = BytesIO(r.content)
37zip_ref = zipfile.ZipFile(zipBytesIO)
38
Ed Tanous8b564552022-09-23 12:03:18 -070039
Ed Tanous204c3762022-12-12 09:50:09 -080040class SchemaVersion:
Patrick Williamsfd06b302022-12-12 10:39:42 -060041 """
Ed Tanous204c3762022-12-12 09:50:09 -080042 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 Williamsfd06b302022-12-12 10:39:42 -060046 """
Ed Tanous8b564552022-09-23 12:03:18 -070047
Ed Tanous204c3762022-12-12 09:50:09 -080048 def __init__(self, key):
49 key = str.casefold(key)
Ed Tanous8b564552022-09-23 12:03:18 -070050
Ed Tanous204c3762022-12-12 09:50:09 -080051 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 Tanous8b564552022-09-23 12:03:18 -070056
Ed Tanous204c3762022-12-12 09:50:09 -080057 if version.startswith("v"):
58 version = version[1:]
59 if any(char.isdigit() for char in version):
Patrick Williamsfd06b302022-12-12 10:39:42 -060060 self.version_pieces.extend([int(x) for x in version.split("_")])
Ed Tanous8b564552022-09-23 12:03:18 -070061
Ed Tanous204c3762022-12-12 09:50:09 -080062 def __lt__(self, other):
63 return self.version_pieces < other.version_pieces
Ed Tanous8b564552022-09-23 12:03:18 -070064
65
Ed Tanousa529a6a2024-05-29 16:51:37 -070066shutil.rmtree(schema_path)
67os.makedirs(schema_path)
Ed Tanous683f7272018-07-26 12:47:19 -070068
Ed Tanousa529a6a2024-05-29 16:51:37 -070069shutil.rmtree(json_schema_path)
70os.makedirs(json_schema_path)
Ed Tanous683f7272018-07-26 12:47:19 -070071
Ed Tanous8b564552022-09-23 12:03:18 -070072csdl_filenames = []
73json_schema_files = defaultdict(list)
74
Ed Tanous204c3762022-12-12 09:50:09 -080075for zip_file in zip_ref.infolist():
76 if zip_file.is_dir():
77 continue
Gunnar Mills28cfceb2024-08-22 15:12:11 -050078 if zip_file.filename.startswith(VERSION + "/csdl/"):
Ed Tanous204c3762022-12-12 09:50:09 -080079 csdl_filenames.append(os.path.basename(zip_file.filename))
Gunnar Mills28cfceb2024-08-22 15:12:11 -050080 elif zip_file.filename.startswith(VERSION + "/json-schema/"):
Ed Tanous204c3762022-12-12 09:50:09 -080081 filename = os.path.basename(zip_file.filename)
Ed Tanous8b564552022-09-23 12:03:18 -070082 filenamesplit = filename.split(".")
Ed Tanous8b564552022-09-23 12:03:18 -070083 json_schema_files[filenamesplit[0]].append(filename)
Gunnar Mills28cfceb2024-08-22 15:12:11 -050084 elif zip_file.filename.startswith(VERSION + "/openapi/"):
Ed Tanous8b564552022-09-23 12:03:18 -070085 pass
Gunnar Mills28cfceb2024-08-22 15:12:11 -050086 elif zip_file.filename.startswith(VERSION + "/dictionaries/"):
Ed Tanous8b564552022-09-23 12:03:18 -070087 pass
88
89# sort the json files by version
90for key, value in json_schema_files.items():
Ed Tanous204c3762022-12-12 09:50:09 -080091 value.sort(key=SchemaVersion, reverse=True)
Ed Tanous8b564552022-09-23 12:03:18 -070092
93# Create a dictionary ordered by schema name
94json_schema_files = OrderedDict(
Ed Tanous204c3762022-12-12 09:50:09 -080095 sorted(json_schema_files.items(), key=lambda x: SchemaVersion(x[0]))
Ed Tanous8b564552022-09-23 12:03:18 -070096)
Ed Tanousa529a6a2024-05-29 16:51:37 -070097
98
Ed Tanous720c9892024-05-11 07:28:09 -070099for csdl_file in csdl_filenames:
100 with open(os.path.join(schema_path, csdl_file), "wb") as schema_out:
Gunnar Mills28cfceb2024-08-22 15:12:11 -0500101 content = zip_ref.read(os.path.join(VERSION + "/csdl", csdl_file))
Ed Tanous720c9892024-05-11 07:28:09 -0700102 content = content.replace(b"\r\n", b"\n")
103 schema_out.write(content)
Myung Bae480662d2023-10-04 07:19:38 -0700104
Ed Tanousa529a6a2024-05-29 16:51:37 -0700105for schema_filename, versions in json_schema_files.items():
Gunnar Mills28cfceb2024-08-22 15:12:11 -0500106 zip_filepath = os.path.join(VERSION + "/json-schema", versions[0])
Ed Tanous118b1c72018-09-13 13:45:51 -0700107
Ed Tanousa529a6a2024-05-29 16:51:37 -0700108 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 Tanous683f7272018-07-26 12:47:19 -0700114
115zip_ref.close()
Ed Tanous0ec8b832022-03-14 14:56:47 -0700116
117generate_schema_enums.main()
Carson Labrado9e031402022-07-08 20:56:52 +0000118generate_top_collections()