Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check prefix before complete loading when checking yaml and cwl tools #12604

Merged
merged 6 commits into from
May 25, 2022
Merged
Prev Previous commit
Next Next commit
make return values more explicit
  • Loading branch information
bernt-matthias committed Apr 22, 2022
commit 4d2ee6e2d02052af42a96f969be86cfb762031d5
19 changes: 10 additions & 9 deletions lib/galaxy/tool_util/loader_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,31 +198,32 @@ def as_dict_if_looks_like_yaml_or_cwl_with_class(path, classes):
try:
start_contents = f.read(5 * 1024)
except UnicodeDecodeError:
return None
return False, None
if re.search(rf"\nclass:\s+({'|'.join(classes)})\s*\n", start_contents) is None:
return None
return False, None

with open(path) as f:
try:
as_dict = yaml.safe_load(f)
except Exception:
return None
return False, None

if not isinstance(as_dict, dict):
return None
return False, None

file_class = as_dict.get("class", None)
if file_class not in classes:
return None
return False, None

return as_dict
return True, as_dict


def is_a_yaml_with_class(path, classes):
"""Determine if a file is a valid YAML with a supplied ``class`` entry."""
if not _has_extension(path, YAML_EXTENSIONS):
return False
return as_dict_if_looks_like_yaml_or_cwl_with_class(path, classes) is not None
is_yaml, as_dict = as_dict_if_looks_like_yaml_or_cwl_with_class(path, classes)
return is_yaml


def looks_like_a_tool_yaml(path):
Expand All @@ -235,8 +236,8 @@ def looks_like_a_cwl_artifact(path, classes=None):
if not _has_extension(path, CWL_EXTENSIONS):
return False

as_dict = as_dict_if_looks_like_yaml_or_cwl_with_class(path, classes)
if as_dict is None:
is_yaml, as_dict = as_dict_if_looks_like_yaml_or_cwl_with_class(path, classes)
if not is_yaml is None:
bernt-matthias marked this conversation as resolved.
Show resolved Hide resolved
return False

file_cwl_version = as_dict.get("cwlVersion", None)
Expand Down