2016-09-16 19:25:02 +02:00
|
|
|
import sys
|
2016-09-18 16:30:45 +02:00
|
|
|
from jsonschema import validate
|
|
|
|
import yaml
|
2016-09-18 18:18:22 +02:00
|
|
|
import argparse
|
|
|
|
import glob
|
2016-09-16 19:25:02 +02:00
|
|
|
|
|
|
|
|
2016-09-18 16:30:45 +02:00
|
|
|
def main(argv):
|
2016-09-18 18:18:22 +02:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("--all", help="Validate all protocols.", action="store_true")
|
|
|
|
parser.add_argument("-f", help="Validate file.", default=None)
|
|
|
|
parser.add_argument("-s", help="Use schema file.", default="schema.yml")
|
|
|
|
parser.add_argument("-v", help="Verbosity...", action="store_true")
|
|
|
|
args = parser.parse_args()
|
|
|
|
schemafile = args.s
|
2016-09-18 16:30:45 +02:00
|
|
|
schemadata = None
|
2016-09-16 19:25:02 +02:00
|
|
|
with open(schemafile, "r") as f:
|
2016-09-18 16:30:45 +02:00
|
|
|
schemadata = yaml.load(f)
|
2016-09-18 18:18:22 +02:00
|
|
|
if args.all:
|
|
|
|
# search for files and validate them.
|
|
|
|
print("Validating all files in protocols/osi*")
|
|
|
|
list = glob.glob("protocols/osi*/*.yml", recursive=True)
|
|
|
|
for file in list:
|
|
|
|
print("Validating file: " + file)
|
|
|
|
validateFile(file, schemadata, True if args.v else False)
|
|
|
|
pass
|
|
|
|
elif args.f is not None:
|
|
|
|
validateFile(args.f, schemadata, True if args.v else False)
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def validateFile(file, schema, verbose=False):
|
2016-09-18 16:30:45 +02:00
|
|
|
data = None
|
2016-09-18 18:18:22 +02:00
|
|
|
with open(file, "r") as f:
|
2016-09-18 16:30:45 +02:00
|
|
|
data = yaml.load(f)
|
2016-09-18 18:18:22 +02:00
|
|
|
try:
|
|
|
|
validate(data, schema)
|
|
|
|
print(file + " is valid.")
|
|
|
|
except:
|
|
|
|
print("Failed to validate: " + file)
|
|
|
|
if verbose:
|
|
|
|
raise
|
2016-09-16 19:25:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2016-09-18 16:30:45 +02:00
|
|
|
main(sys.argv)
|