9
9
from dataclasses import dataclass
10
10
from pathlib import Path , PurePath
11
11
12
- import pykwalify . core
12
+ import jsonschema
13
13
import yaml
14
+ from jsonschema .exceptions import best_match
14
15
15
16
try :
16
17
from yaml import CSafeLoader as SafeLoader
17
18
except ImportError :
18
19
from yaml import SafeLoader
19
20
20
21
21
- SOC_SCHEMA_PATH = str (Path (__file__ ).parent / 'schemas' / 'soc-schema.yml ' )
22
+ SOC_SCHEMA_PATH = str (Path (__file__ ).parent / 'schemas' / 'soc-schema.yaml ' )
22
23
with open (SOC_SCHEMA_PATH ) as f :
23
24
soc_schema = yaml .load (f .read (), Loader = SafeLoader )
24
25
25
- SOC_VALIDATOR = pykwalify .core .Core (schema_data = soc_schema , source_data = {})
26
-
27
- ARCH_SCHEMA_PATH = str (Path (__file__ ).parent / 'schemas' / 'arch-schema.yml' )
26
+ ARCH_SCHEMA_PATH = str (Path (__file__ ).parent / 'schemas' / 'arch-schema.yaml' )
28
27
with open (ARCH_SCHEMA_PATH ) as f :
29
28
arch_schema = yaml .load (f .read (), Loader = SafeLoader )
30
29
31
- ARCH_VALIDATOR = pykwalify .core .Core (schema_data = arch_schema , source_data = {})
30
+ validator_class = jsonschema .validators .validator_for (soc_schema )
31
+ validator_class .check_schema (soc_schema )
32
+ soc_validator = validator_class (soc_schema )
33
+
34
+ validator_class = jsonschema .validators .validator_for (arch_schema )
35
+ validator_class .check_schema (arch_schema )
36
+ arch_validator = validator_class (arch_schema )
32
37
33
38
SOC_YML = 'soc.yml'
34
39
ARCHS_YML_PATH = PurePath ('arch/archs.yml' )
@@ -44,12 +49,12 @@ def __init__(self, folder='', soc_yaml=None):
44
49
if soc_yaml is None :
45
50
return
46
51
47
- try :
48
- data = yaml . load ( soc_yaml , Loader = SafeLoader )
49
- SOC_VALIDATOR . source = data
50
- SOC_VALIDATOR . validate ()
51
- except ( yaml . YAMLError , pykwalify . errors . SchemaError ) as e :
52
- sys . exit ( f'ERROR: Malformed yaml { soc_yaml . as_posix () } ' , e )
52
+ data = yaml . load ( soc_yaml , Loader = SafeLoader )
53
+ errors = list ( soc_validator . iter_errors ( data ) )
54
+ if errors :
55
+ sys . exit ( 'ERROR: Malformed soc YAML file: \n '
56
+ f' { soc_yaml } \n '
57
+ f' { best_match ( errors ). message } in { best_match ( errors ). json_path } ' )
53
58
54
59
for f in data .get ('family' , []):
55
60
family = Family (f ['name' ], [folder ], [], [])
@@ -82,10 +87,6 @@ def __init__(self, folder='', soc_yaml=None):
82
87
self ._socs .extend (socs )
83
88
84
89
for soc in data .get ('socs' , []):
85
- mutual_exclusive = {'name' , 'extend' }
86
- if len (mutual_exclusive - soc .keys ()) < 1 :
87
- sys .exit (f'ERROR: Malformed content in SoC file: { soc_yaml } \n '
88
- f'{ mutual_exclusive } are mutual exclusive at this level.' )
89
90
if soc .get ('name' ) is not None :
90
91
self ._socs .append (Soc (soc ['name' ], [c ['name' ] for c in soc .get ('cpuclusters' , [])],
91
92
[folder ], '' , '' ))
@@ -94,8 +95,9 @@ def __init__(self, folder='', soc_yaml=None):
94
95
[c ['name' ] for c in soc .get ('cpuclusters' , [])],
95
96
[folder ], '' , '' ))
96
97
else :
98
+ # This should not happen if schema validation passed
97
99
sys .exit (f'ERROR: Malformed "socs" section in SoC file: { soc_yaml } \n '
98
- f'Cannot find one of required keys { mutual_exclusive } .' )
100
+ f'SoC entry must have either "name" or "extend" property .' )
99
101
100
102
# Ensure that any runner configuration matches socs and cpuclusters declared in the same
101
103
# soc.yml file
@@ -217,11 +219,11 @@ def find_v2_archs(args):
217
219
with Path (archs_yml ).open ('r' , encoding = 'utf-8' ) as f :
218
220
archs = yaml .load (f .read (), Loader = SafeLoader )
219
221
220
- try :
221
- ARCH_VALIDATOR . source = archs
222
- ARCH_VALIDATOR . validate ()
223
- except pykwalify . errors . SchemaError as e :
224
- sys . exit ( f'ERROR: Malformed "build" section in file: { archs_yml . as_posix () } \n { e } ' )
222
+ errors = list ( arch_validator . iter_errors ( archs ))
223
+ if errors :
224
+ sys . exit ( 'ERROR: Malformed arch YAML file: '
225
+ f' { archs_yml . as_posix () } \n '
226
+ f' { best_match ( errors ). message } in { best_match ( errors ). json_path } ' )
225
227
226
228
if args .arch is not None :
227
229
archs = {'archs' : list (filter (
0 commit comments