blob: 1fc5ca8c9f008f25d02b4575e308d1611edacb99 [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
11
12SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
13
14proxies = {
15 'https': os.environ.get("https_proxy", None)
16}
17
18r = requests.get('https://www.dmtf.org/sites/default/files/standards/documents/'
19 'DSP8010_2018.1.zip', proxies=proxies)
20
21r.raise_for_status()
22
23static_path = os.path.realpath(os.path.join(SCRIPT_DIR, "..", "static",
24 "redfish", "v1"))
25
26schema_path = os.path.join(static_path, "schema")
27json_schema_path = os.path.join(static_path, "JsonSchemas")
28
29zipBytesIO = BytesIO(r.content)
30zip_ref = zipfile.ZipFile(zipBytesIO)
31
32# Remove the old files
33if os.path.exists(schema_path):
34 shutil.rmtree(schema_path)
35if os.path.exists(json_schema_path):
36 shutil.rmtree(json_schema_path)
37os.makedirs(schema_path)
38os.makedirs(json_schema_path)
39
40for 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
47schema_files = {}
48for 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
63for 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
71with 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
92zip_ref.close()