|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright lowRISC contributors (COSMIC project). |
| 3 | +# Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +import jsonschema |
| 7 | + |
| 8 | +try: |
| 9 | + import json |
| 10 | + import pathlib |
| 11 | + import urllib.error |
| 12 | + import urllib.request |
| 13 | + |
| 14 | + url_string = ('https://raw.githubusercontent.com/' |
| 15 | + 'marnovandermaas/tool-schema/refs/' |
| 16 | + 'heads/main/tool_schema.json') |
| 17 | + |
| 18 | + with urllib.request.urlopen(url_string) as url: |
| 19 | + schema = json.loads(url.read()) |
| 20 | + |
| 21 | + with pathlib.Path('tool_data.json').open() as valid_data_file: |
| 22 | + tool_data = json.load(valid_data_file) |
| 23 | +except FileNotFoundError: |
| 24 | + print('Error: The tool data file was not found.') |
| 25 | + raise SystemExit(1) from FileNotFoundError |
| 26 | +except json.JSONDecodeError: |
| 27 | + print('Error: Failed to decode JSON from the file.') |
| 28 | + raise SystemExit(2) from json.JSONDecodeError |
| 29 | +except urllib.error.URLError: |
| 30 | + print('Failed to fetch tool schema.') |
| 31 | + raise SystemExit(3) from urllib.error.URLError |
| 32 | + |
| 33 | +try: |
| 34 | + jsonschema.validate(instance=tool_data, schema=schema) |
| 35 | +except jsonschema.ValidationError: |
| 36 | + print('Tool data is invalid according to the schema.') |
| 37 | + raise SystemExit(10) from jsonschema.ValidationError |
| 38 | + |
| 39 | +# If we get here the tool data has successfully been validated. |
| 40 | +raise SystemExit(0) |
0 commit comments