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 |
| 11 | |
| 12 | SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 13 | |
| 14 | proxies = { |
| 15 | 'https': os.environ.get("https_proxy", None) |
| 16 | } |
| 17 | |
| 18 | r = requests.get('https://www.dmtf.org/sites/default/files/standards/documents/' |
| 19 | 'DSP8010_2018.1.zip', proxies=proxies) |
| 20 | |
| 21 | r.raise_for_status() |
| 22 | |
| 23 | static_path = os.path.realpath(os.path.join(SCRIPT_DIR, "..", "static", |
| 24 | "redfish", "v1")) |
| 25 | |
| 26 | schema_path = os.path.join(static_path, "schema") |
| 27 | json_schema_path = os.path.join(static_path, "JsonSchemas") |
| 28 | |
| 29 | zipBytesIO = BytesIO(r.content) |
| 30 | zip_ref = zipfile.ZipFile(zipBytesIO) |
| 31 | |
| 32 | # Remove the old files |
| 33 | if os.path.exists(schema_path): |
| 34 | shutil.rmtree(schema_path) |
| 35 | if os.path.exists(json_schema_path): |
| 36 | shutil.rmtree(json_schema_path) |
| 37 | os.makedirs(schema_path) |
| 38 | os.makedirs(json_schema_path) |
| 39 | |
| 40 | for zip_filepath in zip_ref.namelist(): |
| 41 | |
| 42 | if zip_filepath.startswith('metadata/'): |
| 43 | filename = os.path.basename(zip_filepath) |
| 44 | with open(os.path.join(schema_path, filename), 'wb') as schema_file: |
| 45 | schema_file.write(zip_ref.read(zip_filepath)) |
| 46 | |
| 47 | schema_files = {} |
| 48 | for zip_filepath in zip_ref.namelist(): |
| 49 | if zip_filepath.startswith('json-schema/'): |
| 50 | filename = os.path.basename(zip_filepath) |
| 51 | filenamesplit = filename.split(".") |
| 52 | if len(filenamesplit) == 3: |
| 53 | thisSchemaVersion = schema_files.get(filenamesplit[0], None) |
| 54 | if thisSchemaVersion == None: |
| 55 | schema_files[filenamesplit[0]] = filenamesplit[1] |
| 56 | else: |
| 57 | # need to see if we're a newer version. |
| 58 | if list(map(int, filenamesplit[1][1:].split("_"))) > list(map( |
| 59 | int, thisSchemaVersion[1:].split("_"))): |
| 60 | schema_files[filenamesplit[0]] = filenamesplit[1] |
| 61 | |
| 62 | |
| 63 | for schema, version in schema_files.items(): |
| 64 | basename = schema + "." + version + ".json" |
| 65 | zip_filepath = os.path.join("json-schema", basename) |
| 66 | schemadir = os.path.join(json_schema_path, schema) |
| 67 | os.makedirs(schemadir) |
| 68 | with open(os.path.join(schemadir, "index.json"), 'wb') as schema_file: |
| 69 | schema_file.write(zip_ref.read(zip_filepath)) |
| 70 | |
| 71 | with open(os.path.join(json_schema_path, "index.json"), 'w') as index_file: |
| 72 | members = [{"@odata.id": "/redfish/v1/JsonSchemas/" + schema + "/"} |
| 73 | for schema in schema_files] |
| 74 | |
| 75 | members.sort(key=lambda x: x["@odata.id"]) |
| 76 | |
| 77 | indexData = OrderedDict() |
| 78 | |
| 79 | indexData["@odata.id"] = "/redfish/v1/JsonSchemas" |
| 80 | indexData["@odata.context"] = ("/redfish/v1/$metadata" |
| 81 | "#JsonSchemaFileCollection." |
| 82 | "JsonSchemaFileCollection") |
| 83 | indexData["@odata.type"] = ("#JsonSchemaFileCollection." |
| 84 | "JsonSchemaFileCollection") |
| 85 | indexData["Name"] = "JsonSchemaFile Collection" |
| 86 | indexData["Description"] = "Collection of JsonSchemaFiles" |
| 87 | indexData["Members@odata.count"] = len(schema_files) |
| 88 | indexData["Members"] = members |
| 89 | |
| 90 | json.dump(indexData, index_file, indent=2) |
| 91 | |
| 92 | zip_ref.close() |