blob: 4207c5391d9a43eef80e4225a4b59e4e57e3a2c7 [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 Millsf2a8e572024-06-18 09:31:34 -050012VERSION = "DSP8010_2024.1"
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 Tanousa529a6a2024-05-29 16:51:37 -070075
Ed Tanous204c3762022-12-12 09:50:09 -080076for zip_file in zip_ref.infolist():
77 if zip_file.is_dir():
78 continue
79 if zip_file.filename.startswith("csdl/"):
80 csdl_filenames.append(os.path.basename(zip_file.filename))
81 elif zip_file.filename.startswith("json-schema/"):
82 filename = os.path.basename(zip_file.filename)
Ed Tanous8b564552022-09-23 12:03:18 -070083 filenamesplit = filename.split(".")
Ed Tanous8b564552022-09-23 12:03:18 -070084 json_schema_files[filenamesplit[0]].append(filename)
Ed Tanous204c3762022-12-12 09:50:09 -080085 elif zip_file.filename.startswith("openapi/"):
Ed Tanous8b564552022-09-23 12:03:18 -070086 pass
Ed Tanous204c3762022-12-12 09:50:09 -080087 elif zip_file.filename.startswith("dictionaries/"):
Ed Tanous8b564552022-09-23 12:03:18 -070088 pass
89
90# sort the json files by version
91for key, value in json_schema_files.items():
Ed Tanous204c3762022-12-12 09:50:09 -080092 value.sort(key=SchemaVersion, reverse=True)
Ed Tanous8b564552022-09-23 12:03:18 -070093
94# Create a dictionary ordered by schema name
95json_schema_files = OrderedDict(
Ed Tanous204c3762022-12-12 09:50:09 -080096 sorted(json_schema_files.items(), key=lambda x: SchemaVersion(x[0]))
Ed Tanous8b564552022-09-23 12:03:18 -070097)
Ed Tanousa529a6a2024-05-29 16:51:37 -070098
99
Ed Tanous720c9892024-05-11 07:28:09 -0700100for csdl_file in csdl_filenames:
101 with open(os.path.join(schema_path, csdl_file), "wb") as schema_out:
102 content = zip_ref.read(os.path.join("csdl", csdl_file))
103 content = content.replace(b"\r\n", b"\n")
104 schema_out.write(content)
Myung Bae480662d2023-10-04 07:19:38 -0700105
Ed Tanousa529a6a2024-05-29 16:51:37 -0700106for schema_filename, versions in json_schema_files.items():
107 zip_filepath = os.path.join("json-schema", versions[0])
Ed Tanous118b1c72018-09-13 13:45:51 -0700108
Ed Tanousa529a6a2024-05-29 16:51:37 -0700109 with open(
110 os.path.join(json_schema_path, versions[0]), "wb"
111 ) as schema_file:
112 content = zip_ref.read(zip_filepath)
113 content = content.replace(b"\r\n", b"\n")
114 schema_file.write(content)
Ed Tanous683f7272018-07-26 12:47:19 -0700115
116zip_ref.close()
Ed Tanous0ec8b832022-03-14 14:56:47 -0700117
118generate_schema_enums.main()
Carson Labrado9e031402022-07-08 20:56:52 +0000119generate_top_collections()