|
| 1 | +""" |
| 2 | +Shared functions to be used within a Snakemake workflow for enforcing |
| 3 | +versions of dependencies the repo defines within its `nextstrain-pathogen.yaml` |
| 4 | +""" |
| 5 | + |
| 6 | +from os import path |
| 7 | +from sys import stderr |
| 8 | +from packaging.specifiers import SpecifierSet, InvalidSpecifier # snakemake dependency |
| 9 | +from packaging.version import Version, InvalidVersion # snakemake dependency |
| 10 | +from importlib.metadata import version as importlib_version, PackageNotFoundError |
| 11 | +from snakemake.common import __version__ as snakemake_version |
| 12 | +import subprocess |
| 13 | +from shutil import which |
| 14 | +import re |
| 15 | + |
| 16 | +class ProgramNotFoundError(Exception): |
| 17 | + pass |
| 18 | + |
| 19 | +class DependencyChecker(): |
| 20 | + def __init__(self, registration): |
| 21 | + super().__init__() |
| 22 | + self.error_attrs = ["version_incompatibilities", "not_found_dependencies", "declaration_errors", "unexpected_errors"] |
| 23 | + for attr in self.error_attrs: |
| 24 | + setattr(self, attr, []) |
| 25 | + self.declared_dependencies = self.parse_dependencies(registration) |
| 26 | + |
| 27 | + def parse_dependencies(self, registration): |
| 28 | + declared_dependencies = {} |
| 29 | + dependencies = registration.get('dependencies', {}) |
| 30 | + if type(dependencies) is not dict: |
| 31 | + raise WorkflowError(f"Within `nextstrain-pathogen.yaml` the dependencies must be a dict of <name>: <specifier>. You provided {type(dependencies).__name__}") |
| 32 | + for name, spec in dependencies.items(): |
| 33 | + try: |
| 34 | + declared_dependencies[name] = SpecifierSet(spec) |
| 35 | + except InvalidSpecifier: |
| 36 | + self.declaration_errors.append(f"This pathogen declared an invalid version specification for CLI program {name!r} of {spec}") |
| 37 | + return declared_dependencies |
| 38 | + |
| 39 | + def check(self): |
| 40 | + for name, specifier in self.declared_dependencies.items(): |
| 41 | + try: # First assume it's a python package |
| 42 | + self.check_python_package(name, specifier) |
| 43 | + except PackageNotFoundError: |
| 44 | + try: # if it's not a python package, maybe it's a CLI? |
| 45 | + self.check_cli_version(name, specifier) |
| 46 | + except ProgramNotFoundError: |
| 47 | + self.not_found_dependencies.append(f"{name!r} is not installed as a python dependency nor a CLI program. This pathogen requires a version satisfying {specifier!r}") |
| 48 | + |
| 49 | + def report_errors(self) -> bool: |
| 50 | + if sum([len(getattr(self, attr)) for attr in self.error_attrs])==0: |
| 51 | + print("All dependencies declared by this pathogen satisfied", file=stderr) |
| 52 | + return False |
| 53 | + |
| 54 | + print() |
| 55 | + print('_'*80) |
| 56 | + print(f"This pathogen declares dependencies which were not met.", file=stderr) |
| 57 | + for attr in self.error_attrs: |
| 58 | + errors = getattr(self, attr) |
| 59 | + if len(errors)==0: |
| 60 | + continue |
| 61 | + print(attr.replace('_', ' ').capitalize() + ":") |
| 62 | + print("-"*(len(attr)+1)) |
| 63 | + for msg in errors: |
| 64 | + print(f"\t{msg}", file=stderr) |
| 65 | + print('_'*80) |
| 66 | + print() |
| 67 | + return True |
| 68 | + |
| 69 | + def check_python_package(self, name: str, specifier: SpecifierSet): |
| 70 | + """ |
| 71 | + Check whether the installed python library *name* meets the specifier *specifier*. |
| 72 | + This uses importlib.metadata to check the available version which avoids importing |
| 73 | + the top-level import. |
| 74 | +
|
| 75 | + If the package is found but the version doesn't satisfy the provided *specifier* |
| 76 | + we log an error. Raises `PackageNotFoundError` if the package is not found. |
| 77 | + """ |
| 78 | + try: |
| 79 | + if name=='snakemake': |
| 80 | + # in conda environments importlib reports a snakemake version of 0.0.0, |
| 81 | + # so follow the approach of Snakemake's own min_version function |
| 82 | + version = Version(snakemake_version) |
| 83 | + else: |
| 84 | + version = Version(importlib_version(name)) |
| 85 | + except InvalidVersion: # <https://packaging.pypa.io/en/stable/version.html#packaging.version.InvalidVersion> |
| 86 | + self.unexpected_errors.append(f"Python dependency {name!r} reported a version of {output} which we were unable to parse") |
| 87 | + return |
| 88 | + |
| 89 | + ok = specifier.contains(version) |
| 90 | + # print(f"[DEBUG] Checking python dependency: {name!r} installed: {version} requirements: {specifier} OK? {ok}", file=stderr) |
| 91 | + if not ok: |
| 92 | + self.version_incompatibilities.append(f"Python dependency {name!r} version incompatibility. You have {version} but this pathogen declares {specifier}") |
| 93 | + |
| 94 | + def check_cli_version(self, name: str, specifier: SpecifierSet) -> None: |
| 95 | + """ |
| 96 | + Check whether the requested *name* is (a) installed and (b) reports a version |
| 97 | + which satisfies the *specifier*. Both (a) and (b) are achieved by calling |
| 98 | + `<name> --version`. |
| 99 | + |
| 100 | + If *name* isn't found (or is not executable) we raise a ProgramNotFoundError. |
| 101 | + If the package is found but the version doesn't satisfy the provided *specifier* |
| 102 | + we log an error. |
| 103 | + """ |
| 104 | + if which(name) is None: |
| 105 | + raise ProgramNotFoundError() |
| 106 | + |
| 107 | + cmd = [name, "--version"] |
| 108 | + try: |
| 109 | + proc = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True) |
| 110 | + output = ((proc.stdout or "") + " " + (proc.stderr or "")).strip() |
| 111 | + except subprocess.CalledProcessError as e: |
| 112 | + self.unexpected_errors.append(f"CLI program {name!r} exited code {e.returncode} when called using {' '.join(cmd)!r}") |
| 113 | + return |
| 114 | + |
| 115 | + m = re.search(r"\d+(\.\d+(\.\d+)?)?([.-][0-9A-Za-z]+)*", output) |
| 116 | + # 1 . 2 . 3 alpha etc |
| 117 | + if not m: |
| 118 | + self.unexpected_errors.append(f"CLI program {name!r} didn't report a parseable version when called via {' '.join(cmd)!r}") |
| 119 | + return |
| 120 | + |
| 121 | + try: |
| 122 | + version = Version(m.group(0)) |
| 123 | + except InvalidVersion: # <https://packaging.pypa.io/en/stable/version.html#packaging.version.InvalidVersion> |
| 124 | + self.unexpected_errors.append(f"CLI program {name!r} reported a version of {m.group(0)} which we were unable to parse") |
| 125 | + |
| 126 | + ok = specifier.contains(version) |
| 127 | + # print(f"[DEBUG] Checking CLI program: {name!r} installed: {version} requirements: {specifier} OK? {ok}", file=stderr) |
| 128 | + if not ok: |
| 129 | + self.version_incompatibilities.append(f"CLI program {name!r} version incompatibility. You have {version} but this pathogen declares {specifier}") |
| 130 | + |
| 131 | + |
| 132 | +def _read_nextstrain_pathogen_yaml(path: str) -> dict: |
| 133 | + """ |
| 134 | + Reads a ``nextstrain-pathogen.yaml`` file at *path* and returns a dict of |
| 135 | + its deserialized contents. |
| 136 | +
|
| 137 | + Taken from <https://github.com/nextstrain/cli/blob/4dbac262b22a3db9c48267e23f713ad56251ffd0/nextstrain/cli/pathogens.py#L843C1-L858C24> |
| 138 | + with modifications. (Note: pathogen repos don't need the nextstrain CLI to be installed and thus we can't import the code.) |
| 139 | + """ |
| 140 | + import yaml |
| 141 | + with open(path, encoding = "utf-8") as f: |
| 142 | + registration = yaml.safe_load(f) |
| 143 | + |
| 144 | + if not isinstance(registration, dict): |
| 145 | + raise ValueError(f"nextstrain-pathogen.yaml not a dict (got a {type(registration).__name__}): {str(path)!r}") |
| 146 | + |
| 147 | + return registration |
| 148 | + |
| 149 | +def pathogen_yaml(*, subdir_max=3): |
| 150 | + _searched_paths = [] |
| 151 | + for i in range(0, subdir_max): |
| 152 | + p = path.normpath(path.join(workflow.basedir, *['..']*i, "nextstrain-pathogen.yaml")) |
| 153 | + _searched_paths.append(p) |
| 154 | + if path.isfile(p): |
| 155 | + try: |
| 156 | + registration = _read_nextstrain_pathogen_yaml(p) |
| 157 | + except Exception as e: |
| 158 | + raise WorkflowError(f"Unable to parse {p} (as YAML). Error: {e}") |
| 159 | + break |
| 160 | + else: |
| 161 | + print("Could not find a nextstrain-pathogen.yaml file to check version dependencies.\n" |
| 162 | + "Searched paths:\n\t" + "\n\t".join(_searched_paths)) |
| 163 | + raise WorkflowError() |
| 164 | + return registration |
| 165 | + |
| 166 | + |
| 167 | +def check_pathogen_required_versions(*, fatal=True): |
| 168 | + """ |
| 169 | + Checks if dependencies declared via the pathogen's 'nextstrain-pathogen.yaml' |
| 170 | + are satisfied. Dependencies should be defined within the YAML like so: |
| 171 | +
|
| 172 | + dependencies: |
| 173 | + <name>: <specification> |
| 174 | +
|
| 175 | + The syntax of <specification> is detailed in <https://packaging.python.org/en/latest/specifications/version-specifiers/#id5> |
| 176 | +
|
| 177 | + We first check if the <name> is a python package. If it is not installed |
| 178 | + as a python package we check if it's an installed CLI and attempt to |
| 179 | + get the version by running `<name> --version`. |
| 180 | +
|
| 181 | + If *fatal* is True (default) we raise a WorkflowError if |
| 182 | + all conditions are not satisfied. |
| 183 | + """ |
| 184 | + if config.get('skip_dependency_version_checking', False) is True: |
| 185 | + print("Skipping dependency version checking as per config setting", file=stderr) |
| 186 | + return |
| 187 | + checker = DependencyChecker(pathogen_yaml()) |
| 188 | + checker.check() |
| 189 | + errors = checker.report_errors() |
| 190 | + if errors and fatal: |
| 191 | + raise WorkflowError("Dependencies not satisfied") |
0 commit comments