Skip to content

Commit 39fad82

Browse files
committed
wip
Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
1 parent 6dff513 commit 39fad82

File tree

8 files changed

+544
-679
lines changed

8 files changed

+544
-679
lines changed

scripts/pylib/twister/scl.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88
# Set of code that other projects can also import to do things on
99
# Zephyr's sanity check testcases.
1010

11+
import jsonschema
1112
import logging
1213
import yaml
14+
15+
from jsonschema.exceptions import best_match
16+
1317
try:
1418
# Use the C LibYAML parser if available, rather than the Python parser.
1519
# It's much faster.
@@ -20,7 +24,8 @@
2024
from yaml import Loader, SafeLoader, Dumper
2125

2226
log = logging.getLogger("scl")
23-
27+
# Cache for validators to avoid recreating them for the same JSON schema
28+
_validator_cache = {}
2429

2530
class EmptyYamlFileException(Exception):
2631
pass
@@ -51,23 +56,21 @@ def yaml_load(filename):
5156
e.note, cmark.name, cmark.line, cmark.column, e.context)
5257
raise
5358

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)
59+
def _yaml_validate(data, schema):
60+
if not schema:
61+
return
6062

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)
63+
schema_key = id(schema)
64+
if schema_key not in _validator_cache:
65+
validator_class = jsonschema.validators.validator_for(schema)
66+
validator_class.check_schema(schema)
67+
_validator_cache[schema_key] = validator_class(schema)
6668

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
69+
validator = _validator_cache[schema_key]
70+
errors = list(validator.iter_errors(data))
71+
if errors:
72+
err = best_match(errors)
73+
raise jsonschema.ValidationError(f"Error: {err.message} in {err.json_path}")
7174

7275
def yaml_load_verify(filename, schema):
7376
"""
@@ -79,7 +82,7 @@ def yaml_load_verify(filename, schema):
7982
8083
# 'document.yaml' contains a single YAML document.
8184
:raises yaml.scanner.ScannerError: on YAML parsing error
82-
:raises pykwalify.errors.SchemaError: on Schema violation error
85+
:raises jsonschema.exceptions.ValidationError: on Schema violation error
8386
"""
8487
# 'document.yaml' contains a single YAML document.
8588
y = yaml_load(filename)
Lines changed: 60 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,60 @@
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-
"post_script":
45-
type: str
46-
required: false
47-
"post_flash_script":
48-
type: str
49-
required: false
50-
"pre_script":
51-
type: str
52-
required: false
53-
"fixtures":
54-
type: seq
55-
required: false
56-
sequence:
57-
- type: str
58-
"flash_timeout":
59-
type: int
60-
required: false
61-
"flash_with_test":
62-
type: bool
63-
required: false
64-
"flash_before":
65-
type: bool
66-
required: false
67-
"script_param":
68-
type: map
69-
required: false
70-
mapping:
71-
"pre_script_timeout":
72-
type: int
73-
required: false
74-
"post_script_timeout":
75-
type: int
76-
required: false
77-
"post_flash_timeout":
78-
type: int
79-
required: false
1+
$schema: "https://json-schema.org/draft/2020-12/schema"
2+
$id: "https://zephyrproject.org/schemas/twister/hwmap-schema"
3+
title: "Zephyr Hardware Map Schema"
4+
description: "Schema to validate a YAML file describing Zephyr hardware mapping"
5+
type: "array"
6+
items:
7+
type: "object"
8+
required: ["connected", "id", "platform", "product", "runner"]
9+
properties:
10+
available:
11+
type: "boolean"
12+
connected:
13+
type: "boolean"
14+
id:
15+
type: "string"
16+
notes:
17+
type: "string"
18+
platform:
19+
type: "string"
20+
probe_id:
21+
type: "string"
22+
product:
23+
type: "string"
24+
runner:
25+
type: "string"
26+
runner_params:
27+
type: "array"
28+
items:
29+
type: "string"
30+
serial_pty:
31+
type: "string"
32+
serial:
33+
type: "string"
34+
baud:
35+
type: "integer"
36+
post_script:
37+
type: "string"
38+
post_flash_script:
39+
type: "string"
40+
pre_script:
41+
type: "string"
42+
fixtures:
43+
type: "array"
44+
items:
45+
type: "string"
46+
flash_timeout:
47+
type: "integer"
48+
flash_with_test:
49+
type: "boolean"
50+
flash_before:
51+
type: "boolean"
52+
script_param:
53+
type: "object"
54+
properties:
55+
pre_script_timeout:
56+
type: "integer"
57+
post_script_timeout:
58+
type: "integer"
59+
post_flash_timeout:
60+
type: "integer"

0 commit comments

Comments
 (0)