forked from ibm-openbmc/bmcweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_schemas.py
executable file
·170 lines (140 loc) · 6.88 KB
/
update_schemas.py
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/python3
import requests
import zipfile
from io import BytesIO
import os
from collections import defaultdict
from collections import OrderedDict
from distutils.version import StrictVersion
import shutil
import json
import glob
import xml.etree.ElementTree as ET
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/'
'DSP8010_2018.3.zip', proxies=proxies)
r.raise_for_status()
static_path = os.path.realpath(os.path.join(SCRIPT_DIR, "..", "static",
"redfish", "v1"))
schema_path = os.path.join(static_path, "schema")
json_schema_path = os.path.join(static_path, "JsonSchemas")
metadata_index_path = os.path.join(static_path, "$metadata", "index.xml")
zipBytesIO = BytesIO(r.content)
zip_ref = zipfile.ZipFile(zipBytesIO)
# Remove the old files
if os.path.exists(schema_path):
files = glob.glob(os.path.join(schema_path, '[!Oem]*'))
for f in files:
os.remove(f)
if os.path.exists(json_schema_path):
files = glob.glob(os.path.join(json_schema_path, '[!Oem]*'))
for f in files:
if (os.path.isfile(f)):
os.remove(f)
else:
shutil.rmtree(f)
os.remove(metadata_index_path)
if not os.path.exists(schema_path):
os.makedirs(schema_path)
if not os.path.exists(json_schema_path):
os.makedirs(json_schema_path)
with open(metadata_index_path, 'w') as metadata_index:
metadata_index.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
metadata_index.write(
"<edmx:Edmx xmlns:edmx=\"http://docs.oasis-open.org/odata/ns/edmx\" Version=\"4.0\">\n")
for zip_filepath in zip_ref.namelist():
if zip_filepath.startswith('DSP8010_2018.3/csdl/') & (zip_filepath != "DSP8010_2018.3/csdl/"):
filename = os.path.basename(zip_filepath)
with open(os.path.join(schema_path, filename), 'wb') as schema_file:
metadata_index.write(
" <edmx:Reference Uri=\"/redfish/v1/schema/" + filename + "\">\n")
content = zip_ref.read(zip_filepath)
xml_root = ET.fromstring(content)
for edmx_child in xml_root:
if edmx_child.tag == "{http://docs.oasis-open.org/odata/ns/edmx}DataServices":
for data_child in edmx_child:
if data_child.tag == "{http://docs.oasis-open.org/odata/ns/edm}Schema":
namespace = data_child.attrib["Namespace"]
if namespace.startswith("RedfishExtensions"):
metadata_index.write(
" <edmx:Include Namespace=\"" + namespace + "\" Alias=\"Redfish\"/>\n")
else:
metadata_index.write(
" <edmx:Include Namespace=\"" + namespace + "\"/>\n")
schema_file.write(content)
metadata_index.write(" </edmx:Reference>\n")
metadata_index.write(""" <edmx:DataServices>
<Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="Service">
<EntityContainer Name="Service" Extends="ServiceRoot.v1_0_0.ServiceContainer"/>
</Schema>
</edmx:DataServices>
""")
#TODO:Issue#32 There's a bug in the script that currently deletes this
#schema (because it's an OEM schema). Because it's the only one, and we
#don't update schemas very often, we just manually fix it. Need a
#permanent fix to the script.
metadata_index.write(" <edmx:Reference Uri=\"/redfish/v1/schema/OemManager_v1.xml\">\n")
metadata_index.write(" <edmx:Include Namespace=\"OemManager\"/>\n")
metadata_index.write(" </edmx:Reference>\n")
metadata_index.write("</edmx:Edmx>\n")
schema_files = {}
for zip_filepath in zip_ref.namelist():
if zip_filepath.startswith('DSP8010_2018.3/json-schema/'):
filename = os.path.basename(zip_filepath)
filenamesplit = filename.split(".")
if len(filenamesplit) == 3:
thisSchemaVersion = schema_files.get(filenamesplit[0], None)
if thisSchemaVersion == None:
schema_files[filenamesplit[0]] = filenamesplit[1]
else:
# need to see if we're a newer version.
if list(map(int, filenamesplit[1][1:].split("_"))) > list(map(
int, thisSchemaVersion[1:].split("_"))):
schema_files[filenamesplit[0]] = filenamesplit[1]
for schema, version in schema_files.items():
basename = schema + "." + version + ".json"
zip_filepath = os.path.join("DSP8010_2018.3/json-schema", basename)
schemadir = os.path.join(json_schema_path, schema)
os.makedirs(schemadir)
location_json = OrderedDict()
location_json["Language"] = "en"
location_json["PublicationUri"] = (
"http://redfish.dmtf.org/schemas/v1/" + schema + ".json")
location_json["Uri"] = (
"/redfish/v1/JsonSchemas/" + schema + "/" + schema + ".json")
index_json = OrderedDict()
index_json["@odata.context"] = "/redfish/v1/$metadata#JsonSchemaFile.JsonSchemaFile"
index_json["@odata.id"] = "/redfish/v1/JsonSchemas/" + schema
index_json["@odata.type"] = "#JsonSchemaFile.v1_0_2.JsonSchemaFile"
index_json["Name"] = schema + " Schema File"
index_json["Schema"] = "#" + schema + "." + schema
index_json["Description"] = schema + " Schema File Location"
index_json["Id"] = schema
index_json["Languages"] = [ "en" ]
index_json["[email protected]"] = 1
index_json["Location"] = [ location_json ]
index_json["[email protected]"] = 1
with open(os.path.join(schemadir, "index.json"), 'w') as schema_file:
json.dump(index_json, schema_file, indent=4)
with open(os.path.join(schemadir, schema + ".json"), 'wb') as schema_file:
schema_file.write(zip_ref.read(zip_filepath))
with open(os.path.join(json_schema_path, "index.json"), 'w') as index_file:
members = [{"@odata.id": "/redfish/v1/JsonSchemas/" + schema}
for schema in schema_files]
members.sort(key=lambda x: x["@odata.id"])
indexData = OrderedDict()
indexData["@odata.id"] = "/redfish/v1/JsonSchemas"
indexData["@odata.context"] = ("/redfish/v1/$metadata"
"#JsonSchemaFileCollection."
"JsonSchemaFileCollection")
indexData["@odata.type"] = ("#JsonSchemaFileCollection."
"JsonSchemaFileCollection")
indexData["Name"] = "JsonSchemaFile Collection"
indexData["Description"] = "Collection of JsonSchemaFiles"
indexData["[email protected]"] = len(schema_files)
indexData["Members"] = members
json.dump(indexData, index_file, indent=2)
zip_ref.close()