Skip to content

Commit

Permalink
dt-validate: Support multiple schema id matches
Browse files Browse the repository at this point in the history
The schema validation in the kernel has supported providing multiple schema
substrings to limit validation to matching schemas, but no one has noticed
that it didn't work for dt-validate (only validation of schemas themselves).
Add the necessary support to support multiple matches in dt-validate.

Signed-off-by: Rob Herring (Arm) <[email protected]>
  • Loading branch information
robherring committed Aug 15, 2024
1 parent 449b457 commit 5270b95
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
14 changes: 9 additions & 5 deletions dtschema/dtb_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ def main():
help="Filename or directory of devicetree DTB input file(s)")
ap.add_argument('-s', '--schema', help="preparsed schema file or path to schema files")
ap.add_argument('-p', '--preparse', help="preparsed schema file (deprecated, use '-s')")
ap.add_argument('-l', '--limit', help="limit validation to schemas with $id matching LIMIT substring")
ap.add_argument('-l', '--limit', help="limit validation to schemas with $id matching LIMIT substring. " \
"Multiple substrings separated by ':' can be listed (e.g. foo:bar:baz).")
ap.add_argument('-c', '--compatible-match', action="store_true",
help="limit validation to schema matching nodes' most specific compatible string")
ap.add_argument('-m', '--show-unmatched',
Expand All @@ -118,14 +119,17 @@ def main():

verbose = args.verbose
show_unmatched = args.show_unmatched
match_schema_file = args.limit
if args.limit:
match_schema_file = args.limit.split(':')
compatible_match = args.compatible_match

# Maintain prior behaviour which accepted file paths by stripping the file path
if args.url_path and args.limit:
for d in args.url_path.split(os.path.sep):
if d and match_schema_file.startswith(d):
match_schema_file = match_schema_file[(len(d) + 1):]
for i,match in enumerate(match_schema_file):
for d in args.url_path.split(os.path.sep):
if d and match.startswith(d):
match = match[(len(d) + 1):]
match_schema_file[i] = match

if args.preparse:
sg = schema_group(args.preparse)
Expand Down
12 changes: 10 additions & 2 deletions dtschema/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,12 +416,20 @@ def annotate_error(self, id, error):
error.linecol = -1, -1
error.note = None

def _filter_match(self, schema_id, filter):
if not filter:
return True
for f in filter:
if f in schema_id:
return True
return False

def iter_errors(self, instance, filter=None, compatible_match=False):
if 'compatible' in instance:
for inst_compat in instance['compatible']:
if inst_compat in self.compat_map:
schema_id = self.compat_map[inst_compat]
if not filter or filter in schema_id:
if self._filter_match(schema_id, filter):
schema = self.schemas[schema_id]
for error in self.DtValidator(schema,
resolver=self.resolver,
Expand All @@ -434,7 +442,7 @@ def iter_errors(self, instance, filter=None, compatible_match=False):
return

for schema_id in self.always_schemas:
if filter and filter not in schema_id:
if not self._filter_match(schema_id, filter):
continue
schema = {'if': self.schemas[schema_id]['select'],
'then': self.schemas[schema_id]}
Expand Down

0 comments on commit 5270b95

Please sign in to comment.