Skip to content

Commit

Permalink
Improvements to the script validating WDLs using miniwdl (#402)
Browse files Browse the repository at this point in the history
* Improvements to the script validating WDLs using miniwdl.

# - Take imports dir as an input argument;
# - Allow setting max depth of import, and set it to max 2 (compared to max 10 of miniwdl default);
# - Improve displaying messages on the console.

* Improve logging errors when pos is None.

* fix a linting issue.

* Make a necessary str to int conversion.
  • Loading branch information
VJalili authored Sep 15, 2022
1 parent ed87266 commit 2da5e29
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions scripts/test/miniwdl_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,66 @@
import WDL


def format_error_message(error_type, message, pos):
return f"{error_type}: '{message}' at {pos.abspath}, Line: {pos.line}, Col: {pos.column}"
def format_error_message(error_type, message, pos=None, tips=None):
tips = f"Tips: {tips}" if tips else ""
msg = f"{error_type}: '{message}'"
if pos:
msg += f"at {pos.abspath}, Line: {pos.line}, Col: {pos.column}."
msg += tips
return msg


def check(wdl_filename):
def check(wdl_filename, imports_dir, import_max_depth):
try:
WDL.load(wdl_filename)
WDL.load(wdl_filename, path=[imports_dir], import_max_depth=import_max_depth)
return True
except WDL.Error.SyntaxError as e:
print(format_error_message("Syntax Error", e, e.pos))
except WDL.Error.ValidationError as e:
print(format_error_message("Validation Error", e, e.node.pos))
print(format_error_message("Validation Error", e, e.node.pos if e.node else None))
except WDL.Error.MultipleValidationErrors as e:
for ex in e.exceptions:
print(format_error_message("Validation Error", ex, ex.node.pos))
print(format_error_message("Validation Error", ex, ex.node.pos if ex.node else None))
except WDL.Error.ImportError as e:
print(e)
print(format_error_message("Import Error", e, e.pos))
print(format_error_message(
"Import Error", e, e.pos,
f"This could indicate two issues; either the WDLs imported in {wdl_filename} "
f"cannot be found in the given imports directory (i.e., `{imports_dir}`), "
f"or miniwdl identifies errors while parsing the imported WDLs in "
f"maximum {import_max_depth} import depth. You may separately check the "
f"WDLs imported in {wdl_filename} and make sure they all pass the "
f"miniwdl validation."))
finally:
return False


def main():
parser = argparse.ArgumentParser()
parser.add_argument("files", nargs="+", help="The WDL files to be checked.")
parser.add_argument("--imports-dir", required=True, help="The directory to search for WDLs to be imported.")
parser.add_argument("--max-import-depth", default=2, help="Maximum depth of imports to check for validating WDLs.")
args = parser.parse_args()

wdl_filenames = args.files
imports_dir = args.imports_dir
max_import_depth = int(args.max_import_depth)

exit_code = 0
counter = 0
for wdl_filename in wdl_filenames:
if not check(wdl_filename):
# print text in yellow.
print("\033[93m" + f"\nValidating file {wdl_filename}" + "\033[0m")
if not check(wdl_filename, imports_dir, max_import_depth):
exit_code = 1
counter += 1
print(f"{counter} of {len(wdl_filenames)} checked WDL files failed the miniwdl validation.")

print("\n\n")
if counter > 0:
# print text in bold red.
print("\033[1m\033[91m" + f"{counter} of {len(wdl_filenames)} checked WDL files failed the miniwdl validation." + "\033[0m")
else:
# print text in bold green.
print("\033[1m\033[92m" + "All WDL files are successfully passed the miniwdl validation." + "\033[0m")
sys.exit(exit_code)


Expand Down

0 comments on commit 2da5e29

Please sign in to comment.