79 lines · python
1"""2DTLTO JSON Validator.3 4This script is used for DTLTO testing to check that the distributor has5been invoked correctly.6 7Usage:8 python validate.py <json_file>9 10Arguments:11 - <json_file> : JSON file describing the DTLTO jobs.12 13The script does the following:14 1. Prints the supplied distributor arguments.15 2. Loads the JSON file.16 3. Pretty prints the JSON.17 4. Validates the structure and required fields.18"""19 20import sys21import json22from pathlib import Path23 24 25def take(jvalue, jpath):26 parts = jpath.split(".")27 for part in parts[:-1]:28 jvalue = jvalue[part]29 return jvalue.pop(parts[-1], KeyError)30 31 32def validate(jdoc):33 # Check the format of the JSON34 assert type(take(jdoc, "common.linker_output")) is str35 36 args = take(jdoc, "common.args")37 assert type(args) is list38 assert len(args) > 039 assert all(type(i) is str for i in args)40 41 inputs = take(jdoc, "common.inputs")42 assert type(inputs) is list43 assert all(type(i) is str for i in inputs)44 45 assert len(take(jdoc, "common")) == 046 47 jobs = take(jdoc, "jobs")48 assert type(jobs) is list49 for j in jobs:50 assert type(j) is dict51 52 for attr, min_size in (("args", 0), ("inputs", 2), ("outputs", 1)):53 array = take(j, attr)54 assert len(array) >= min_size55 assert type(array) is list56 assert all(type(a) is str for a in array)57 58 assert len(j) == 059 60 assert len(jdoc) == 061 62 63if __name__ == "__main__":64 json_arg = Path(sys.argv[-1])65 distributor_args = sys.argv[1:-1]66 67 # Print the supplied distributor arguments.68 print(f"{distributor_args=}")69 70 # Load the DTLTO information from the input JSON file.71 with json_arg.open() as f:72 jdoc = json.load(f)73 74 # Write the input JSON to stdout.75 print(json.dumps(jdoc, indent=4))76 77 # Check the format of the JSON.78 validate(jdoc)79