Update checker scripts to work correctly from the scripts/ dir

check-swagger-sources also had a bug which caused it to fail while validating the directory
structure of /data/api. This was fixed by @KitsuneRal - thank you!
pull/977/head
Andrew Morgan 3 years ago committed by Richard van der Hoff
parent b8dafe86f4
commit 64231f079d

@ -128,7 +128,14 @@ def check_example_dir(exampledir, schemadir):
if __name__ == '__main__': if __name__ == '__main__':
# Get the directory that this script is residing in
script_directory = os.path.dirname(os.path.realpath(__file__))
# Resolve the directories to check, relative to the script path
examples_directory = os.path.join(script_directory, "../event-schemas/examples")
schema_directory = os.path.join(script_directory, "../event-schemas/schema")
try: try:
check_example_dir("examples", "schema") check_example_dir(examples_directory, schema_directory)
except: except:
sys.exit(1) sys.exit(1)

@ -108,13 +108,36 @@ def check_swagger_file(filepath):
def resolve_references(path, schema): def resolve_references(path, schema):
"""Recurse through a given schema until we find a $ref key. Upon doing so,
check that the referenced file exists, then load it up and check all of the
references in that file. Continue on until we've hit all dead ends.
$ref values are deleted from schemas as they are validated, to prevent
duplicate work.
"""
if isinstance(schema, dict): if isinstance(schema, dict):
# do $ref first # do $ref first
if '$ref' in schema: if '$ref' in schema:
value = schema['$ref'] # Pull the referenced filepath from the schema
path = os.path.abspath(os.path.join(os.path.dirname(path), value)) referenced_file = schema['$ref']
ref = load_file("file://" + path)
result = resolve_references(path, ref) # Referenced filepaths are relative, so take the current path's
# directory and append the relative, referenced path to it.
inner_path = os.path.join(os.path.dirname(path), referenced_file)
# Then convert the path (which may contiain '../') into a
# normalised, absolute path
inner_path = os.path.abspath(inner_path)
# Load the referenced file
ref = load_file("file://" + inner_path)
# Check that the references in *this* file are valid
result = resolve_references(inner_path, ref)
# They were valid, and so were the sub-references. Delete
# the reference here to ensure we don't pass over it again
# when checking other files
del schema['$ref'] del schema['$ref']
else: else:
result = {} result = {}
@ -143,15 +166,22 @@ def load_file(path):
if __name__ == '__main__': if __name__ == '__main__':
paths = sys.argv[1:] # Get the directory that this script is residing in
if not paths: script_directory = os.path.dirname(os.path.realpath(__file__))
paths = []
for (root, dirs, files) in os.walk(os.curdir): # Resolve the directory containing the swagger sources,
for filename in files: # relative to the script path
if filename.endswith(".yaml"): source_files_directory = os.path.realpath(os.path.join(script_directory, "../data"))
paths.append(os.path.join(root, filename))
for path in paths: # Walk the source path directory, looking for YAML files to check
try: for (root, dirs, files) in os.walk(source_files_directory):
check_swagger_file(path) for filename in files:
except Exception as e: if not filename.endswith(".yaml"):
raise ValueError("Error checking file %r" % (path,), e) continue
path = os.path.join(root, filename)
try:
check_swagger_file(path)
except Exception as e:
raise ValueError("Error checking file %s" % (path,), e)

Loading…
Cancel
Save