blob: d576179ecb0285dcae912ebf32e4d73dd9673028 [file] [log] [blame]
Ed Tanous683f7272018-07-26 12:47:19 -07001#!/usr/bin/python3
2import requests
3import zipfile
4from io import BytesIO
5import os
6from collections import defaultdict
7from collections import OrderedDict
8from distutils.version import StrictVersion
9import shutil
10import json
James Feistaee8d842018-09-10 16:07:40 -070011import glob
Ed Tanous683f7272018-07-26 12:47:19 -070012
Ed Tanous118b1c72018-09-13 13:45:51 -070013import xml.etree.ElementTree as ET
14
Gunnar Mills09b9d452020-02-11 13:27:39 -060015VERSION = "DSP8010_2019.4"
Ed Tanouscb103132019-10-08 11:34:22 -070016
Ed Tanous683f7272018-07-26 12:47:19 -070017SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
18
19proxies = {
20 'https': os.environ.get("https_proxy", None)
21}
22
Ed Tanouscb103132019-10-08 11:34:22 -070023r = requests.get(
24 'https://www.dmtf.org/sites/default/files/standards/documents/' +
25 VERSION +
26 '.zip',
27 proxies=proxies)
Ed Tanous683f7272018-07-26 12:47:19 -070028
29r.raise_for_status()
30
31static_path = os.path.realpath(os.path.join(SCRIPT_DIR, "..", "static",
32 "redfish", "v1"))
33
34schema_path = os.path.join(static_path, "schema")
35json_schema_path = os.path.join(static_path, "JsonSchemas")
Ed Tanous118b1c72018-09-13 13:45:51 -070036metadata_index_path = os.path.join(static_path, "$metadata", "index.xml")
Ed Tanous683f7272018-07-26 12:47:19 -070037
38zipBytesIO = BytesIO(r.content)
39zip_ref = zipfile.ZipFile(zipBytesIO)
40
41# Remove the old files
42if os.path.exists(schema_path):
James Feistaee8d842018-09-10 16:07:40 -070043 files = glob.glob(os.path.join(schema_path, '[!Oem]*'))
44 for f in files:
45 os.remove(f)
Ed Tanous683f7272018-07-26 12:47:19 -070046if os.path.exists(json_schema_path):
Ed Tanous118b1c72018-09-13 13:45:51 -070047 files = glob.glob(os.path.join(json_schema_path, '[!Oem]*'))
48 for f in files:
49 if (os.path.isfile(f)):
50 os.remove(f)
51 else:
52 shutil.rmtree(f)
53os.remove(metadata_index_path)
Ed Tanous683f7272018-07-26 12:47:19 -070054
Ed Tanous118b1c72018-09-13 13:45:51 -070055if not os.path.exists(schema_path):
56 os.makedirs(schema_path)
57if not os.path.exists(json_schema_path):
58 os.makedirs(json_schema_path)
Ed Tanous683f7272018-07-26 12:47:19 -070059
Ed Tanous118b1c72018-09-13 13:45:51 -070060with open(metadata_index_path, 'w') as metadata_index:
61
62 metadata_index.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
63 metadata_index.write(
64 "<edmx:Edmx xmlns:edmx=\"http://docs.oasis-open.org/odata/ns/edmx\" Version=\"4.0\">\n")
65
66 for zip_filepath in zip_ref.namelist():
Ed Tanouscb103132019-10-08 11:34:22 -070067 if zip_filepath.startswith(VERSION +
Gunnar Mills09b9d452020-02-11 13:27:39 -060068 '/' +
69 VERSION +
Ed Tanouscb103132019-10-08 11:34:22 -070070 '/csdl/') & (zip_filepath != VERSION +
71 "/csdl/") & (zip_filepath != VERSION +
Gunnar Mills09b9d452020-02-11 13:27:39 -060072 '/' +
73 VERSION +
Ed Tanouscb103132019-10-08 11:34:22 -070074 "/csdl/"):
Ed Tanous118b1c72018-09-13 13:45:51 -070075 filename = os.path.basename(zip_filepath)
76 with open(os.path.join(schema_path, filename), 'wb') as schema_file:
77
78 metadata_index.write(
Ed Tanouscb103132019-10-08 11:34:22 -070079 " <edmx:Reference Uri=\"/redfish/v1/schema/" +
80 filename +
81 "\">\n")
Ed Tanous118b1c72018-09-13 13:45:51 -070082
83 content = zip_ref.read(zip_filepath)
Ed Tanouscb103132019-10-08 11:34:22 -070084 content = content.replace(b'\r\n', b'\n')
Ed Tanous118b1c72018-09-13 13:45:51 -070085 xml_root = ET.fromstring(content)
86
87 for edmx_child in xml_root:
88
89 if edmx_child.tag == "{http://docs.oasis-open.org/odata/ns/edmx}DataServices":
90 for data_child in edmx_child:
91 if data_child.tag == "{http://docs.oasis-open.org/odata/ns/edm}Schema":
92 namespace = data_child.attrib["Namespace"]
93 if namespace.startswith("RedfishExtensions"):
94 metadata_index.write(
95 " <edmx:Include Namespace=\"" + namespace + "\" Alias=\"Redfish\"/>\n")
96
97 else:
98 metadata_index.write(
99 " <edmx:Include Namespace=\"" + namespace + "\"/>\n")
100 schema_file.write(content)
101 metadata_index.write(" </edmx:Reference>\n")
102
103 metadata_index.write(""" <edmx:DataServices>
104 <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="Service">
105 <EntityContainer Name="Service" Extends="ServiceRoot.v1_0_0.ServiceContainer"/>
106 </Schema>
107 </edmx:DataServices>
108""")
Ed Tanouscb103132019-10-08 11:34:22 -0700109 # TODO:Issue#32 There's a bug in the script that currently deletes this
Gunnar Mills20778992020-02-06 16:36:47 -0600110 # schema (because it's an OEM schema). Because it's the only five, and we
Ed Tanouscb103132019-10-08 11:34:22 -0700111 # don't update schemas very often, we just manually fix it. Need a
112 # permanent fix to the script.
113 metadata_index.write(
114 " <edmx:Reference Uri=\"/redfish/v1/schema/OemManager_v1.xml\">\n")
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600115 metadata_index.write(" <edmx:Include Namespace=\"OemManager\"/>\n")
116 metadata_index.write(" </edmx:Reference>\n")
Gunnar Mills20778992020-02-06 16:36:47 -0600117
118 metadata_index.write(
119 " <edmx:Reference Uri=\"/redfish/v1/schema/OemCrashdump_v1.xml\">\n")
120 metadata_index.write(" <edmx:Include Namespace=\"OemCrashdump.v1_0_0\"/>\n")
121 metadata_index.write(" </edmx:Reference>\n")
122
123 metadata_index.write(
124 " <edmx:Reference Uri=\"/redfish/v1/schema/OemComputerSystem_v1.xml\">\n")
125 metadata_index.write(" <edmx:Include Namespace=\"OemComputerSystem\"/>\n")
126 metadata_index.write(" </edmx:Reference>\n")
127
128 metadata_index.write(
129 " <edmx:Reference Uri=\"/redfish/v1/schema/OemVirtualMedia_v1.xml\">\n")
130 metadata_index.write(" <edmx:Include Namespace=\"OemVirtualMedia\"/>\n")
131 metadata_index.write(" <edmx:Include Namespace=\"OemVirtualMedia.v1_0_0\"/>\n")
132 metadata_index.write(" </edmx:Reference>\n")
133
134 metadata_index.write(
135 " <edmx:Reference Uri=\"/redfish/v1/schema/OemAccountService_v1.xml\">\n")
136 metadata_index.write(" <edmx:Include Namespace=\"OemAccountService\"/>\n")
137 metadata_index.write(" <edmx:Include Namespace=\"OemAccountService.v1_0_0\"/>\n")
138 metadata_index.write(" </edmx:Reference>\n")
139
Ed Tanous118b1c72018-09-13 13:45:51 -0700140 metadata_index.write("</edmx:Edmx>\n")
Ed Tanous683f7272018-07-26 12:47:19 -0700141
142schema_files = {}
143for zip_filepath in zip_ref.namelist():
Gunnar Mills09b9d452020-02-11 13:27:39 -0600144 if zip_filepath.startswith(os.path.join(VERSION, VERSION, 'json-schema/')):
Ed Tanous683f7272018-07-26 12:47:19 -0700145 filename = os.path.basename(zip_filepath)
146 filenamesplit = filename.split(".")
147 if len(filenamesplit) == 3:
148 thisSchemaVersion = schema_files.get(filenamesplit[0], None)
Ed Tanouscb103132019-10-08 11:34:22 -0700149 if thisSchemaVersion is None:
Ed Tanous683f7272018-07-26 12:47:19 -0700150 schema_files[filenamesplit[0]] = filenamesplit[1]
151 else:
152 # need to see if we're a newer version.
153 if list(map(int, filenamesplit[1][1:].split("_"))) > list(map(
154 int, thisSchemaVersion[1:].split("_"))):
155 schema_files[filenamesplit[0]] = filenamesplit[1]
156
157
158for schema, version in schema_files.items():
159 basename = schema + "." + version + ".json"
Gunnar Mills09b9d452020-02-11 13:27:39 -0600160 zip_filepath = os.path.join(VERSION, VERSION, "json-schema", basename)
Ed Tanous683f7272018-07-26 12:47:19 -0700161 schemadir = os.path.join(json_schema_path, schema)
162 os.makedirs(schemadir)
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600163 location_json = OrderedDict()
164 location_json["Language"] = "en"
165 location_json["PublicationUri"] = (
166 "http://redfish.dmtf.org/schemas/v1/" + schema + ".json")
167 location_json["Uri"] = (
Ed Tanous63faafa2019-01-03 14:09:56 -0800168 "/redfish/v1/JsonSchemas/" + schema + "/" + schema + ".json")
Ed Tanous118b1c72018-09-13 13:45:51 -0700169
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600170 index_json = OrderedDict()
171 index_json["@odata.context"] = "/redfish/v1/$metadata#JsonSchemaFile.JsonSchemaFile"
Ed Tanous63faafa2019-01-03 14:09:56 -0800172 index_json["@odata.id"] = "/redfish/v1/JsonSchemas/" + schema
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600173 index_json["@odata.type"] = "#JsonSchemaFile.v1_0_2.JsonSchemaFile"
174 index_json["Name"] = schema + " Schema File"
175 index_json["Schema"] = "#" + schema + "." + schema
Ed Tanouscb103132019-10-08 11:34:22 -0700176 index_json["Description"] = schema + " Schema File Location"
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600177 index_json["Id"] = schema
Ed Tanouscb103132019-10-08 11:34:22 -0700178 index_json["Languages"] = ["en"]
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600179 index_json["Languages@odata.count"] = 1
Ed Tanouscb103132019-10-08 11:34:22 -0700180 index_json["Location"] = [location_json]
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600181 index_json["Location@odata.count"] = 1
Ed Tanous118b1c72018-09-13 13:45:51 -0700182
183 with open(os.path.join(schemadir, "index.json"), 'w') as schema_file:
184 json.dump(index_json, schema_file, indent=4)
185 with open(os.path.join(schemadir, schema + ".json"), 'wb') as schema_file:
Ed Tanouscb103132019-10-08 11:34:22 -0700186 schema_file.write(zip_ref.read(zip_filepath).replace(b'\r\n', b'\n'))
Ed Tanous683f7272018-07-26 12:47:19 -0700187
188with open(os.path.join(json_schema_path, "index.json"), 'w') as index_file:
Ed Tanous6f56d0c2019-05-03 17:15:41 -0700189 members = [{"@odata.id": "/redfish/v1/JsonSchemas/" + schema}
Ed Tanous683f7272018-07-26 12:47:19 -0700190 for schema in schema_files]
191
192 members.sort(key=lambda x: x["@odata.id"])
193
194 indexData = OrderedDict()
195
196 indexData["@odata.id"] = "/redfish/v1/JsonSchemas"
197 indexData["@odata.context"] = ("/redfish/v1/$metadata"
198 "#JsonSchemaFileCollection."
199 "JsonSchemaFileCollection")
200 indexData["@odata.type"] = ("#JsonSchemaFileCollection."
201 "JsonSchemaFileCollection")
202 indexData["Name"] = "JsonSchemaFile Collection"
203 indexData["Description"] = "Collection of JsonSchemaFiles"
204 indexData["Members@odata.count"] = len(schema_files)
205 indexData["Members"] = members
206
207 json.dump(indexData, index_file, indent=2)
208
209zip_ref.close()