summaryrefslogtreecommitdiff
path: root/scripts/update_schemas.py
blob: 20dfefa6aa6662cf483df95d3ff6e75333c5d562 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python3
import os
import shutil
import zipfile
from collections import OrderedDict, defaultdict
from io import BytesIO

import generate_schema_enums
import requests
from generate_schema_collections import generate_top_collections

VERSION = "DSP8010_2025.2"

SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))

proxies = {"https": os.environ.get("https_proxy", None)}

r = requests.get(
    "https://www.dmtf.org/sites/default/files/standards/documents/"
    + VERSION
    + ".zip",
    proxies=proxies,
)

r.raise_for_status()

redfish_core_path = os.path.join(SCRIPT_DIR, "..", "redfish-core")

cpp_path = os.path.realpath(os.path.join(redfish_core_path, "include"))

schema_path = os.path.join(redfish_core_path, "schema", "dmtf", "csdl")
json_schema_path = os.path.join(
    redfish_core_path, "schema", "dmtf", "json-schema"
)

# installed csdl/json symlinks
schema_installed_path = os.path.join(schema_path, "..", "installed")
json_schema_installed_path = json_schema_path + "-installed"

zipBytesIO = BytesIO(r.content)
zip_ref = zipfile.ZipFile(zipBytesIO)


class SchemaVersion:
    """
    A Python class for sorting Redfish schema versions.  Allows sorting Redfish
    versions in the way humans expect, by comparing version strings as lists
    (ie 0_2_0 comes before 0_10_0) in the way humans expect.  It does case
    insensitive schema name comparisons
    """

    def __init__(self, key):
        key = str.casefold(key)

        split_tup = key.split(".")
        self.version_pieces = [split_tup[0]]
        if len(split_tup) < 2:
            return
        version = split_tup[1]

        if version.startswith("v"):
            version = version[1:]
        if any(char.isdigit() for char in version):
            self.version_pieces.extend([int(x) for x in version.split("_")])

    def __lt__(self, other):
        return self.version_pieces < other.version_pieces


shutil.rmtree(schema_path)
os.makedirs(schema_path)

shutil.rmtree(json_schema_path)
os.makedirs(json_schema_path)

csdl_filenames = []
json_schema_files = defaultdict(list)

for zip_file in zip_ref.infolist():
    if zip_file.is_dir():
        continue
    if zip_file.filename.startswith(VERSION + "/csdl/"):
        csdl_filenames.append(os.path.basename(zip_file.filename))
    elif zip_file.filename.startswith(VERSION + "/json-schema/"):
        filename = os.path.basename(zip_file.filename)
        filenamesplit = filename.split(".")
        json_schema_files[filenamesplit[0]].append(filename)
    elif zip_file.filename.startswith(VERSION + "/openapi/"):
        pass
    elif zip_file.filename.startswith(VERSION + "/dictionaries/"):
        pass

# sort the json files by version
for key, value in json_schema_files.items():
    value.sort(key=SchemaVersion, reverse=True)

# Create a dictionary ordered by schema name
json_schema_files = OrderedDict(
    sorted(json_schema_files.items(), key=lambda x: SchemaVersion(x[0]))
)

# Get the currently-installed csdl
csdl_installed_symlinks = set()
for csdl_symlink in os.listdir(schema_installed_path):
    csdl_installed_symlinks.add(csdl_symlink)

shutil.rmtree(schema_installed_path)
os.makedirs(schema_installed_path)

# Create csdl files & keep the updated csdl-installed symlinks
for csdl_file in csdl_filenames:
    with open(os.path.join(schema_path, csdl_file), "wb") as schema_out:
        content = zip_ref.read(os.path.join(VERSION + "/csdl", csdl_file))
        content = content.replace(b"\r\n", b"\n")
        schema_out.write(content)

    if csdl_file in csdl_installed_symlinks:
        os.symlink(
            os.path.join("..", "csdl", csdl_file),
            os.path.join(schema_installed_path, csdl_file),
        )

# Get the currently-installed json symlinks
json_base_symlinks = defaultdict(list)
for json_symlink in os.listdir(json_schema_installed_path):
    base_json_name = json_symlink.split(".")[0]
    json_base_symlinks[base_json_name].append(json_symlink)

shutil.rmtree(json_schema_installed_path)
os.makedirs(json_schema_installed_path)

# Create json files & keep the installed json symlinks
for schema_filename, versions in json_schema_files.items():
    zip_filepath = os.path.join(VERSION + "/json-schema", versions[0])

    with open(
        os.path.join(json_schema_path, versions[0]), "wb"
    ) as schema_file:
        content = zip_ref.read(zip_filepath)
        content = content.replace(b"\r\n", b"\n")
        schema_file.write(content)

    if schema_filename in json_base_symlinks:
        os.symlink(
            os.path.join("..", "json-schema", versions[0]),
            os.path.join(json_schema_installed_path, versions[0]),
        )

zip_ref.close()

generate_schema_enums.main()
generate_top_collections()