1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
import asyncio
import bisect
import datetime
import pathlib
import sys
from dataclasses import dataclass, field
import rich_click as click
import yaml
from dateutil.relativedelta import relativedelta
from rattler import Gateway, Version
from rich.console import Console
from rich.panel import Panel
from rich.style import Style
from rich.table import Column, Table
from tlz.functoolz import curry, pipe
from tlz.itertoolz import concat, groupby
click.rich_click.SHOW_ARGUMENTS = True
channels = ["conda-forge"]
platforms = ["noarch", "linux-64"]
# these packages don't fail the CI, but will be printed in the report
ignore_failure_packages = [
# remove when we can pin to pydap 3.5.1 without error
"pydap",
]
# these packages are completely ignored
ignored_packages = [
"coveralls",
"pip",
"pytest",
"pytest-asyncio",
"pytest-cov",
"pytest-env",
"pytest-mypy-plugins",
"pytest-timeout",
"pytest-xdist",
"hypothesis",
]
@dataclass
class Policy:
package_months: dict
default_months: int
overrides: dict[str, Version] = field(default_factory=dict)
def minimum_version(self, package_name, releases):
if (override := self.overrides.get(package_name)) is not None:
return override
policy_months = self.package_months.get(package_name, self.default_months)
today = datetime.date.today()
cutoff_date = today - relativedelta(months=policy_months)
index = bisect.bisect_left(
releases, cutoff_date, key=lambda x: x.timestamp.date()
)
return releases[index - 1 if index > 0 else 0]
@dataclass
class Spec:
name: str
version: Version | None
@classmethod
def parse(cls, spec_text):
warnings = []
if ">" in spec_text or "<" in spec_text:
warnings.append(
f"package should be pinned with an exact version: {spec_text!r}"
)
spec_text = spec_text.replace(">", "").replace("<", "")
if "=" in spec_text:
name, version_text = spec_text.split("=", maxsplit=1)
version = Version(version_text)
segments = version.segments()
if len(segments) != 2 or (len(segments) == 3 and segments[2] != 0):
warnings.append(
f"package should be pinned to a minor version (got {version})"
)
else:
name = spec_text
version = None
return cls(name, version), (name, warnings)
@dataclass(order=True)
class Release:
version: Version
build_number: int
timestamp: datetime.datetime = field(compare=False)
@classmethod
def from_repodata_record(cls, repo_data):
return cls(
version=repo_data.version,
build_number=repo_data.build_number,
timestamp=repo_data.timestamp,
)
def parse_environment(text):
env = yaml.safe_load(text)
specs = []
warnings = []
for dep in env["dependencies"]:
spec, warnings_ = Spec.parse(dep)
warnings.append(warnings_)
specs.append(spec)
return specs, warnings
def is_preview(version):
candidates = ["rc", "beta", "alpha"]
*_, last_segment = version.segments()
return any(candidate in last_segment for candidate in candidates)
def group_packages(records):
groups = groupby(lambda r: r.name.normalized, records)
return {
name: sorted(map(Release.from_repodata_record, group))
for name, group in groups.items()
}
def filter_releases(predicate, releases):
return {
name: [r for r in records if predicate(r)] for name, records in releases.items()
}
def deduplicate_releases(package_info):
def deduplicate(releases):
return min(releases, key=lambda p: p.timestamp)
return {
name: list(map(deduplicate, groupby(lambda p: p.version, group).values()))
for name, group in package_info.items()
}
def find_policy_versions(policy, releases):
return {
name: policy.minimum_version(name, package_releases)
for name, package_releases in releases.items()
}
def is_suitable_release(release):
if release.timestamp is None:
return False
segments = release.version.extend_to_length(3).segments()
return segments[2] == [0]
def lookup_spec_release(spec, releases):
version = spec.version.extend_to_length(3)
return releases[spec.name][version]
def compare_versions(environments, policy_versions):
status = {}
for env, specs in environments.items():
env_status = any(
(
(spec.version > policy_versions[spec.name].version)
and (spec.name not in ignore_failure_packages)
)
for spec in specs
)
status[env] = env_status
return status
def version_comparison_symbol(required, policy):
if required < policy:
return "<"
elif required > policy:
return ">"
else:
return "="
def format_bump_table(specs, policy_versions, releases, warnings):
table = Table(
Column("Package", width=20),
Column("Required", width=8),
"Required (date)",
Column("Policy", width=8),
"Policy (date)",
"Status",
)
heading_style = Style(color="#ff0000", bold=True)
warning_style = Style(color="#ffff00", bold=True)
styles = {
">": Style(color="#ff0000", bold=True),
"=": Style(color="#008700", bold=True),
"<": Style(color="#d78700", bold=True),
}
for spec in specs:
policy_release = policy_versions[spec.name]
policy_version = policy_release.version.with_segments(0, 2)
policy_date = policy_release.timestamp
required_version = spec.version
required_date = lookup_spec_release(spec, releases).timestamp
status = version_comparison_symbol(required_version, policy_version)
style = styles[status]
table.add_row(
spec.name,
str(required_version),
f"{required_date:%Y-%m-%d}",
str(policy_version),
f"{policy_date:%Y-%m-%d}",
status,
style=style,
)
grid = Table.grid(expand=True, padding=(0, 2))
grid.add_column(style=heading_style, vertical="middle")
grid.add_column()
grid.add_row("Version summary", table)
if any(warnings.values()):
warning_table = Table(width=table.width, expand=True)
warning_table.add_column("Package")
warning_table.add_column("Warning")
for package, messages in warnings.items():
if not messages:
continue
warning_table.add_row(package, messages[0], style=warning_style)
for message in messages[1:]:
warning_table.add_row("", message, style=warning_style)
grid.add_row("Warnings", warning_table)
return grid
@click.command()
@click.argument(
"environment_paths",
type=click.Path(exists=True, readable=True, path_type=pathlib.Path),
nargs=-1,
)
def main(environment_paths):
console = Console()
parsed_environments = {
path.stem: parse_environment(path.read_text()) for path in environment_paths
}
warnings = {
env: dict(warnings_) for env, (_, warnings_) in parsed_environments.items()
}
environments = {
env: [spec for spec in specs if spec.name not in ignored_packages]
for env, (specs, _) in parsed_environments.items()
}
all_packages = list(
dict.fromkeys(spec.name for spec in concat(environments.values()))
)
policy_months = {
"python": 30,
"numpy": 18,
}
policy_months_default = 12
overrides = {}
policy = Policy(
policy_months, default_months=policy_months_default, overrides=overrides
)
gateway = Gateway()
query = gateway.query(channels, platforms, all_packages, recursive=False)
records = asyncio.run(query)
package_releases = pipe(
records,
concat,
group_packages,
curry(filter_releases, lambda r: r.timestamp is not None),
deduplicate_releases,
)
policy_versions = pipe(
package_releases,
curry(filter_releases, is_suitable_release),
curry(find_policy_versions, policy),
)
status = compare_versions(environments, policy_versions)
release_lookup = {
n: {r.version: r for r in releases} for n, releases in package_releases.items()
}
grids = {
env: format_bump_table(specs, policy_versions, release_lookup, warnings[env])
for env, specs in environments.items()
}
root_grid = Table.grid()
root_grid.add_column()
for env, grid in grids.items():
root_grid.add_row(Panel(grid, title=env, expand=True))
console.print(root_grid)
status_code = 1 if any(status.values()) else 0
sys.exit(status_code)
if __name__ == "__main__":
main()
|