Scott Johnson | d16af11 | 2019-10-17 15:03:58 -0700 | [diff] [blame] | 1 | # Copyright lowRISC contributors. |
| 2 | # Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | """ |
Philipp Wagner | 14a3fee | 2019-11-21 10:07:02 +0000 | [diff] [blame] | 5 | Dashboard project JSON file validation |
Scott Johnson | d16af11 | 2019-10-17 15:03:58 -0700 | [diff] [blame] | 6 | """ |
| 7 | |
| 8 | import logging as log |
| 9 | import sys |
| 10 | |
| 11 | |
| 12 | def check_keys(obj, required_keys, optional_keys, err_prefix): |
| 13 | error = 0 |
| 14 | for x in required_keys: |
| 15 | if not x in obj: |
| 16 | error += 1 |
| 17 | log.error(err_prefix + " missing required key " + x) |
| 18 | for x in obj: |
| 19 | type = '' |
| 20 | if x in required_keys: |
| 21 | type = required_keys[x][0] |
| 22 | elif x in optional_keys: |
| 23 | type = optional_keys[x][0] |
| 24 | else: |
Rasmus Madsen | ef68b81 | 2020-12-09 17:37:36 +0100 | [diff] [blame] | 25 | log.warning(err_prefix + " contains extra key rmn " + x) |
| 26 | # log.warning('{} contains extra key {!r}'.format(err_prefix, x)) |
Scott Johnson | d16af11 | 2019-10-17 15:03:58 -0700 | [diff] [blame] | 27 | |
| 28 | return error |
| 29 | |
| 30 | |
| 31 | field_required = { |
| 32 | 'name': ['s', "module name"], |
| 33 | 'version': ['s', "module version"], |
| 34 | 'life_stage': ['s', "life stage of module"] |
| 35 | } |
| 36 | field_optional = { |
Srikrishna Iyer | d1f896e | 2020-03-05 13:52:40 -0800 | [diff] [blame] | 37 | 'design_spec': |
| 38 | ['s', "path to the design specification, relative to repo root"], |
Srikrishna Iyer | 01c56b4 | 2021-08-02 12:50:11 -0700 | [diff] [blame] | 39 | 'dv_doc': ['s', "path to the DV document, relative to repo root"], |
Srikrishna Iyer | 84dac53 | 2020-04-02 21:45:21 -0700 | [diff] [blame] | 40 | 'hw_checklist': ['s', "path to the hw_checklist, relative to repo root"], |
Sam Elliott | 108420c | 2020-04-20 19:56:54 +0100 | [diff] [blame] | 41 | 'sw_checklist': ['s', "path to the sw_checklist, relative to repo root"], |
Scott Johnson | d16af11 | 2019-10-17 15:03:58 -0700 | [diff] [blame] | 42 | 'design_stage': ['s', "design stage of module"], |
Sam Elliott | 108420c | 2020-04-20 19:56:54 +0100 | [diff] [blame] | 43 | 'dif_stage': ['s', 'DIF stage of module'], |
Scott Johnson | d16af11 | 2019-10-17 15:03:58 -0700 | [diff] [blame] | 44 | 'verification_stage': ['s', "verification stage of module"], |
| 45 | 'notes': ['s', "random notes"], |
| 46 | } |
| 47 | |
Eunchan Kim | f3039d7 | 2019-11-18 08:50:26 -0800 | [diff] [blame] | 48 | entry_required = { |
| 49 | 'version': ['s', "module version"], |
| 50 | 'life_stage': ['s', "life stage of module"] |
| 51 | } |
| 52 | entry_optional = { |
| 53 | 'design_stage': ['s', "design stage of module"], |
| 54 | 'verification_stage': ['s', "verification stage of module"], |
Sam Elliott | 108420c | 2020-04-20 19:56:54 +0100 | [diff] [blame] | 55 | 'dif_stage': ['s', 'DIF stage of module'], |
Eunchan Kim | f3039d7 | 2019-11-18 08:50:26 -0800 | [diff] [blame] | 56 | 'commit_id': ['s', "Staged commit ID"], |
| 57 | 'notes': ['s', "notes"], |
| 58 | } |
| 59 | |
Scott Johnson | d16af11 | 2019-10-17 15:03:58 -0700 | [diff] [blame] | 60 | |
| 61 | def validate(regs): |
| 62 | if not 'name' in regs: |
| 63 | log.error("Component has no name. Aborting.") |
| 64 | return 1 |
| 65 | component = regs['name'] |
| 66 | |
Eunchan Kim | f3039d7 | 2019-11-18 08:50:26 -0800 | [diff] [blame] | 67 | # If `revisions` is not in the object keys, the tool runs previous |
| 68 | # version checker, which has only one version entry. |
| 69 | if not "revisions" in regs: |
| 70 | error = check_keys(regs, field_required, field_optional, component) |
| 71 | if (error > 0): |
| 72 | log.error("Component has top level errors. Aborting.") |
| 73 | return error |
| 74 | |
| 75 | # Assumes `revisions` field exists in the Hjson object. |
| 76 | # It iterates the entries in the `revisions` group. |
| 77 | error = 0 |
| 78 | if not isinstance(regs['revisions'], list): |
| 79 | error += 1 |
| 80 | log.error("`revisions` field should be a list of version entries") |
| 81 | return error |
| 82 | |
| 83 | for rev in regs['revisions']: |
| 84 | error += check_keys(rev, entry_required, entry_optional, component) |
| 85 | |
Scott Johnson | d16af11 | 2019-10-17 15:03:58 -0700 | [diff] [blame] | 86 | if (error > 0): |
Eunchan Kim | f3039d7 | 2019-11-18 08:50:26 -0800 | [diff] [blame] | 87 | log.error("Component has errors in revision field. Aborting.") |
Scott Johnson | d16af11 | 2019-10-17 15:03:58 -0700 | [diff] [blame] | 88 | |
| 89 | return error |