|
| 1 | +"""Fetch from conda database all available versions of the xarray dependencies and their |
| 2 | +publication date. Compare it against requirements/py36-min-all-deps.yml to verify the |
| 3 | +policy on obsolete dependencies is being followed. Print a pretty report :) |
| 4 | +""" |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | +from concurrent.futures import ThreadPoolExecutor |
| 8 | +from datetime import datetime, timedelta |
| 9 | +from typing import Dict, Iterator, Tuple |
| 10 | + |
| 11 | +import yaml |
| 12 | + |
| 13 | +IGNORE_DEPS = { |
| 14 | + "black", |
| 15 | + "coveralls", |
| 16 | + "flake8", |
| 17 | + "hypothesis", |
| 18 | + "mypy", |
| 19 | + "pip", |
| 20 | + "pytest", |
| 21 | + "pytest-cov", |
| 22 | + "pytest-env", |
| 23 | +} |
| 24 | + |
| 25 | +POLICY_MONTHS = {"python": 42, "numpy": 24, "pandas": 12, "scipy": 12} |
| 26 | +POLICY_MONTHS_DEFAULT = 6 |
| 27 | + |
| 28 | +has_errors = False |
| 29 | + |
| 30 | + |
| 31 | +def error(msg: str) -> None: |
| 32 | + global has_errors |
| 33 | + has_errors = True |
| 34 | + print("ERROR:", msg) |
| 35 | + |
| 36 | + |
| 37 | +def parse_requirements(fname) -> Iterator[Tuple[str, int, int]]: |
| 38 | + """Load requirements/py36-min-all-deps.yml |
| 39 | +
|
| 40 | + Yield (package name, major version, minor version) |
| 41 | + """ |
| 42 | + global has_errors |
| 43 | + |
| 44 | + with open(fname) as fh: |
| 45 | + contents = yaml.safe_load(fh) |
| 46 | + for row in contents["dependencies"]: |
| 47 | + if isinstance(row, dict) and list(row) == ["pip"]: |
| 48 | + continue |
| 49 | + pkg, eq, version = row.partition("=") |
| 50 | + if pkg.rstrip("<>") in IGNORE_DEPS: |
| 51 | + continue |
| 52 | + if pkg.endswith("<") or pkg.endswith(">") or eq != "=": |
| 53 | + error("package should be pinned with exact version: " + row) |
| 54 | + continue |
| 55 | + try: |
| 56 | + major, minor = version.split(".") |
| 57 | + except ValueError: |
| 58 | + error("expected major.minor (without patch): " + row) |
| 59 | + continue |
| 60 | + try: |
| 61 | + yield pkg, int(major), int(minor) |
| 62 | + except ValueError: |
| 63 | + error("failed to parse version: " + row) |
| 64 | + |
| 65 | + |
| 66 | +def query_conda(pkg: str) -> Dict[Tuple[int, int], datetime]: |
| 67 | + """Query the conda repository for a specific package |
| 68 | +
|
| 69 | + Return map of {(major version, minor version): publication date} |
| 70 | + """ |
| 71 | + stdout = subprocess.check_output( |
| 72 | + ["conda", "search", pkg, "--info", "-c", "defaults", "-c", "conda-forge"] |
| 73 | + ) |
| 74 | + out = {} # type: Dict[Tuple[int, int], datetime] |
| 75 | + major = None |
| 76 | + minor = None |
| 77 | + |
| 78 | + for row in stdout.decode("utf-8").splitlines(): |
| 79 | + label, _, value = row.partition(":") |
| 80 | + label = label.strip() |
| 81 | + if label == "file name": |
| 82 | + value = value.strip()[len(pkg) :] |
| 83 | + major, minor = value.split("-")[1].split(".")[:2] |
| 84 | + major = int(major) |
| 85 | + minor = int(minor) |
| 86 | + if label == "timestamp": |
| 87 | + assert major is not None |
| 88 | + assert minor is not None |
| 89 | + ts = datetime.strptime(value.split()[0].strip(), "%Y-%m-%d") |
| 90 | + |
| 91 | + if (major, minor) in out: |
| 92 | + out[major, minor] = min(out[major, minor], ts) |
| 93 | + else: |
| 94 | + out[major, minor] = ts |
| 95 | + |
| 96 | + # Hardcoded fix to work around incorrect dates in conda |
| 97 | + if pkg == "python": |
| 98 | + out.update( |
| 99 | + { |
| 100 | + (2, 7): datetime(2010, 6, 3), |
| 101 | + (3, 5): datetime(2015, 9, 13), |
| 102 | + (3, 6): datetime(2016, 12, 23), |
| 103 | + (3, 7): datetime(2018, 6, 27), |
| 104 | + (3, 8): datetime(2019, 10, 14), |
| 105 | + } |
| 106 | + ) |
| 107 | + |
| 108 | + return out |
| 109 | + |
| 110 | + |
| 111 | +def process_pkg( |
| 112 | + pkg: str, req_major: int, req_minor: int |
| 113 | +) -> Tuple[str, int, int, str, int, int, str, str]: |
| 114 | + """Compare package version from requirements file to available versions in conda. |
| 115 | + Return row to build pandas dataframe: |
| 116 | +
|
| 117 | + - package name |
| 118 | + - major version in requirements file |
| 119 | + - minor version in requirements file |
| 120 | + - publication date of version in requirements file (YYYY-MM-DD) |
| 121 | + - major version suggested by policy |
| 122 | + - minor version suggested by policy |
| 123 | + - publication date of version suggested by policy (YYYY-MM-DD) |
| 124 | + - status ("<", "=", "> (!)") |
| 125 | + """ |
| 126 | + print("Analyzing %s..." % pkg) |
| 127 | + versions = query_conda(pkg) |
| 128 | + |
| 129 | + try: |
| 130 | + req_published = versions[req_major, req_minor] |
| 131 | + except KeyError: |
| 132 | + error("not found in conda: " + pkg) |
| 133 | + return pkg, req_major, req_minor, "-", 0, 0, "-", "(!)" |
| 134 | + |
| 135 | + policy_months = POLICY_MONTHS.get(pkg, POLICY_MONTHS_DEFAULT) |
| 136 | + policy_published = datetime.now() - timedelta(days=policy_months * 30) |
| 137 | + |
| 138 | + policy_major = req_major |
| 139 | + policy_minor = req_minor |
| 140 | + policy_published_actual = req_published |
| 141 | + for (major, minor), published in reversed(sorted(versions.items())): |
| 142 | + if published < policy_published: |
| 143 | + break |
| 144 | + policy_major = major |
| 145 | + policy_minor = minor |
| 146 | + policy_published_actual = published |
| 147 | + |
| 148 | + if (req_major, req_minor) < (policy_major, policy_minor): |
| 149 | + status = "<" |
| 150 | + elif (req_major, req_minor) > (policy_major, policy_minor): |
| 151 | + status = "> (!)" |
| 152 | + error("Package is too new: " + pkg) |
| 153 | + else: |
| 154 | + status = "=" |
| 155 | + |
| 156 | + return ( |
| 157 | + pkg, |
| 158 | + req_major, |
| 159 | + req_minor, |
| 160 | + req_published.strftime("%Y-%m-%d"), |
| 161 | + policy_major, |
| 162 | + policy_minor, |
| 163 | + policy_published_actual.strftime("%Y-%m-%d"), |
| 164 | + status, |
| 165 | + ) |
| 166 | + |
| 167 | + |
| 168 | +def main() -> None: |
| 169 | + fname = sys.argv[1] |
| 170 | + with ThreadPoolExecutor(8) as ex: |
| 171 | + futures = [ |
| 172 | + ex.submit(process_pkg, pkg, major, minor) |
| 173 | + for pkg, major, minor in parse_requirements(fname) |
| 174 | + ] |
| 175 | + rows = [f.result() for f in futures] |
| 176 | + |
| 177 | + print("Package Required Policy Status") |
| 178 | + print("------------- ----------------- ----------------- ------") |
| 179 | + fmt = "{:13} {:>1d}.{:<2d} ({:10}) {:>1d}.{:<2d} ({:10}) {}" |
| 180 | + for row in rows: |
| 181 | + print(fmt.format(*row)) |
| 182 | + |
| 183 | + assert not has_errors |
| 184 | + |
| 185 | + |
| 186 | +if __name__ == "__main__": |
| 187 | + main() |
0 commit comments