Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | import requests |
| 3 | import zipfile |
| 4 | from io import BytesIO |
| 5 | import os |
| 6 | from collections import defaultdict |
| 7 | from collections import OrderedDict |
| 8 | from distutils.version import StrictVersion |
| 9 | import shutil |
| 10 | import json |
James Feist | aee8d84 | 2018-09-10 16:07:40 -0700 | [diff] [blame] | 11 | import glob |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 12 | |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 13 | import xml.etree.ElementTree as ET |
| 14 | |
Gunnar Mills | 844b415 | 2020-06-22 12:44:09 -0500 | [diff] [blame^] | 15 | VERSION = "DSP8010_2020.2" |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 16 | |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 17 | SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 18 | |
| 19 | proxies = { |
| 20 | 'https': os.environ.get("https_proxy", None) |
| 21 | } |
| 22 | |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 23 | r = requests.get( |
| 24 | 'https://www.dmtf.org/sites/default/files/standards/documents/' + |
| 25 | VERSION + |
| 26 | '.zip', |
| 27 | proxies=proxies) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 28 | |
| 29 | r.raise_for_status() |
| 30 | |
| 31 | static_path = os.path.realpath(os.path.join(SCRIPT_DIR, "..", "static", |
| 32 | "redfish", "v1")) |
| 33 | |
| 34 | schema_path = os.path.join(static_path, "schema") |
| 35 | json_schema_path = os.path.join(static_path, "JsonSchemas") |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 36 | metadata_index_path = os.path.join(static_path, "$metadata", "index.xml") |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 37 | |
| 38 | zipBytesIO = BytesIO(r.content) |
| 39 | zip_ref = zipfile.ZipFile(zipBytesIO) |
| 40 | |
| 41 | # Remove the old files |
| 42 | if os.path.exists(schema_path): |
James Feist | aee8d84 | 2018-09-10 16:07:40 -0700 | [diff] [blame] | 43 | files = glob.glob(os.path.join(schema_path, '[!Oem]*')) |
| 44 | for f in files: |
| 45 | os.remove(f) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 46 | if os.path.exists(json_schema_path): |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 47 | 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) |
| 53 | os.remove(metadata_index_path) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 54 | |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 55 | if not os.path.exists(schema_path): |
| 56 | os.makedirs(schema_path) |
| 57 | if not os.path.exists(json_schema_path): |
| 58 | os.makedirs(json_schema_path) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 59 | |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 60 | with 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 Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 67 | if zip_filepath.startswith(VERSION + |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 68 | '/csdl/') & (zip_filepath != VERSION + |
| 69 | "/csdl/") & (zip_filepath != VERSION + |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 70 | "/csdl/"): |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 71 | filename = os.path.basename(zip_filepath) |
| 72 | with open(os.path.join(schema_path, filename), 'wb') as schema_file: |
| 73 | |
| 74 | metadata_index.write( |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 75 | " <edmx:Reference Uri=\"/redfish/v1/schema/" + |
| 76 | filename + |
| 77 | "\">\n") |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 78 | |
| 79 | content = zip_ref.read(zip_filepath) |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 80 | content = content.replace(b'\r\n', b'\n') |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 81 | xml_root = ET.fromstring(content) |
| 82 | |
| 83 | for edmx_child in xml_root: |
| 84 | |
| 85 | if edmx_child.tag == "{http://docs.oasis-open.org/odata/ns/edmx}DataServices": |
| 86 | for data_child in edmx_child: |
| 87 | if data_child.tag == "{http://docs.oasis-open.org/odata/ns/edm}Schema": |
| 88 | namespace = data_child.attrib["Namespace"] |
| 89 | if namespace.startswith("RedfishExtensions"): |
| 90 | metadata_index.write( |
| 91 | " <edmx:Include Namespace=\"" + namespace + "\" Alias=\"Redfish\"/>\n") |
| 92 | |
| 93 | else: |
| 94 | metadata_index.write( |
| 95 | " <edmx:Include Namespace=\"" + namespace + "\"/>\n") |
| 96 | schema_file.write(content) |
| 97 | metadata_index.write(" </edmx:Reference>\n") |
| 98 | |
| 99 | metadata_index.write(""" <edmx:DataServices> |
| 100 | <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="Service"> |
| 101 | <EntityContainer Name="Service" Extends="ServiceRoot.v1_0_0.ServiceContainer"/> |
| 102 | </Schema> |
| 103 | </edmx:DataServices> |
| 104 | """) |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 105 | # TODO:Issue#32 There's a bug in the script that currently deletes this |
Gunnar Mills | 2077899 | 2020-02-06 16:36:47 -0600 | [diff] [blame] | 106 | # schema (because it's an OEM schema). Because it's the only five, and we |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 107 | # don't update schemas very often, we just manually fix it. Need a |
| 108 | # permanent fix to the script. |
| 109 | metadata_index.write( |
| 110 | " <edmx:Reference Uri=\"/redfish/v1/schema/OemManager_v1.xml\">\n") |
Marri Devender Rao | d45d2d0 | 2019-01-21 10:11:34 -0600 | [diff] [blame] | 111 | metadata_index.write(" <edmx:Include Namespace=\"OemManager\"/>\n") |
| 112 | metadata_index.write(" </edmx:Reference>\n") |
Gunnar Mills | 2077899 | 2020-02-06 16:36:47 -0600 | [diff] [blame] | 113 | |
| 114 | metadata_index.write( |
| 115 | " <edmx:Reference Uri=\"/redfish/v1/schema/OemCrashdump_v1.xml\">\n") |
| 116 | metadata_index.write(" <edmx:Include Namespace=\"OemCrashdump.v1_0_0\"/>\n") |
| 117 | metadata_index.write(" </edmx:Reference>\n") |
| 118 | |
| 119 | metadata_index.write( |
| 120 | " <edmx:Reference Uri=\"/redfish/v1/schema/OemComputerSystem_v1.xml\">\n") |
| 121 | metadata_index.write(" <edmx:Include Namespace=\"OemComputerSystem\"/>\n") |
| 122 | metadata_index.write(" </edmx:Reference>\n") |
| 123 | |
| 124 | metadata_index.write( |
| 125 | " <edmx:Reference Uri=\"/redfish/v1/schema/OemVirtualMedia_v1.xml\">\n") |
| 126 | metadata_index.write(" <edmx:Include Namespace=\"OemVirtualMedia\"/>\n") |
| 127 | metadata_index.write(" <edmx:Include Namespace=\"OemVirtualMedia.v1_0_0\"/>\n") |
| 128 | metadata_index.write(" </edmx:Reference>\n") |
| 129 | |
| 130 | metadata_index.write( |
| 131 | " <edmx:Reference Uri=\"/redfish/v1/schema/OemAccountService_v1.xml\">\n") |
| 132 | metadata_index.write(" <edmx:Include Namespace=\"OemAccountService\"/>\n") |
| 133 | metadata_index.write(" <edmx:Include Namespace=\"OemAccountService.v1_0_0\"/>\n") |
| 134 | metadata_index.write(" </edmx:Reference>\n") |
| 135 | |
Ravi Teja | e7d68c3 | 2020-03-15 13:30:41 -0500 | [diff] [blame] | 136 | metadata_index.write( |
| 137 | " <edmx:Reference Uri=\"/redfish/v1/schema/OemLogService_v1.xml\">\n") |
| 138 | metadata_index.write(" <edmx:Include Namespace=\"OemLogService\"/>\n") |
| 139 | metadata_index.write(" <edmx:Include Namespace=\"OemLogService.v1_0_0\"/>\n") |
| 140 | metadata_index.write(" </edmx:Reference>\n") |
| 141 | |
| 142 | metadata_index.write( |
| 143 | " <edmx:Reference Uri=\"/redfish/v1/schema/OemLogEntry_v1.xml\">\n") |
| 144 | metadata_index.write(" <edmx:Include Namespace=\"OemLogEntry\"/>\n") |
| 145 | metadata_index.write(" <edmx:Include Namespace=\"OemLogEntry.v1_0_0\"/>\n") |
| 146 | metadata_index.write(" </edmx:Reference>\n") |
| 147 | |
Sunitha Harish | 9dc5074 | 2020-05-11 00:10:20 -0500 | [diff] [blame] | 148 | metadata_index.write( |
| 149 | " <edmx:Reference Uri=\"/redfish/v1/schema/OemSession_v1.xml\">\n") |
| 150 | metadata_index.write(" <edmx:Include Namespace=\"OemSession\"/>\n") |
| 151 | metadata_index.write(" <edmx:Include Namespace=\"OemSession.v1_0_0\"/>\n") |
| 152 | metadata_index.write(" </edmx:Reference>\n") |
| 153 | |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 154 | metadata_index.write("</edmx:Edmx>\n") |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 155 | |
| 156 | schema_files = {} |
| 157 | for zip_filepath in zip_ref.namelist(): |
Gunnar Mills | a778c02 | 2020-05-12 12:20:36 -0500 | [diff] [blame] | 158 | if zip_filepath.startswith(os.path.join(VERSION, 'json-schema/')): |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 159 | filename = os.path.basename(zip_filepath) |
| 160 | filenamesplit = filename.split(".") |
| 161 | if len(filenamesplit) == 3: |
| 162 | thisSchemaVersion = schema_files.get(filenamesplit[0], None) |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 163 | if thisSchemaVersion is None: |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 164 | schema_files[filenamesplit[0]] = filenamesplit[1] |
| 165 | else: |
| 166 | # need to see if we're a newer version. |
| 167 | if list(map(int, filenamesplit[1][1:].split("_"))) > list(map( |
| 168 | int, thisSchemaVersion[1:].split("_"))): |
| 169 | schema_files[filenamesplit[0]] = filenamesplit[1] |
| 170 | |
| 171 | |
| 172 | for schema, version in schema_files.items(): |
| 173 | basename = schema + "." + version + ".json" |
Gunnar Mills | a778c02 | 2020-05-12 12:20:36 -0500 | [diff] [blame] | 174 | zip_filepath = os.path.join(VERSION, "json-schema", basename) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 175 | schemadir = os.path.join(json_schema_path, schema) |
| 176 | os.makedirs(schemadir) |
Marri Devender Rao | d45d2d0 | 2019-01-21 10:11:34 -0600 | [diff] [blame] | 177 | location_json = OrderedDict() |
| 178 | location_json["Language"] = "en" |
| 179 | location_json["PublicationUri"] = ( |
| 180 | "http://redfish.dmtf.org/schemas/v1/" + schema + ".json") |
| 181 | location_json["Uri"] = ( |
Ed Tanous | 63faafa | 2019-01-03 14:09:56 -0800 | [diff] [blame] | 182 | "/redfish/v1/JsonSchemas/" + schema + "/" + schema + ".json") |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 183 | |
Marri Devender Rao | d45d2d0 | 2019-01-21 10:11:34 -0600 | [diff] [blame] | 184 | index_json = OrderedDict() |
| 185 | index_json["@odata.context"] = "/redfish/v1/$metadata#JsonSchemaFile.JsonSchemaFile" |
Ed Tanous | 63faafa | 2019-01-03 14:09:56 -0800 | [diff] [blame] | 186 | index_json["@odata.id"] = "/redfish/v1/JsonSchemas/" + schema |
Marri Devender Rao | d45d2d0 | 2019-01-21 10:11:34 -0600 | [diff] [blame] | 187 | index_json["@odata.type"] = "#JsonSchemaFile.v1_0_2.JsonSchemaFile" |
| 188 | index_json["Name"] = schema + " Schema File" |
| 189 | index_json["Schema"] = "#" + schema + "." + schema |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 190 | index_json["Description"] = schema + " Schema File Location" |
Marri Devender Rao | d45d2d0 | 2019-01-21 10:11:34 -0600 | [diff] [blame] | 191 | index_json["Id"] = schema |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 192 | index_json["Languages"] = ["en"] |
Marri Devender Rao | d45d2d0 | 2019-01-21 10:11:34 -0600 | [diff] [blame] | 193 | index_json["Languages@odata.count"] = 1 |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 194 | index_json["Location"] = [location_json] |
Marri Devender Rao | d45d2d0 | 2019-01-21 10:11:34 -0600 | [diff] [blame] | 195 | index_json["Location@odata.count"] = 1 |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 196 | |
| 197 | with open(os.path.join(schemadir, "index.json"), 'w') as schema_file: |
| 198 | json.dump(index_json, schema_file, indent=4) |
| 199 | with open(os.path.join(schemadir, schema + ".json"), 'wb') as schema_file: |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 200 | schema_file.write(zip_ref.read(zip_filepath).replace(b'\r\n', b'\n')) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 201 | |
| 202 | with open(os.path.join(json_schema_path, "index.json"), 'w') as index_file: |
Ed Tanous | 6f56d0c | 2019-05-03 17:15:41 -0700 | [diff] [blame] | 203 | members = [{"@odata.id": "/redfish/v1/JsonSchemas/" + schema} |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 204 | for schema in schema_files] |
| 205 | |
| 206 | members.sort(key=lambda x: x["@odata.id"]) |
| 207 | |
| 208 | indexData = OrderedDict() |
| 209 | |
| 210 | indexData["@odata.id"] = "/redfish/v1/JsonSchemas" |
| 211 | indexData["@odata.context"] = ("/redfish/v1/$metadata" |
| 212 | "#JsonSchemaFileCollection." |
| 213 | "JsonSchemaFileCollection") |
| 214 | indexData["@odata.type"] = ("#JsonSchemaFileCollection." |
| 215 | "JsonSchemaFileCollection") |
| 216 | indexData["Name"] = "JsonSchemaFile Collection" |
| 217 | indexData["Description"] = "Collection of JsonSchemaFiles" |
| 218 | indexData["Members@odata.count"] = len(schema_files) |
| 219 | indexData["Members"] = members |
| 220 | |
| 221 | json.dump(indexData, index_file, indent=2) |
| 222 | |
| 223 | zip_ref.close() |