xref: /openbmc/bmcweb/scripts/update_schemas.py (revision 9a70ffcd8ee1f0a8cc15ed8691b8fa05f422548f)
1#!/usr/bin/env python3
2import os
3import shutil
4import zipfile
5from collections import OrderedDict, defaultdict
6from io import BytesIO
7
8import generate_schema_enums
9import requests
10from generate_schema_collections import generate_top_collections
11
12VERSION = "DSP8010_2025.2"
13
14SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
15
16proxies = {"https": os.environ.get("https_proxy", None)}
17
18r = requests.get(
19    "https://www.dmtf.org/sites/default/files/standards/documents/"
20    + VERSION
21    + ".zip",
22    proxies=proxies,
23)
24
25r.raise_for_status()
26
27redfish_core_path = os.path.join(SCRIPT_DIR, "..", "redfish-core")
28
29cpp_path = os.path.realpath(os.path.join(redfish_core_path, "include"))
30
31schema_path = os.path.join(redfish_core_path, "schema", "dmtf", "csdl")
32json_schema_path = os.path.join(
33    redfish_core_path, "schema", "dmtf", "json-schema"
34)
35
36# installed csdl/json symlinks
37schema_installed_path = os.path.join(schema_path, "..", "installed")
38json_schema_installed_path = json_schema_path + "-installed"
39
40zipBytesIO = BytesIO(r.content)
41zip_ref = zipfile.ZipFile(zipBytesIO)
42
43
44class SchemaVersion:
45    """
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
50    """
51
52    def __init__(self, key):
53        key = str.casefold(key)
54
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]
60
61        if version.startswith("v"):
62            version = version[1:]
63        if any(char.isdigit() for char in version):
64            self.version_pieces.extend([int(x) for x in version.split("_")])
65
66    def __lt__(self, other):
67        return self.version_pieces < other.version_pieces
68
69
70shutil.rmtree(schema_path)
71os.makedirs(schema_path)
72
73shutil.rmtree(json_schema_path)
74os.makedirs(json_schema_path)
75
76csdl_filenames = []
77json_schema_files = defaultdict(list)
78
79for zip_file in zip_ref.infolist():
80    if zip_file.is_dir():
81        continue
82    if zip_file.filename.startswith(VERSION + "/csdl/"):
83        csdl_filenames.append(os.path.basename(zip_file.filename))
84    elif zip_file.filename.startswith(VERSION + "/json-schema/"):
85        filename = os.path.basename(zip_file.filename)
86        filenamesplit = filename.split(".")
87        json_schema_files[filenamesplit[0]].append(filename)
88    elif zip_file.filename.startswith(VERSION + "/openapi/"):
89        pass
90    elif zip_file.filename.startswith(VERSION + "/dictionaries/"):
91        pass
92
93# sort the json files by version
94for key, value in json_schema_files.items():
95    value.sort(key=SchemaVersion, reverse=True)
96
97# Create a dictionary ordered by schema name
98json_schema_files = OrderedDict(
99    sorted(json_schema_files.items(), key=lambda x: SchemaVersion(x[0]))
100)
101
102# 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)
106
107shutil.rmtree(schema_installed_path)
108os.makedirs(schema_installed_path)
109
110# Create csdl files & keep the updated csdl-installed symlinks
111for csdl_file in csdl_filenames:
112    with open(os.path.join(schema_path, csdl_file), "wb") as schema_out:
113        content = zip_ref.read(os.path.join(VERSION + "/csdl", csdl_file))
114        content = content.replace(b"\r\n", b"\n")
115        schema_out.write(content)
116
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
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
133for schema_filename, versions in json_schema_files.items():
134    zip_filepath = os.path.join(VERSION + "/json-schema", versions[0])
135
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)
142
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
149zip_ref.close()
150
151generate_schema_enums.main()
152generate_top_collections()
153