Commit a615696c authored by Rémi Duraffort's avatar Rémi Duraffort Committed by stevanradakovic

lava-schema: factorize and allow to check device config

Signed-off-by: default avatarRémi Duraffort <remi.duraffort@linaro.org>
parent 0c68b065
...@@ -26,46 +26,64 @@ import sys ...@@ -26,46 +26,64 @@ import sys
import voluptuous as v import voluptuous as v
import yaml import yaml
from lava_common.schemas import validate from lava_common.schemas import validate as validate_job
from lava_common.schemas.device import validate as validate_device
def check_device(data, options, prefix=""):
try:
data = yaml.safe_load(data)
except yaml.yamlerror as exc:
print("%sinvalid device definition:" % prefix)
print("%sinvalid yaml" % prefix)
return 1
try:
validate_device(data)
except v.Invalid as exc:
print("%sinvalid device definition:" % prefix)
print("%skey: %s" % (prefix, exc.path))
print("%smgs: %s" % (prefix, exc.msg))
return 1
return 0
def check_job(data, options, prefix=""): def check_job(data, options, prefix=""):
try: try:
data = yaml.safe_load(data) data = yaml.safe_load(data)
except yaml.YAMLError as exc: except yaml.yamlerror as exc:
print("%sInvalid job definition:" % prefix) print("%sinvalid job definition:" % prefix)
print("%sinvalid yaml" % prefix) print("%sinvalid yaml" % prefix)
return 1 return 1
try: try:
validate(data, options.strict) validate_job(data, options.strict)
except v.Invalid as exc: except v.Invalid as exc:
print("%sInvalid job definition:" % prefix) print("%sinvalid job definition:" % prefix)
print("%skey: %s" % (prefix, exc.path)) print("%skey: %s" % (prefix, exc.path))
print("%smgs: %s" % (prefix, exc.msg)) print("%smgs: %s" % (prefix, exc.msg))
return 1 return 1
return 0 return 0
def handle_job(options): def handle(options, files, check):
failed = 0 failed = 0
for jobfile in options.jobs: for fileobj in files:
if jobfile.is_dir() and options.recursive: if fileobj.is_dir() and options.recursive:
job_iter = jobfile.rglob("*.yaml") files_iter = fileobj.rglob("*.yaml")
else: else:
job_iter = [jobfile] files_iter = [fileobj]
for job in job_iter: for f in files_iter:
if not job.as_posix() == "-" and not job.is_file(): if not f.as_posix() == "-" and not f.is_file():
continue continue
if job.name in options.exclude: if f.name in options.exclude:
print("* %s [skip]" % str(job)) print("* %s [skip]" % str(f))
continue continue
if job.as_posix() == "-": if f.as_posix() == "-":
print("* stdin") print("* stdin")
data = sys.stdin.read() data = sys.stdin.read()
else: else:
print("* %s" % str(job)) print("* %s" % str(f))
data = job.read_text(encoding="utf-8") data = f.read_text(encoding="utf-8")
if check_job(data, options, prefix=" -> "): if check(data, options, prefix=" -> "):
failed += 1 failed += 1
return failed return failed
...@@ -75,6 +93,8 @@ def main(): ...@@ -75,6 +93,8 @@ def main():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
sub = parser.add_subparsers(dest="sub_command") sub = parser.add_subparsers(dest="sub_command")
sub.required = True sub.required = True
# "job"
job_parser = sub.add_parser("job", help="check job schema") job_parser = sub.add_parser("job", help="check job schema")
job_parser.add_argument("jobs", type=pathlib.Path, nargs="+", help="job definition") job_parser.add_argument("jobs", type=pathlib.Path, nargs="+", help="job definition")
...@@ -92,10 +112,29 @@ def main(): ...@@ -92,10 +112,29 @@ def main():
"--strict", action="store_true", default=False, help="make the validator strict" "--strict", action="store_true", default=False, help="make the validator strict"
) )
# "device"
device_parser = sub.add_parser("device", help="check device schema")
device_parser.add_argument(
"devices", type=pathlib.Path, nargs="+", help="device definition"
)
device_parser.add_argument(
"--exclude", type=str, default=[], action="append", help="exclude some devices"
)
device_parser.add_argument(
"--recursive",
"-r",
action="store_true",
default=False,
help="recurse on directories",
)
# Parse the command line
options = parser.parse_args() options = parser.parse_args()
if options.sub_command == "job": if options.sub_command == "job":
return handle_job(options) return handle(options, options.jobs, check_job)
elif options.sub_command == "device":
return handle(options, options.devices, check_device)
raise NotImplementedError("Unsupported command") raise NotImplementedError("Unsupported command")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment