Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -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 | import glob |
| 12 | import subprocess |
| 13 | |
| 14 | import xml.etree.ElementTree as ET |
| 15 | |
| 16 | REGISTRY_HEADER = '''/* |
James Feist | e51c710 | 2020-03-17 10:38:18 -0700 | [diff] [blame] | 17 | // Copyright (c) 2020 Intel Corporation |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 18 | // |
| 19 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 20 | // you may not use this file except in compliance with the License. |
| 21 | // You may obtain a copy of the License at |
| 22 | // |
| 23 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 24 | // |
| 25 | // Unless required by applicable law or agreed to in writing, software |
| 26 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 27 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 28 | // See the License for the specific language governing permissions and |
| 29 | // limitations under the License. |
| 30 | */ |
| 31 | /**************************************************************** |
| 32 | * This is an auto-generated header which contains definitions |
| 33 | * for Redfish DMTF defined messages. |
| 34 | ***************************************************************/ |
| 35 | #pragma once |
| 36 | #include <registries.hpp> |
| 37 | |
| 38 | namespace redfish::message_registries::{} |
| 39 | {{ |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 40 | ''' |
| 41 | |
| 42 | SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 43 | |
Jason M. Bills | ac70637 | 2020-02-18 11:36:47 -0800 | [diff] [blame] | 44 | include_path = os.path.realpath( |
| 45 | os.path.join( |
| 46 | SCRIPT_DIR, |
| 47 | "..", |
| 48 | "redfish-core", |
| 49 | "include", |
| 50 | "registries")) |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 51 | |
| 52 | proxies = { |
| 53 | 'https': os.environ.get("https_proxy", None) |
| 54 | } |
| 55 | |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 56 | |
James Feist | e51c710 | 2020-03-17 10:38:18 -0700 | [diff] [blame] | 57 | def make_getter(dmtf_name, header_name, type_name): |
| 58 | url = 'https://redfish.dmtf.org/registries/{}'.format(dmtf_name) |
| 59 | dmtf = requests.get(url, proxies=proxies) |
| 60 | dmtf.raise_for_status() |
| 61 | json_file = json.loads(dmtf.text) |
| 62 | path = os.path.join(include_path, header_name) |
| 63 | return (path, json_file, type_name, url) |
| 64 | |
| 65 | |
| 66 | files = [] |
| 67 | files.append(make_getter('Base.1.4.0.json', |
| 68 | 'base_message_registry.hpp', 'base')) |
| 69 | files.append(make_getter('TaskEvent.1.0.1.json', |
| 70 | 'task_event_message_registry.hpp', 'task_event')) |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 71 | |
| 72 | # Remove the old files |
James Feist | e51c710 | 2020-03-17 10:38:18 -0700 | [diff] [blame] | 73 | for file, json, namespace, url in files: |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 74 | try: |
| 75 | os.remove(file) |
Jason M. Bills | ac70637 | 2020-02-18 11:36:47 -0800 | [diff] [blame] | 76 | except BaseException: |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 77 | print("{} not found".format(file)) |
| 78 | |
| 79 | with open(file, 'w') as registry: |
| 80 | registry.write(REGISTRY_HEADER.format(namespace)) |
Jason M. Bills | 351d306 | 2019-03-27 12:58:21 -0700 | [diff] [blame] | 81 | # Parse the Registry header info |
| 82 | registry.write("const Header header = {") |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 83 | registry.write("\"{}\",".format(json["@Redfish.Copyright"])) |
| 84 | registry.write("\"{}\",".format(json["@odata.type"])) |
| 85 | registry.write("\"{}\",".format(json["Id"])) |
Jason M. Bills | ac70637 | 2020-02-18 11:36:47 -0800 | [diff] [blame] | 86 | registry.write("\"{}\",".format(json["Name"])) |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 87 | registry.write("\"{}\",".format(json["Language"])) |
| 88 | registry.write("\"{}\",".format(json["Description"])) |
Jason M. Bills | ac70637 | 2020-02-18 11:36:47 -0800 | [diff] [blame] | 89 | registry.write("\"{}\",".format(json["RegistryPrefix"])) |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 90 | registry.write("\"{}\",".format(json["RegistryVersion"])) |
| 91 | registry.write("\"{}\",".format(json["OwningEntity"])) |
Jason M. Bills | 351d306 | 2019-03-27 12:58:21 -0700 | [diff] [blame] | 92 | registry.write("};") |
| 93 | |
James Feist | e51c710 | 2020-03-17 10:38:18 -0700 | [diff] [blame] | 94 | registry.write('constexpr const char * url = "{}";\n\n'.format(url)) |
Jason M. Bills | 351d306 | 2019-03-27 12:58:21 -0700 | [diff] [blame] | 95 | # Parse each Message entry |
Jason M. Bills | ac70637 | 2020-02-18 11:36:47 -0800 | [diff] [blame] | 96 | registry.write("constexpr std::array<MessageEntry, {}> registry = {{".format( |
| 97 | len(json["Messages"]))) |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 98 | for messageId, message in sorted(json["Messages"].items()): |
| 99 | registry.write("MessageEntry{") |
| 100 | registry.write("\"{}\",".format(messageId)) |
| 101 | registry.write("{") |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 102 | registry.write("\"{}\",".format(message["Description"])) |
| 103 | registry.write("\"{}\",".format(message["Message"])) |
| 104 | registry.write("\"{}\",".format(message["Severity"])) |
| 105 | registry.write("{},".format(message["NumberOfArgs"])) |
| 106 | registry.write("{") |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 107 | paramTypes = message.get("ParamTypes") |
| 108 | if paramTypes: |
| 109 | for paramType in paramTypes: |
| 110 | registry.write("\"{}\",".format(paramType)) |
| 111 | registry.write("},") |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 112 | registry.write("\"{}\",".format(message["Resolution"])) |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 113 | registry.write("}},") |
| 114 | registry.write("};}\n") |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 115 | subprocess.check_call(["clang-format-6.0", "-i", file]) |