| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
import os |
| 12 |
import pathlib |
| 13 |
import re |
| 14 |
import subprocess |
| 15 |
import sys |
| 16 |
from datetime import date |
| 17 |
from pathlib import Path |
| 18 |
from textwrap import indent |
| 19 |
|
| 20 |
import requests |
| 21 |
from coverage.config import CoverageConfig |
| 22 |
|
| 23 |
import hypothesistooling as tools |
| 24 |
import hypothesistooling.projects.hypothesispython as hp |
| 25 |
from hypothesistooling import installers as install, releasemanagement as rm |
| 26 |
from hypothesistooling.scripts import pip_tool |
| 27 |
|
| 28 |
TASKS = {} |
| 29 |
BUILD_FILES = tuple( |
| 30 |
os.path.join(tools.ROOT, f) |
| 31 |
for f in ["tooling", "requirements", ".github", "hypothesis-python/tox.ini"] |
| 32 |
) |
| 33 |
TODAY = date.today().isoformat() |
| 34 |
|
| 35 |
|
| 36 |
def task(if_changed=()): |
| 37 |
if not isinstance(if_changed, tuple): |
| 38 |
if_changed = (if_changed,) |
| 39 |
|
| 40 |
def accept(fn): |
| 41 |
def wrapped(*args, **kwargs): |
| 42 |
if if_changed and tools.IS_PULL_REQUEST: |
| 43 |
if not tools.has_changes(if_changed + BUILD_FILES): |
| 44 |
changed = ", ".join(map(str, if_changed)) |
| 45 |
print(f"Skipping task due to no changes in {changed}") |
| 46 |
return |
| 47 |
fn(*args, **kwargs) |
| 48 |
|
| 49 |
wrapped.__name__ = fn.__name__ |
| 50 |
name = fn.__name__.replace("_", "-") |
| 51 |
if name != "<lambda>": |
| 52 |
TASKS[name] = wrapped |
| 53 |
return wrapped |
| 54 |
|
| 55 |
return accept |
| 56 |
|
| 57 |
|
| 58 |
@task() |
| 59 |
def check_installed(): |
| 60 |
"""No-op task that can be used to test for a successful install (so we |
| 61 |
don't fail to run if a previous install failed midway).""" |
| 62 |
|
| 63 |
|
| 64 |
def codespell(*files): |
| 65 |
pip_tool( |
| 66 |
"codespell", |
| 67 |
"--check-hidden", |
| 68 |
"--check-filenames", |
| 69 |
"--ignore-words=./tooling/codespell-ignore.txt", |
| 70 |
# passing a custom --dictionary disables the default dictionary by default. |
| 71 |
# Add it back in with --dictionary=-. |
| 72 |
"--dictionary=-", |
| 73 |
"--dictionary=./tooling/codespell-dict.txt", |
| 74 |
"--skip=__pycache__,.mypy_cache,.venv,.git,tlds-alpha-by-domain.txt", |
| 75 |
*files, |
| 76 |
) |
| 77 |
|
| 78 |
|
| 79 |
@task() |
| 80 |
def lint(): |
| 81 |
pip_tool("ruff", "check", ".") |
| 82 |
codespell(*(p for p in tools.all_files() if not p.name.endswith("by-domain.txt"))) |
| 83 |
|
| 84 |
failed = False |
| 85 |
|
| 86 |
matches = subprocess.run( |
| 87 |
r"git grep -En '@(dataclasses\.)?dataclass\(.*\)' " |
| 88 |
"| grep -Ev 'frozen=.*slots=|slots=.*frozen='", |
| 89 |
shell=True, |
| 90 |
capture_output=True, |
| 91 |
text=True, |
| 92 |
).stdout |
| 93 |
if matches: |
| 94 |
print("\nAll dataclass decorators must pass slots= and frozen= arguments:") |
| 95 |
print(indent(matches, " ")) |
| 96 |
failed = True |
| 97 |
|
| 98 |
matches = subprocess.run( |
| 99 |
r"git grep -nP '\b(the|as|a|to|because|user|test|about|from|only)\s+\1\b'", |
| 100 |
shell=True, |
| 101 |
capture_output=True, |
| 102 |
text=True, |
| 103 |
).stdout |
| 104 |
if matches: |
| 105 |
print("\nFound duplicate words:") |
| 106 |
print(indent(matches, " ")) |
| 107 |
failed = True |
| 108 |
|
| 109 |
if failed: |
| 110 |
sys.exit(1) |
| 111 |
|
| 112 |
|
| 113 |
def do_release(package): |
| 114 |
if not package.has_release(): |
| 115 |
print(f"No release for {package.__name__}") |
| 116 |
return |
| 117 |
|
| 118 |
os.chdir(package.BASE_DIR) |
| 119 |
|
| 120 |
print("Updating changelog and version") |
| 121 |
package.update_changelog_and_version() |
| 122 |
|
| 123 |
print("Committing changes") |
| 124 |
rm.commit_pending_release(package) |
| 125 |
|
| 126 |
print("Building distribution") |
| 127 |
package.build_distribution() |
| 128 |
|
| 129 |
print("Looks good to release!") |
| 130 |
|
| 131 |
tag_name = package.tag_name() |
| 132 |
|
| 133 |
print(f"Creating tag {tag_name}") |
| 134 |
tools.create_tag(tag_name) |
| 135 |
tools.push_tag(tag_name) |
| 136 |
|
| 137 |
print("Uploading distribution") |
| 138 |
package.upload_distribution() |
| 139 |
|
| 140 |
|
| 141 |
@task() |
| 142 |
def deploy(): |
| 143 |
HEAD = tools.hash_for_name("HEAD") |
| 144 |
MASTER = tools.hash_for_name("origin/master") |
| 145 |
|
| 146 |
print("Current head: ", HEAD) |
| 147 |
print("Current master:", MASTER) |
| 148 |
|
| 149 |
if not tools.is_ancestor(HEAD, MASTER): |
| 150 |
print("Not deploying due to not being on master") |
| 151 |
sys.exit(0) |
| 152 |
|
| 153 |
if "TWINE_PASSWORD" not in os.environ: |
| 154 |
print("Running without access to secure variables, so no deployment") |
| 155 |
sys.exit(0) |
| 156 |
|
| 157 |
tools.configure_git() |
| 158 |
|
| 159 |
for project in tools.all_projects(): |
| 160 |
do_release(project) |
| 161 |
|
| 162 |
sys.exit(0) |
| 163 |
|
| 164 |
|
| 165 |
# See https://www.linuxfoundation.org/blog/copyright-notices-in-open-source-software-projects/ |
| 166 |
HEADER = """ |
| 167 |
# This file is part of Hypothesis, which may be found at |
| 168 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 169 |
# |
| 170 |
# Copyright the Hypothesis Authors. |
| 171 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 172 |
# |
| 173 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 174 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 175 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 176 |
""".strip() |
| 177 |
|
| 178 |
# this pattern is copied from shed |
| 179 |
# https://github.com/Zac-HD/shed/blob/6471da71c5b5cc443443ef5ed072799db275e7c0/src/shed/__init__.py#L297 |
| 180 |
rst_pattern = re.compile( |
| 181 |
r"(?P<before>" |
| 182 |
r"^(?P<indent> *)\.\. " |
| 183 |
r"(?P<block>jupyter-execute::|" |
| 184 |
r"invisible-code-block: python|" # magic rst comment for Sybil doctests |
| 185 |
r"(code|code-block|sourcecode|ipython):: (python|py|sage|python3|py3|numpy))\n" |
| 186 |
r"((?P=indent) +:.*\n)*" |
| 187 |
r"\n*" |
| 188 |
r")" |
| 189 |
r"(?P<code>(^((?P=indent) +.*)?\n)+)", |
| 190 |
flags=re.MULTILINE, |
| 191 |
) |
| 192 |
|
| 193 |
|
| 194 |
def remove_consecutive_newlines_in_rst(path: Path): |
| 195 |
# replace 2+ empty lines in `.. code-block:: python` blocks with just one empty |
| 196 |
# line |
| 197 |
content = path.read_text() |
| 198 |
processed_content = rst_pattern.sub( |
| 199 |
lambda m: m["before"] + re.sub(r"\n{3,}", "\n\n", m["code"]), content |
| 200 |
) |
| 201 |
if processed_content != content: |
| 202 |
path.write_text(processed_content) |
| 203 |
|
| 204 |
|
| 205 |
@task() |
| 206 |
def format(): |
| 207 |
changed = tools.modified_files() |
| 208 |
|
| 209 |
format_all = os.environ.get("FORMAT_ALL", "").lower() == "true" |
| 210 |
if "requirements/tools.txt" in changed: |
| 211 |
# We've changed the tools, which includes a lot of our formatting |
| 212 |
# logic, so we need to rerun formatters. |
| 213 |
format_all = True |
| 214 |
|
| 215 |
paths = tools.all_files() if format_all else changed |
| 216 |
|
| 217 |
doc_paths_to_format = [p for p in sorted(paths) if p.suffix in {".rst", ".md"}] |
| 218 |
paths_to_format = [p for p in sorted(paths) if p.suffix == ".py"] |
| 219 |
|
| 220 |
if not (paths_to_format or doc_paths_to_format): |
| 221 |
return |
| 222 |
|
| 223 |
# .coveragerc lists several regex patterns to treat as nocover pragmas, and |
| 224 |
# we want to find (and delete) cases where # pragma: no cover is redundant. |
| 225 |
def warn(msg): |
| 226 |
raise Exception(msg) |
| 227 |
|
| 228 |
config = CoverageConfig() |
| 229 |
config.from_file(os.path.join(hp.BASE_DIR, ".coveragerc"), warn=warn, our_file=True) |
| 230 |
pattern = "|".join(l for l in config.exclude_list if "pragma" not in l) |
| 231 |
unused_pragma_pattern = re.compile(f"(({pattern}).*) # pragma: no (branch|cover)") |
| 232 |
last_header_line = HEADER.splitlines()[-1].rstrip() |
| 233 |
|
| 234 |
for p in paths_to_format: |
| 235 |
lines = [] |
| 236 |
with open(p, encoding="utf-8") as fp: |
| 237 |
shebang = None |
| 238 |
first = True |
| 239 |
in_header = True |
| 240 |
for l in fp: |
| 241 |
if first: |
| 242 |
first = False |
| 243 |
if l[:2] == "#!": |
| 244 |
shebang = l |
| 245 |
continue |
| 246 |
elif in_header and l.rstrip() == last_header_line: |
| 247 |
in_header = False |
| 248 |
lines = [] |
| 249 |
else: |
| 250 |
lines.append(unused_pragma_pattern.sub(r"\1", l)) |
| 251 |
|
| 252 |
source = "".join(lines).strip() |
| 253 |
with open(p, "w", encoding="utf-8") as fp: |
| 254 |
if shebang is not None: |
| 255 |
fp.write(shebang) |
| 256 |
fp.write("\n") |
| 257 |
fp.write(HEADER) |
| 258 |
if source: |
| 259 |
fp.write("\n\n") |
| 260 |
fp.write(source) |
| 261 |
fp.write("\n") |
| 262 |
|
| 263 |
codespell("--write-changes", *paths_to_format, *doc_paths_to_format) |
| 264 |
pip_tool("ruff", "check", "--fix-only", ".") |
| 265 |
pip_tool("shed", "--py310-plus", *paths_to_format, *doc_paths_to_format) |
| 266 |
|
| 267 |
for p in doc_paths_to_format: |
| 268 |
remove_consecutive_newlines_in_rst(p) |
| 269 |
|
| 270 |
|
| 271 |
VALID_STARTS = (HEADER.split()[0], "#!/usr/bin/env python") |
| 272 |
|
| 273 |
|
| 274 |
@task() |
| 275 |
def check_format(): |
| 276 |
format() |
| 277 |
n = max(map(len, VALID_STARTS)) |
| 278 |
bad = False |
| 279 |
for p in tools.all_files(): |
| 280 |
if p.suffix != ".py": |
| 281 |
continue |
| 282 |
with open(p, encoding="utf-8") as fp: |
| 283 |
start = fp.read(n) |
| 284 |
if not any(start.startswith(s) for s in VALID_STARTS): |
| 285 |
print(f"{p} has incorrect start {start!r}", file=sys.stderr) |
| 286 |
bad = True |
| 287 |
assert not bad |
| 288 |
try: |
| 289 |
check_not_changed() |
| 290 |
except Exception: |
| 291 |
box_width = 50 |
| 292 |
inner_width = box_width - 2 |
| 293 |
content_width = inner_width - 2 |
| 294 |
msg1 = "Note: code differed after formatting." |
| 295 |
msg2 = "To fix this, run:" |
| 296 |
msg3 = " ./build.sh format" |
| 297 |
|
| 298 |
lines = [ |
| 299 |
"", |
| 300 |
" " + "*" * box_width, |
| 301 |
" *" + " " * inner_width + "*", |
| 302 |
" * " + msg1 + " " * (content_width - len(msg1)) + "*", |
| 303 |
" *" + " " * inner_width + "*", |
| 304 |
" * " + msg2 + " " * (content_width - len(msg2)) + "*", |
| 305 |
" *" + " " * inner_width + "*", |
| 306 |
" * " + msg3 + " " * (content_width - len(msg3)) + "*", |
| 307 |
" *" + " " * inner_width + "*", |
| 308 |
" " + "*" * box_width, |
| 309 |
"", |
| 310 |
] |
| 311 |
print("\n".join(lines), file=sys.stderr) |
| 312 |
raise |
| 313 |
|
| 314 |
|
| 315 |
def check_not_changed(): |
| 316 |
subprocess.check_call(["git", "diff", "--exit-code"]) |
| 317 |
|
| 318 |
|
| 319 |
@task() |
| 320 |
def compile_requirements(*, upgrade=False): |
| 321 |
if upgrade: |
| 322 |
extra = ["--upgrade", "--rebuild"] |
| 323 |
else: |
| 324 |
extra = [] |
| 325 |
|
| 326 |
for f in Path("requirements").glob("*.in"): |
| 327 |
out_file = f.with_suffix(".txt") |
| 328 |
pip_tool( |
| 329 |
"pip-compile", |
| 330 |
"--allow-unsafe", # future default, not actually unsafe |
| 331 |
"--resolver=backtracking", # new pip resolver, default in pip-compile 7+ |
| 332 |
*extra, |
| 333 |
str(f), |
| 334 |
"hypothesis-python/pyproject.toml", |
| 335 |
"--output-file", |
| 336 |
str(out_file), |
| 337 |
cwd=tools.ROOT, |
| 338 |
env={ |
| 339 |
"CUSTOM_COMPILE_COMMAND": "./build.sh upgrade-requirements", |
| 340 |
**os.environ, |
| 341 |
}, |
| 342 |
) |
| 343 |
# Check that we haven't added anything to output files without adding to inputs |
| 344 |
out_pkgs = out_file.read_text(encoding="utf-8") |
| 345 |
for p in f.read_text(encoding="utf-8").splitlines(): |
| 346 |
p = p.lower().replace("_", "-") |
| 347 |
if re.fullmatch(r"[a-z-]+", p): |
| 348 |
assert p + "==" in out_pkgs, f"Package `{p}` deleted from {out_file}" |
| 349 |
out_file.write_text(out_pkgs.replace(f"{tools.ROOT}/", "")) |
| 350 |
|
| 351 |
|
| 352 |
def update_python_versions(): |
| 353 |
install.ensure_python(PYTHONS[ci_version]) |
| 354 |
# `uv python list` prints one candidate per line; it reports only the |
| 355 |
# interpreters uv knows how to install, so we don't need to translate |
| 356 |
# from pyenv's naming scheme any more. |
| 357 |
result = subprocess.run( |
| 358 |
["uv", "python", "list", "--all-versions", "--output-format", "text"], |
| 359 |
stdout=subprocess.PIPE, |
| 360 |
check=True, |
| 361 |
).stdout.decode() |
| 362 |
# Each line starts with something like `cpython-3.14.4-linux-aarch64-gnu` |
| 363 |
# or `cpython-3.14.4+freethreaded-linux-aarch64-gnu` or `pypy-3.10.16-…`. |
| 364 |
# We pull out the `(impl, version)` bit at the head. |
| 365 |
versions = [] |
| 366 |
for line in result.splitlines(): |
| 367 |
token = line.strip().split()[0] if line.strip() else "" |
| 368 |
if m := re.match( |
| 369 |
r"(cpython|pypy)-(\d+\.\d+\.\d+(?:[a-z]\d+)?)(\+freethreaded)?", token |
| 370 |
): |
| 371 |
impl, ver, ft = m.groups() |
| 372 |
versions.append((impl, ver, bool(ft))) |
| 373 |
|
| 374 |
min_minor_version = int( |
| 375 |
re.search( |
| 376 |
r'requires-python = ">= ?3.(\d+)"', |
| 377 |
Path("hypothesis-python/pyproject.toml").read_text(encoding="utf-8"), |
| 378 |
).group(1) |
| 379 |
) |
| 380 |
# For cpython we key by `3.NN` (or `3.NNt` for free-threaded) and pick the |
| 381 |
# newest available version. pypy we key by `pypyX.Y` and format as |
| 382 |
# `pypyX.Y-dev` → the existing PYTHONS dict keeps the upstream pypy |
| 383 |
# version string for informational purposes, so we reconstruct those. |
| 384 |
best = {} |
| 385 |
for impl, ver, ft in versions: |
| 386 |
major_minor = ".".join(ver.split(".")[:2]) |
| 387 |
if int(major_minor.split(".")[-1]) < min_minor_version: |
| 388 |
continue |
| 389 |
if impl == "cpython": |
| 390 |
key = f"{major_minor}{'t' if ft else ''}" |
| 391 |
candidate = f"{ver}{'+freethreaded' if ft else ''}" |
| 392 |
else: |
| 393 |
assert impl == "pypy" |
| 394 |
key = f"pypy{major_minor}" |
| 395 |
candidate = f"pypy{major_minor}-{ver}" |
| 396 |
# `uv python list` sorts newest-first, so first hit wins. |
| 397 |
best.setdefault(key, candidate) |
| 398 |
|
| 399 |
if best == PYTHONS: |
| 400 |
return |
| 401 |
|
| 402 |
# Write the new mapping back to this file |
| 403 |
thisfile = pathlib.Path(__file__) |
| 404 |
before = thisfile.read_text(encoding="utf-8") |
| 405 |
after = re.sub(r"\nPYTHONS = \{[^{}]+\}", f"\nPYTHONS = {best}", before) |
| 406 |
thisfile.write_text(after, encoding="utf-8") |
| 407 |
pip_tool("shed", str(thisfile)) |
| 408 |
|
| 409 |
# Automatically sync ci_version with the version in build.sh |
| 410 |
build_sh = tools.ROOT / "build.sh" |
| 411 |
sh_before = build_sh.read_text(encoding="utf-8") |
| 412 |
sh_after = re.sub(r"3\.\d\d?\.\d\d?", best[ci_version], sh_before) |
| 413 |
if sh_before != sh_after: |
| 414 |
build_sh.unlink() # so bash doesn't reload a modified file |
| 415 |
build_sh.write_text(sh_after, encoding="utf-8") |
| 416 |
build_sh.chmod(0o755) |
| 417 |
|
| 418 |
|
| 419 |
DJANGO_VERSIONS = { |
| 420 |
"4.2": "4.2.30", |
| 421 |
"5.2": "5.2.13", |
| 422 |
"6.0": "6.0.4", |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
def update_django_versions(): |
| 427 |
# https://endoflife.date/django makes it easier to track these |
| 428 |
releases = requests.get("https://endoflife.date/api/django.json").json() |
| 429 |
versions = {r["cycle"]: r["latest"] for r in releases[::-1] if TODAY <= r["eol"]} |
| 430 |
|
| 431 |
if versions == DJANGO_VERSIONS: |
| 432 |
return |
| 433 |
|
| 434 |
# Write the new mapping back to this file |
| 435 |
thisfile = pathlib.Path(__file__) |
| 436 |
before = thisfile.read_text(encoding="utf-8") |
| 437 |
after = re.sub( |
| 438 |
r"DJANGO_VERSIONS = \{[^{}]+\}", |
| 439 |
"DJANGO_VERSIONS = " + repr(versions).replace("}", ",}"), |
| 440 |
before, |
| 441 |
) |
| 442 |
thisfile.write_text(after, encoding="utf-8") |
| 443 |
pip_tool("shed", str(thisfile)) |
| 444 |
|
| 445 |
# Update the minimum version in pyproject.toml |
| 446 |
pyproject_toml = hp.BASE_DIR / "pyproject.toml" |
| 447 |
content = re.sub( |
| 448 |
r"django>=\d+\.\d+", |
| 449 |
f"django>={min(versions, key=float)}", |
| 450 |
pyproject_toml.read_text(encoding="utf-8"), |
| 451 |
) |
| 452 |
pyproject_toml.write_text(content, encoding="utf-8") |
| 453 |
|
| 454 |
# Automatically sync ci_version with the version in build.sh |
| 455 |
tox_ini = hp.BASE_DIR / "tox.ini" |
| 456 |
content = tox_ini.read_text(encoding="utf-8") |
| 457 |
print(versions) |
| 458 |
for short, full in versions.items(): |
| 459 |
content = re.sub( |
| 460 |
rf"django=={short}(\.\d+)?", |
| 461 |
rf"django=={full}", |
| 462 |
content, |
| 463 |
) |
| 464 |
tox_ini.write_text(content, encoding="utf-8") |
| 465 |
|
| 466 |
|
| 467 |
def update_pyodide_versions(): |
| 468 |
|
| 469 |
def version_tuple(v: str) -> tuple[int, int, int]: |
| 470 |
return tuple(int(x) for x in v.split(".")) # type: ignore |
| 471 |
|
| 472 |
vers_re = r"(\d+\.\d+\.\d+)" |
| 473 |
all_pyodide_build_versions = re.findall( |
| 474 |
f"pyodide_build-{vers_re}-py3-none-any.whl", # excludes pre-releases |
| 475 |
requests.get("https://pypi.org/simple/pyodide-build/").text, |
| 476 |
) |
| 477 |
pyodide_build_version = max( |
| 478 |
# Don't just pick the most recent version; find the highest stable version. |
| 479 |
set(all_pyodide_build_versions), |
| 480 |
key=version_tuple, |
| 481 |
) |
| 482 |
|
| 483 |
cross_build_environments_url = "https://raw.githubusercontent.com/pyodide/pyodide/refs/heads/main/pyodide-cross-build-environments.json" |
| 484 |
cross_build_environments_data = requests.get(cross_build_environments_url).json() |
| 485 |
|
| 486 |
# Find the latest stable release for the Pyodide runtime/xbuildenv that is compatible |
| 487 |
# with the pyodide-build version we found |
| 488 |
stable_releases = [ |
| 489 |
release |
| 490 |
for release in cross_build_environments_data["releases"].values() |
| 491 |
if re.fullmatch(vers_re, release["version"]) |
| 492 |
] |
| 493 |
|
| 494 |
compatible_releases = [] |
| 495 |
for release in stable_releases: # sufficiently large values |
| 496 |
min_build_version = release.get("min_pyodide_build_version", "0.0.0") |
| 497 |
max_build_version = release.get("max_pyodide_build_version", "999.999.999") |
| 498 |
|
| 499 |
# Perform version comparisons to avoid getting an incompatible pyodide-build version |
| 500 |
# with the Pyodide runtime |
| 501 |
if ( |
| 502 |
version_tuple(min_build_version) |
| 503 |
<= version_tuple(pyodide_build_version) |
| 504 |
<= version_tuple(max_build_version) |
| 505 |
): |
| 506 |
compatible_releases.append(release) |
| 507 |
|
| 508 |
if not compatible_releases: |
| 509 |
raise RuntimeError( |
| 510 |
f"No compatible Pyodide release found for pyodide-build {pyodide_build_version}" |
| 511 |
) |
| 512 |
|
| 513 |
pyodide_release = max( |
| 514 |
compatible_releases, |
| 515 |
key=lambda release: version_tuple(release["version"]), |
| 516 |
) |
| 517 |
|
| 518 |
pyodide_version = pyodide_release["version"] |
| 519 |
python_version = pyodide_release["python_version"] |
| 520 |
|
| 521 |
ci_file = tools.ROOT / ".github/workflows/main.yml" |
| 522 |
config = ci_file.read_text(encoding="utf-8") |
| 523 |
for name, var in [ |
| 524 |
("PYODIDE", pyodide_version), |
| 525 |
("PYODIDE_BUILD", pyodide_build_version), |
| 526 |
("PYTHON", python_version), |
| 527 |
]: |
| 528 |
config = re.sub(f"{name}_VERSION: {vers_re}", f"{name}_VERSION: {var}", config) |
| 529 |
ci_file.write_text(config, encoding="utf-8") |
| 530 |
|
| 531 |
|
| 532 |
def update_vendored_files(): |
| 533 |
vendor = pathlib.Path(hp.PYTHON_SRC) / "hypothesis" / "vendor" |
| 534 |
|
| 535 |
# Turns out that as well as adding new gTLDs, IANA can *terminate* old ones |
| 536 |
url = "http://data.iana.org/TLD/tlds-alpha-by-domain.txt" |
| 537 |
fname = vendor / url.split("/")[-1] |
| 538 |
new = requests.get(url).content |
| 539 |
# If only the timestamp in the header comment has changed, skip the update. |
| 540 |
if fname.read_bytes().splitlines()[1:] != new.splitlines()[1:]: |
| 541 |
fname.write_bytes(new) |
| 542 |
|
| 543 |
# Always require the most recent version of tzdata - we don't need to worry about |
| 544 |
# pre-releases because tzdata is a 'latest data' package (unlike pyodide-build). |
| 545 |
# Our crosshair extra is research-grade, so we require latest versions there too. |
| 546 |
pyproject_toml = pathlib.Path(hp.BASE_DIR, "pyproject.toml") |
| 547 |
new = pyproject_toml.read_text(encoding="utf-8") |
| 548 |
for pkgname in ("tzdata", "crosshair-tool", "hypothesis-crosshair"): |
| 549 |
pkg_url = f"https://pypi.org/pypi/{pkgname}/json" |
| 550 |
pkg_version = requests.get(pkg_url).json()["info"]["version"] |
| 551 |
new = re.sub(rf"{pkgname}>=([a-z0-9.]+)", f"{pkgname}>={pkg_version}", new) |
| 552 |
pyproject_toml.write_text(new, encoding="utf-8") |
| 553 |
|
| 554 |
|
| 555 |
def has_diff(file_or_directory): |
| 556 |
diff = ["git", "diff", "--no-patch", "--exit-code", "--", file_or_directory] |
| 557 |
return subprocess.call(diff) != 0 |
| 558 |
|
| 559 |
|
| 560 |
@task() |
| 561 |
def upgrade_requirements(): |
| 562 |
update_vendored_files() |
| 563 |
compile_requirements(upgrade=True) |
| 564 |
subprocess.call(["./build.sh", "format"], cwd=tools.ROOT) # exits 1 if changed |
| 565 |
if has_diff(hp.PYTHON_SRC) and not os.path.isfile(hp.RELEASE_FILE): |
| 566 |
msg = hp.get_autoupdate_message(domainlist_changed=has_diff(hp.DOMAINS_LIST)) |
| 567 |
with open(hp.RELEASE_FILE, mode="w", encoding="utf-8") as f: |
| 568 |
f.write(f"RELEASE_TYPE: patch\n\n{msg}") |
| 569 |
update_python_versions() |
| 570 |
update_pyodide_versions() |
| 571 |
update_django_versions() |
| 572 |
subprocess.call(["git", "add", "."], cwd=tools.ROOT) |
| 573 |
|
| 574 |
|
| 575 |
@task() |
| 576 |
def check_requirements(): |
| 577 |
compile_requirements(upgrade=False) |
| 578 |
|
| 579 |
|
| 580 |
@task(if_changed=hp.HYPOTHESIS_PYTHON) |
| 581 |
def documentation(): |
| 582 |
try: |
| 583 |
if hp.has_release(): |
| 584 |
hp.update_changelog_and_version() |
| 585 |
hp.build_docs() |
| 586 |
finally: |
| 587 |
subprocess.check_call( |
| 588 |
["git", "checkout", "docs/changelog.rst", "src/hypothesis/version.py"], |
| 589 |
cwd=hp.HYPOTHESIS_PYTHON, |
| 590 |
) |
| 591 |
|
| 592 |
|
| 593 |
@task() |
| 594 |
def website(): |
| 595 |
subprocess.call([sys.executable, "-m", "pelican"], cwd=tools.ROOT / "website") |
| 596 |
|
| 597 |
|
| 598 |
@task() |
| 599 |
def live_website(): |
| 600 |
subprocess.call( |
| 601 |
[sys.executable, "-m", "pelican", "--autoreload", "--listen"], |
| 602 |
cwd=tools.ROOT / "website", |
| 603 |
) |
| 604 |
|
| 605 |
|
| 606 |
@task() |
| 607 |
def live_docs(): |
| 608 |
pip_tool( |
| 609 |
"sphinx-autobuild", |
| 610 |
"docs", |
| 611 |
"docs/_build/html", |
| 612 |
"--watch", |
| 613 |
"src", |
| 614 |
"--open-browser", |
| 615 |
cwd=hp.HYPOTHESIS_PYTHON, |
| 616 |
) |
| 617 |
|
| 618 |
|
| 619 |
def run_tox(task, version, *args): |
| 620 |
python = install.python_executable(version) |
| 621 |
|
| 622 |
# Create a version of the name that tox will pick up for the correct |
| 623 |
# interpreter alias. |
| 624 |
linked_version = os.path.basename(python) + ALIASES[version] |
| 625 |
try: |
| 626 |
os.symlink(python, linked_version) |
| 627 |
except FileExistsError: |
| 628 |
pass |
| 629 |
|
| 630 |
env = dict(os.environ) |
| 631 |
python = install.python_executable(version) |
| 632 |
|
| 633 |
env["PATH"] = os.path.dirname(python) + ":" + env["PATH"] |
| 634 |
# Set environment variable for tox to use in basepython substitution |
| 635 |
if version.startswith("pypy"): |
| 636 |
# For PyPy, use the version name from e.g. "pypy3.11-7.3.20" |
| 637 |
# to match tox's environment name inference. |
| 638 |
env["TOX_PYTHON_VERSION"] = version.split("-")[0] # "pypy3.11" |
| 639 |
else: |
| 640 |
env["TOX_PYTHON_VERSION"] = ALIASES[version] # "python3.12" |
| 641 |
print(env["PATH"]) |
| 642 |
|
| 643 |
pip_tool("tox", "-e", task, *args, env=env, cwd=hp.HYPOTHESIS_PYTHON) |
| 644 |
|
| 645 |
|
| 646 |
# update_python_versions(), above, keeps the contents of this dict up to date. |
| 647 |
# When a version is added or removed, manually update the env lists in tox.ini and |
| 648 |
# workflows/main.yml, and the `Programming Language ::` specifiers in pyproject.toml |
| 649 |
PYTHONS = { |
| 650 |
"3.10": "3.10.20", |
| 651 |
"3.11": "3.11.15", |
| 652 |
"3.12": "3.12.13", |
| 653 |
"3.13": "3.13.13", |
| 654 |
"3.13t": "3.13t-dev", |
| 655 |
"3.14": "3.14.4", |
| 656 |
"3.14t": "3.14t-dev", |
| 657 |
"3.15": "3.15.0a8", |
| 658 |
"3.15t": "3.15t-dev", |
| 659 |
"pypy3.10": "pypy3.10-7.3.19", |
| 660 |
"pypy3.11": "pypy3.11-7.3.20", |
| 661 |
} |
| 662 |
ci_version = "3.14" # Keep this in sync with GH Actions main.yml and .readthedocs.yml |
| 663 |
|
| 664 |
python_tests = task( |
| 665 |
if_changed=( |
| 666 |
hp.PYTHON_SRC, |
| 667 |
hp.PYTHON_TESTS, |
| 668 |
hp.HYPOTHESIS_PYTHON / "pyproject.toml", |
| 669 |
tools.ROOT / "tooling", |
| 670 |
hp.HYPOTHESIS_PYTHON / "scripts", |
| 671 |
) |
| 672 |
) |
| 673 |
|
| 674 |
|
| 675 |
# ALIASES are the executable names for each Python version |
| 676 |
ALIASES = {} |
| 677 |
for key, version in PYTHONS.items(): |
| 678 |
if key.startswith("pypy"): |
| 679 |
ALIASES[version] = "pypy3" |
| 680 |
name = key.replace(".", "") |
| 681 |
else: |
| 682 |
ALIASES[version] = f"python{key}" |
| 683 |
name = f"py3{key[2:]}" |
| 684 |
TASKS[f"check-{name}"] = python_tests( |
| 685 |
lambda n=f"{name}-full", v=version, *args: run_tox(n, v, *args) |
| 686 |
) |
| 687 |
for subtask in ( |
| 688 |
"brief", |
| 689 |
"full", |
| 690 |
"cover", |
| 691 |
"rest", |
| 692 |
"nocover", |
| 693 |
"niche", |
| 694 |
"custom", |
| 695 |
): |
| 696 |
TASKS[f"check-{name}-{subtask}"] = python_tests( |
| 697 |
lambda n=f"{name}-{subtask}", v=version, *args: run_tox(n, v, *args) |
| 698 |
) |
| 699 |
|
| 700 |
|
| 701 |
@python_tests |
| 702 |
def check_py310_pyjion(*args): |
| 703 |
run_tox("py310-pyjion", PYTHONS["3.10"], *args) |
| 704 |
|
| 705 |
|
| 706 |
@task() |
| 707 |
def tox(*args): |
| 708 |
if len(args) < 2: |
| 709 |
print("Usage: ./build.sh tox TOX_ENV PY_VERSION [tox args]") |
| 710 |
sys.exit(1) |
| 711 |
run_tox(*args) |
| 712 |
|
| 713 |
|
| 714 |
def standard_tox_task(name, py=ci_version): |
| 715 |
TASKS["check-" + name] = python_tests( |
| 716 |
lambda *args: run_tox(name, PYTHONS.get(py, py), *args) |
| 717 |
) |
| 718 |
|
| 719 |
|
| 720 |
standard_tox_task("py311-pytest62", py="3.11") # hits "ast.Str is deprecated" in 3.12+ |
| 721 |
standard_tox_task("pytest74") |
| 722 |
standard_tox_task("pytest84") |
| 723 |
standard_tox_task("pytest9") |
| 724 |
|
| 725 |
dj_version = max(ci_version, "3.12") |
| 726 |
for n in DJANGO_VERSIONS: |
| 727 |
standard_tox_task(f"django{n.replace('.', '')}", py=dj_version) |
| 728 |
# we also test no-contrib on the latest django version |
| 729 |
standard_tox_task("django-nocontrib", py=dj_version) |
| 730 |
|
| 731 |
# test each pandas version with the latest python version they support |
| 732 |
standard_tox_task("py310-pandas11", py="3.10") |
| 733 |
standard_tox_task("py310-pandas12", py="3.10") |
| 734 |
standard_tox_task("py310-pandas13", py="3.10") |
| 735 |
standard_tox_task("py310-pandas14", py="3.10") |
| 736 |
standard_tox_task("py311-pandas15", py="3.11") |
| 737 |
standard_tox_task("py311-pandas20", py="3.11") |
| 738 |
standard_tox_task("py312-pandas21", py="3.12") |
| 739 |
standard_tox_task("py313-pandas22", py="3.13") |
| 740 |
|
| 741 |
for kind in ("cover", "nocover", "niche", "custom"): |
| 742 |
standard_tox_task(f"crosshair-{kind}") |
| 743 |
|
| 744 |
for kind in ("rest", "nocover"): |
| 745 |
# Note, in CI these are executed on alternative platforms (e.g., windows) |
| 746 |
# directly in tox (and not via build.sh) |
| 747 |
standard_tox_task(f"alt-{kind}") |
| 748 |
|
| 749 |
standard_tox_task("threading") |
| 750 |
standard_tox_task("py310-oldestnumpy", py="3.10") |
| 751 |
standard_tox_task("numpy-nightly", py="3.12") |
| 752 |
|
| 753 |
standard_tox_task("coverage") |
| 754 |
standard_tox_task("conjecture-coverage") |
| 755 |
standard_tox_task("snapshots") |
| 756 |
|
| 757 |
|
| 758 |
@task() |
| 759 |
def check_quality(*args): |
| 760 |
run_tox("quality", PYTHONS[ci_version], *args) |
| 761 |
|
| 762 |
|
| 763 |
@task(if_changed=(hp.PYTHON_SRC, os.path.join(hp.HYPOTHESIS_PYTHON, "examples"))) |
| 764 |
def check_examples3(*args): |
| 765 |
run_tox("examples3", PYTHONS[ci_version], *args) |
| 766 |
|
| 767 |
|
| 768 |
@task() |
| 769 |
def check_whole_repo_tests(*args): |
| 770 |
install.ensure_shellcheck() |
| 771 |
subprocess.check_call( |
| 772 |
[sys.executable, "-m", "pip", "install", "--upgrade", hp.HYPOTHESIS_PYTHON] |
| 773 |
) |
| 774 |
|
| 775 |
if not args: |
| 776 |
args = ["-n", "auto", tools.REPO_TESTS / "whole_repo"] |
| 777 |
subprocess.check_call([sys.executable, "-m", "pytest", *args]) |
| 778 |
|
| 779 |
|
| 780 |
@task() |
| 781 |
def check_documentation(*args): |
| 782 |
install.ensure_shellcheck() |
| 783 |
subprocess.check_call( |
| 784 |
[sys.executable, "-m", "pip", "install", "--upgrade", hp.HYPOTHESIS_PYTHON] |
| 785 |
) |
| 786 |
|
| 787 |
if not args: |
| 788 |
args = ["-n", "auto", tools.REPO_TESTS / "documentation"] |
| 789 |
subprocess.check_call([sys.executable, "-m", "pytest", *args]) |
| 790 |
|
| 791 |
|
| 792 |
@task() |
| 793 |
def check_types(*args): |
| 794 |
install.ensure_shellcheck() |
| 795 |
subprocess.check_call( |
| 796 |
[sys.executable, "-m", "pip", "install", "--upgrade", hp.HYPOTHESIS_PYTHON] |
| 797 |
) |
| 798 |
|
| 799 |
if not args: |
| 800 |
args = ["-n", "auto", tools.REPO_TESTS / "types"] |
| 801 |
subprocess.check_call([sys.executable, "-m", "pytest", *args]) |
| 802 |
|
| 803 |
|
| 804 |
@task() |
| 805 |
def check_types_api(*args): |
| 806 |
install.ensure_shellcheck() |
| 807 |
subprocess.check_call( |
| 808 |
[sys.executable, "-m", "pip", "install", "--upgrade", hp.HYPOTHESIS_PYTHON] |
| 809 |
) |
| 810 |
|
| 811 |
if not args: |
| 812 |
ignore = ["--ignore", tools.REPO_TESTS / "types/test_hypothesis.py"] |
| 813 |
args = ["-n", "auto", tools.REPO_TESTS / "types"] + ignore |
| 814 |
subprocess.check_call([sys.executable, "-m", "pytest", *args]) |
| 815 |
|
| 816 |
|
| 817 |
@task() |
| 818 |
def check_types_hypothesis(*args): |
| 819 |
install.ensure_shellcheck() |
| 820 |
subprocess.check_call( |
| 821 |
[sys.executable, "-m", "pip", "install", "--upgrade", hp.HYPOTHESIS_PYTHON] |
| 822 |
) |
| 823 |
|
| 824 |
if not args: |
| 825 |
testcase = "types/test_hypothesis.py" |
| 826 |
args = ["-n", "auto", tools.REPO_TESTS / testcase] |
| 827 |
subprocess.check_call([sys.executable, "-m", "pytest", *args]) |
| 828 |
|
| 829 |
|
| 830 |
@task() |
| 831 |
def shell(): |
| 832 |
import IPython |
| 833 |
|
| 834 |
IPython.start_ipython([]) |
| 835 |
|
| 836 |
|
| 837 |
@task() |
| 838 |
def python(*args): |
| 839 |
os.execv(sys.executable, (sys.executable, *args)) |
| 840 |
|
| 841 |
|
| 842 |
@task() |
| 843 |
def tasks(): |
| 844 |
"""Print a list of all task names supported by the build system.""" |
| 845 |
for task_name in sorted(TASKS.keys()): |
| 846 |
print(" " + task_name) |
| 847 |
|
| 848 |
|
| 849 |
if __name__ == "__main__": |
| 850 |
if "SNAKEPIT" not in os.environ: |
| 851 |
print( |
| 852 |
"This module should not be executed directly, but instead via " |
| 853 |
"build.sh (which sets up its environment)" |
| 854 |
) |
| 855 |
sys.exit(1) |
| 856 |
|
| 857 |
if len(sys.argv) > 1: |
| 858 |
task_to_run = sys.argv[1] |
| 859 |
args = sys.argv[2:] |
| 860 |
else: |
| 861 |
task_to_run = os.environ.get("TASK") |
| 862 |
args = () |
| 863 |
|
| 864 |
if task_to_run is None: |
| 865 |
print( |
| 866 |
"No task specified. Either pass the task to run as an " |
| 867 |
"argument or as an environment variable TASK. " |
| 868 |
'(Use "./build.sh tasks" to list all supported task names.)' |
| 869 |
) |
| 870 |
sys.exit(1) |
| 871 |
|
| 872 |
if task_to_run not in TASKS: |
| 873 |
print(f"\nUnknown task {task_to_run!r}. Available tasks are:") |
| 874 |
tasks() |
| 875 |
sys.exit(1) |
| 876 |
|
| 877 |
try: |
| 878 |
TASKS[task_to_run](*args) |
| 879 |
except subprocess.CalledProcessError as e: |
| 880 |
sys.exit(e.returncode) |