xref: /openbmc/bmcweb/scripts/update_schemas.py (revision a529a6aa)
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_2024.1"
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
36zipBytesIO = BytesIO(r.content)
37zip_ref = zipfile.ZipFile(zipBytesIO)
38
39
40class SchemaVersion:
41    """
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
46    """
47
48    def __init__(self, key):
49        key = str.casefold(key)
50
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]
56
57        if version.startswith("v"):
58            version = version[1:]
59        if any(char.isdigit() for char in version):
60            self.version_pieces.extend([int(x) for x in version.split("_")])
61
62    def __lt__(self, other):
63        return self.version_pieces < other.version_pieces
64
65
66shutil.rmtree(schema_path)
67os.makedirs(schema_path)
68
69shutil.rmtree(json_schema_path)
70os.makedirs(json_schema_path)
71
72csdl_filenames = []
73json_schema_files = defaultdict(list)
74
75
76for 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)
83        filenamesplit = filename.split(".")
84        json_schema_files[filenamesplit[0]].append(filename)
85    elif zip_file.filename.startswith("openapi/"):
86        pass
87    elif zip_file.filename.startswith("dictionaries/"):
88        pass
89
90# sort the json files by version
91for key, value in json_schema_files.items():
92    value.sort(key=SchemaVersion, reverse=True)
93
94# Create a dictionary ordered by schema name
95json_schema_files = OrderedDict(
96    sorted(json_schema_files.items(), key=lambda x: SchemaVersion(x[0]))
97)
98
99
100for 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)
105
106for schema_filename, versions in json_schema_files.items():
107    zip_filepath = os.path.join("json-schema", versions[0])
108
109    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)
115
116zip_ref.close()
117
118generate_schema_enums.main()
119generate_top_collections()
120