|
|
@ -1,15 +1,13 @@ |
|
|
|
# Copyright (c) 2021 The ZMK Contributors |
|
|
|
# Copyright (c) 2021 The ZMK Contributors |
|
|
|
# SPDX-License-Identifier: MIT |
|
|
|
# SPDX-License-Identifier: MIT |
|
|
|
'''Metadata command for ZMK.''' |
|
|
|
"""Metadata command for ZMK.""" |
|
|
|
|
|
|
|
|
|
|
|
from functools import cached_property |
|
|
|
from functools import cached_property |
|
|
|
import glob |
|
|
|
import glob |
|
|
|
import json |
|
|
|
import json |
|
|
|
from jsonschema import validate, ValidationError |
|
|
|
import jsonschema |
|
|
|
import os |
|
|
|
|
|
|
|
import sys |
|
|
|
import sys |
|
|
|
import yaml |
|
|
|
import yaml |
|
|
|
from textwrap import dedent # just for nicer code indentation |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from west.commands import WestCommand |
|
|
|
from west.commands import WestCommand |
|
|
|
from west import log # use this for user output |
|
|
|
from west import log # use this for user output |
|
|
@ -18,42 +16,49 @@ from west import log # use this for user output |
|
|
|
class Metadata(WestCommand): |
|
|
|
class Metadata(WestCommand): |
|
|
|
def __init__(self): |
|
|
|
def __init__(self): |
|
|
|
super().__init__( |
|
|
|
super().__init__( |
|
|
|
'metadata', # gets stored as self.name |
|
|
|
name="metadata", |
|
|
|
'ZMK hardware metadata commands', # self.help |
|
|
|
help="ZMK hardware metadata commands", |
|
|
|
# self.description: |
|
|
|
description="Operate on the board/shield metadata.", |
|
|
|
dedent('''Operate on the board/shield metadata.''')) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
def do_add_parser(self, parser_adder): |
|
|
|
def do_add_parser(self, parser_adder): |
|
|
|
parser = parser_adder.add_parser(self.name, |
|
|
|
parser = parser_adder.add_parser( |
|
|
|
help=self.help, |
|
|
|
self.name, help=self.help, description=self.description |
|
|
|
description=self.description) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
parser.add_argument('subcommand', default="check", |
|
|
|
parser.add_argument( |
|
|
|
help='The subcommand to run. Defaults to "check".', nargs="?") |
|
|
|
"subcommand", |
|
|
|
|
|
|
|
default="check", |
|
|
|
|
|
|
|
help='The subcommand to run. Defaults to "check".', |
|
|
|
|
|
|
|
nargs="?", |
|
|
|
|
|
|
|
) |
|
|
|
return parser # gets stored as self.parser |
|
|
|
return parser # gets stored as self.parser |
|
|
|
|
|
|
|
|
|
|
|
@cached_property |
|
|
|
@cached_property |
|
|
|
def schema(self): |
|
|
|
def schema(self): |
|
|
|
return json.load( |
|
|
|
return json.load(open("../schema/hardware-metadata.schema.json", "r")) |
|
|
|
open("../schema/hardware-metadata.schema.json", 'r')) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def validate_file(self, file): |
|
|
|
def validate_file(self, file): |
|
|
|
print("Validating: " + file) |
|
|
|
print("Validating: " + file) |
|
|
|
with open(file, 'r') as stream: |
|
|
|
with open(file, "r") as stream: |
|
|
|
try: |
|
|
|
try: |
|
|
|
validate(yaml.safe_load(stream), self.schema) |
|
|
|
jsonschema.validate(yaml.safe_load(stream), self.schema) |
|
|
|
except yaml.YAMLError as exc: |
|
|
|
except yaml.YAMLError as exc: |
|
|
|
print("Failed loading metadata yaml: " + file) |
|
|
|
print("Failed loading metadata yaml: " + file) |
|
|
|
print(exc) |
|
|
|
print(exc) |
|
|
|
return False |
|
|
|
return False |
|
|
|
except ValidationError as vexc: |
|
|
|
except jsonschema.ValidationError as vexc: |
|
|
|
print("Failed validation of: " + file) |
|
|
|
print("Failed validation of: " + file) |
|
|
|
print(vexc) |
|
|
|
print(vexc) |
|
|
|
return False |
|
|
|
return False |
|
|
|
return True |
|
|
|
return True |
|
|
|
|
|
|
|
|
|
|
|
def do_run(self, args, unknown_args): |
|
|
|
def do_run(self, args, unknown_args): |
|
|
|
status = all([self.validate_file(f) for f in glob.glob( |
|
|
|
status = all( |
|
|
|
"boards/**/*.zmk.yml", recursive=True)]) |
|
|
|
[ |
|
|
|
|
|
|
|
self.validate_file(f) |
|
|
|
|
|
|
|
for f in glob.glob("boards/**/*.zmk.yml", recursive=True) |
|
|
|
|
|
|
|
] |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
sys.exit(0 if status else 1) |
|
|
|
sys.exit(0 if status else 1) |
|
|
|