forked from petejohanson/zmk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: make west scripts more pythonic and apply Black
- Loading branch information
1 parent
e0620f1
commit f767abe
Showing
3 changed files
with
50 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,6 @@ | |
"files.associations": { | ||
"*.overlay": "dts", | ||
"*.keymap": "dts" | ||
} | ||
}, | ||
"python.formatting.provider": "black" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,64 @@ | ||
# Copyright (c) 2021 The ZMK Contributors | ||
# SPDX-License-Identifier: MIT | ||
'''Metadata command for ZMK.''' | ||
"""Metadata command for ZMK.""" | ||
|
||
from functools import cached_property | ||
import glob | ||
import json | ||
from jsonschema import validate, ValidationError | ||
import os | ||
import jsonschema | ||
import sys | ||
import yaml | ||
from textwrap import dedent # just for nicer code indentation | ||
|
||
from west.commands import WestCommand | ||
from west import log # use this for user output | ||
from west import log # use this for user output | ||
|
||
|
||
class Metadata(WestCommand): | ||
def __init__(self): | ||
super().__init__( | ||
'metadata', # gets stored as self.name | ||
'ZMK hardware metadata commands', # self.help | ||
# self.description: | ||
dedent('''Operate on the board/shield metadata.''')) | ||
name="metadata", | ||
help="ZMK hardware metadata commands", | ||
description="Operate on the board/shield metadata.", | ||
) | ||
|
||
def do_add_parser(self, parser_adder): | ||
parser = parser_adder.add_parser(self.name, | ||
help=self.help, | ||
description=self.description) | ||
parser = parser_adder.add_parser( | ||
self.name, help=self.help, description=self.description | ||
) | ||
|
||
parser.add_argument('subcommand', default="check", | ||
help='The subcommand to run. Defaults to "check".', nargs="?") | ||
return parser # gets stored as self.parser | ||
parser.add_argument( | ||
"subcommand", | ||
default="check", | ||
help='The subcommand to run. Defaults to "check".', | ||
nargs="?", | ||
) | ||
return parser # gets stored as self.parser | ||
|
||
@cached_property | ||
def schema(self): | ||
return json.load( | ||
open("../schema/hardware-metadata.schema.json", 'r')) | ||
return json.load(open("../schema/hardware-metadata.schema.json", "r")) | ||
|
||
def validate_file(self, file): | ||
print("Validating: " + file) | ||
with open(file, 'r') as stream: | ||
with open(file, "r") as stream: | ||
try: | ||
validate(yaml.safe_load(stream), self.schema) | ||
jsonschema.validate(yaml.safe_load(stream), self.schema) | ||
except yaml.YAMLError as exc: | ||
print("Failed loading metadata yaml: " + file) | ||
print(exc) | ||
return False | ||
except ValidationError as vexc: | ||
except jsonschema.ValidationError as vexc: | ||
print("Failed validation of: " + file) | ||
print(vexc) | ||
return False | ||
return True | ||
|
||
def do_run(self, args, unknown_args): | ||
status = all([self.validate_file(f) for f in glob.glob( | ||
"boards/**/*.zmk.yml", recursive=True)]) | ||
status = all( | ||
[ | ||
self.validate_file(f) | ||
for f in glob.glob("boards/**/*.zmk.yml", recursive=True) | ||
] | ||
) | ||
|
||
sys.exit(0 if status else 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,41 @@ | ||
# Copyright (c) 2020 The ZMK Contributors | ||
# SPDX-License-Identifier: MIT | ||
'''Test runner for ZMK.''' | ||
"""Test runner for ZMK.""" | ||
|
||
import os | ||
import subprocess | ||
from textwrap import dedent # just for nicer code indentation | ||
|
||
from west.commands import WestCommand | ||
from west import log # use this for user output | ||
from west import log # use this for user output | ||
|
||
|
||
class Test(WestCommand): | ||
def __init__(self): | ||
super().__init__( | ||
'test', # gets stored as self.name | ||
'run ZMK testsuite', # self.help | ||
# self.description: | ||
dedent('''Run the ZMK testsuite.''')) | ||
name="test", | ||
help="run ZMK testsuite", | ||
description="Run the ZMK testsuite.", | ||
) | ||
|
||
def do_add_parser(self, parser_adder): | ||
parser = parser_adder.add_parser(self.name, | ||
help=self.help, | ||
description=self.description) | ||
parser = parser_adder.add_parser( | ||
self.name, | ||
help=self.help, | ||
description=self.description, | ||
) | ||
|
||
parser.add_argument('test_path', default="all", | ||
help='The path to the test. Defaults to "all".', nargs="?") | ||
return parser # gets stored as self.parser | ||
parser.add_argument( | ||
"test_path", | ||
default="all", | ||
help='The path to the test. Defaults to "all".', | ||
nargs="?", | ||
) | ||
return parser | ||
|
||
def do_run(self, args, unknown_args): | ||
# the run-test script assumes the app directory is the current dir. | ||
os.chdir(f'{self.topdir}/app') | ||
os.chdir(f"{self.topdir}/app") | ||
completed_process = subprocess.run( | ||
[f'{self.topdir}/app/run-test.sh', args.test_path]) | ||
[f"{self.topdir}/app/run-test.sh", args.test_path] | ||
) | ||
exit(completed_process.returncode) |