Skip to content

Commit e9f106d

Browse files
gudnimgMaureenHelm
authored andcommitted
twister: replace pykwalify with jsonschema
Usage of pykwalify is deprecated. Signed-off-by: Guðni Már Gilbert <gudni.m.g@gmail.com>
1 parent fdca758 commit e9f106d

File tree

8 files changed

+549
-770
lines changed

8 files changed

+549
-770
lines changed

‎scripts/pylib/twister/scl.py‎

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
1-
#! /usr/bin/python
1+
#!/usr/bin/env python3
22
#
33
# SPDX-License-Identifier: Apache-2.0
4+
# SPDX-FileCopyrightText: Copyright The Zephyr Project Contributors
45
# Zephyr's Twister library
56
#
6-
# pylint: disable=unused-import
7-
#
87
# Set of code that other projects can also import to do things on
98
# Zephyr's sanity check testcases.
109

1110
import logging
1211
import yaml
1312
try:
14-
# Use the C LibYAML parser if available, rather than the Python parser.
15-
# It's much faster.
16-
from yaml import CLoader as Loader
1713
from yaml import CSafeLoader as SafeLoader
18-
from yaml import CDumper as Dumper
1914
except ImportError:
20-
from yaml import Loader, SafeLoader, Dumper
15+
from yaml import SafeLoader
16+
17+
from jsonschema import Draft202012Validator
2118

2219
log = logging.getLogger("scl")
2320

@@ -26,8 +23,6 @@ class EmptyYamlFileException(Exception):
2623
pass
2724

2825

29-
#
30-
#
3126
def yaml_load(filename):
3227
"""
3328
Safely load a YAML document
@@ -51,23 +46,23 @@ def yaml_load(filename):
5146
e.note, cmark.name, cmark.line, cmark.column, e.context)
5247
raise
5348

54-
# If pykwalify is installed, then the validate function will work --
55-
# otherwise, it is a stub and we'd warn about it.
56-
try:
57-
import pykwalify.core
58-
# Don't print error messages yourself, let us do it
59-
logging.getLogger("pykwalify.core").setLevel(50)
49+
def _yaml_validate(data, schema):
50+
"""
51+
Validate loaded YAML data against a JSON Schema.
52+
53+
:param dict data: YAML document data
54+
:param dict schema: JSON Schema (already loaded)
55+
:raises jsonschema.exceptions.ValidationError: on schema violation
56+
:raises jsonschema.exceptions.SchemaError: on invalid schema
57+
"""
58+
if not schema:
59+
return
6060

61-
def _yaml_validate(data, schema):
62-
if not schema:
63-
return
64-
c = pykwalify.core.Core(source_data=data, schema_data=schema)
65-
c.validate(raise_exception=True)
61+
validator = Draft202012Validator(schema)
6662

67-
except ImportError as e:
68-
log.warning("can't import pykwalify; won't validate YAML (%s)", e)
69-
def _yaml_validate(data, schema):
70-
pass
63+
# Fail fast on first error
64+
for error in validator.iter_errors(data):
65+
raise error
7166

7267
def yaml_load_verify(filename, schema):
7368
"""
@@ -77,9 +72,9 @@ def yaml_load_verify(filename, schema):
7772
:param str filename: name of the file to load and process
7873
:param dict schema: loaded YAML schema (can load with :func:`yaml_load`)
7974
80-
# 'document.yaml' contains a single YAML document.
8175
:raises yaml.scanner.ScannerError: on YAML parsing error
82-
:raises pykwalify.errors.SchemaError: on Schema violation error
76+
:raises jsonschema.exceptions.ValidationError: on schema violation
77+
:raises jsonschema.exceptions.SchemaError: on Schema violation error
8378
"""
8479
# 'document.yaml' contains a single YAML document.
8580
y = yaml_load(filename)
Lines changed: 65 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,65 @@
1-
type: seq
2-
sequence:
3-
- type: map
4-
required: false
5-
mapping:
6-
"available":
7-
type: bool
8-
required: false
9-
"connected":
10-
type: bool
11-
required: true
12-
"id":
13-
type: str
14-
required: true
15-
"notes":
16-
type: str
17-
required: false
18-
"platform":
19-
type: any
20-
required: true
21-
"probe_id":
22-
type: str
23-
required: false
24-
"product":
25-
type: str
26-
required: true
27-
"runner":
28-
type: str
29-
required: true
30-
"runner_params":
31-
type: seq
32-
required: false
33-
sequence:
34-
- type: str
35-
"serial_pty":
36-
type: str
37-
required: false
38-
"serial":
39-
type: str
40-
required: false
41-
"baud":
42-
type: int
43-
required: false
44-
"serial_baud":
45-
type: int
46-
required: false
47-
"post_script":
48-
type: str
49-
required: false
50-
"post_flash_script":
51-
type: str
52-
required: false
53-
"pre_script":
54-
type: str
55-
required: false
56-
"fixtures":
57-
type: seq
58-
required: false
59-
sequence:
60-
- type: str
61-
"flash_timeout":
62-
type: int
63-
required: false
64-
"flash_with_test":
65-
type: bool
66-
required: false
67-
"flash_before":
68-
type: bool
69-
required: false
70-
"script_param":
71-
type: map
72-
required: false
73-
mapping:
74-
"pre_script_timeout":
75-
type: int
76-
required: false
77-
"post_script_timeout":
78-
type: int
79-
required: false
80-
"post_flash_timeout":
81-
type: int
82-
required: false
1+
$schema: "https://json-schema.org/draft/2020-12/schema"
2+
type: array
3+
items:
4+
type: object
5+
properties:
6+
available:
7+
type: boolean
8+
connected:
9+
type: boolean
10+
id:
11+
type: string
12+
notes:
13+
type: string
14+
platform: {}
15+
probe_id:
16+
type: string
17+
product:
18+
type: string
19+
runner:
20+
type: string
21+
runner_params:
22+
type: array
23+
items:
24+
type: string
25+
serial_pty:
26+
type: string
27+
serial:
28+
type: string
29+
baud:
30+
type: integer
31+
serial_baud:
32+
type: integer
33+
post_script:
34+
type: string
35+
post_flash_script:
36+
type: string
37+
pre_script:
38+
type: string
39+
fixtures:
40+
type: array
41+
items:
42+
type: string
43+
flash_timeout:
44+
type: integer
45+
flash_with_test:
46+
type: boolean
47+
flash_before:
48+
type: boolean
49+
script_param:
50+
type: object
51+
properties:
52+
pre_script_timeout:
53+
type: integer
54+
post_script_timeout:
55+
type: integer
56+
post_flash_timeout:
57+
type: integer
58+
additionalProperties: false
59+
required:
60+
- connected
61+
- id
62+
- platform
63+
- product
64+
- runner
65+
additionalProperties: false

0 commit comments

Comments
 (0)