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