Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .template_sync/manifest.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
template_version: 0.1.43
template_version: 0.1.44
migrations:
- from: 0.0.0
to: 0.0.1
Expand Down Expand Up @@ -135,3 +135,6 @@ migrations:
- from: 0.1.42
to: 0.1.43
script: migrations/v0_1_42_to_v0_1_43.py
- from: 0.1.43
to: 0.1.44
script: migrations/v0_1_43_to_v0_1_44.py
91 changes: 91 additions & 0 deletions .template_sync/migrations/v0_1_43_to_v0_1_44.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
"""Template 0.1.43 -> 0.1.44: a lock file keeps the git references the requirements write (tags), so pip can install apps that depend on
MorphoBricks bricks.

Design note: https://github.com/CellMigrationLab/LabConstrictor/blob/main/.tools/docs/migration_0_1_44.md

lock_requirements.py wrote uv's resolution, in which `name @ git+URL@v1.2.0` becomes `name @ git+URL@<commit>`. pip compares git URLs as
text: when another git package requires the same package with the tag (morphobricks-preprocessing requires morphobricks-normalization
`@v0.10.0`), pip refused the lock file (ResolutionImpossible), the conda check of the workflow "Updates on notebook change" failed and the
app's commit was reverted. Now a git package the requirements name directly keeps the reference as written, with the resolved commit as a
comment. LOCK_FORMAT 3 writes every lock file again at the next run of the requirements workflow.

What this changes in an app repository: lock_requirements.py is replaced by the 0.1.44 file when it is still the template file last shipped
(the 0.1.43 copy; compared with LF line endings; the file keeps its own line endings). A file the app changed itself stays as it is
(SKIPPED, with a note of what to change by hand). A missing file is not created, and nothing is deleted. The lock files change the next
time the requirements workflow runs (a notebook change, or Actions > Updates on notebook change > Run workflow).
"""
from __future__ import annotations

from pathlib import Path
from typing import Any

DOCS_URL = "https://github.com/CellMigrationLab/LabConstrictor/blob/main/.tools/docs/migration_0_1_44.md"
TAG = "[migration 0.1.44]"
RESOURCE_ROOT = Path(__file__).resolve().parents[1] / "resources" / "v0_1_43_to_v0_1_44"
PREVIOUS_ROOT = RESOURCE_ROOT / "previous"

LOCK = Path(".tools/python/lock_requirements.py")
FILES = (LOCK,)

BY_HAND = {
LOCK: (
"after uv's resolution, write each git package that the requirements file names directly with the URL it writes (the tag), as "
"keep_requested_git_references of the template's script does, and set LOCK_FORMAT to \"3\""
),
}


def log(message: str) -> None:
print(f"{TAG} {message}")


def lf_text(path: Path) -> str:
return path.read_bytes().decode("utf-8").replace("\r\n", "\n")


def replaced_file(relative: Path, text_lf: str) -> tuple[str, str]:
"""The 0.1.44 text of `relative` for the app's `text_lf`, and what was done. Unchanged when it is not the 0.1.43 template file."""
new = lf_text(RESOURCE_ROOT / relative)
if text_lf == new:
return text_lf, "already up to date"
if text_lf == lf_text(PREVIOUS_ROOT / relative):
return new, "replaced by the 0.1.44 file"
return text_lf, (
"SKIPPED: customised (not the 0.1.43 template file), so it was left unchanged; compare it with the template's file. "
f"By hand: {BY_HAND[relative]}. See {DOCS_URL}"
)


def update(path: Path, relative: Path) -> str:
data = path.read_bytes().decode("utf-8")
newline = "\r\n" if "\r\n" in data else "\n"
original = data.replace("\r\n", "\n")
text, note = replaced_file(relative, original)
if text == original:
return note if note.startswith("SKIPPED") else f"unchanged: {note}"
path.write_bytes(text.replace("\n", newline).encode("utf-8"))
return f"updated: {note}"


def migrate(repo_root: Path, context: dict[str, Any]) -> None:
"""Apply the 0.1.44 change (idempotent, never raises for a customised file, creates and deletes nothing)."""
_ = context
changed = 0
for relative in FILES:
path = repo_root / relative
if not path.is_file():
log(f"{relative}: not present in this repository (nothing was created)")
continue
try:
status = update(path, relative)
except (OSError, UnicodeDecodeError) as exc:
status = f"SKIPPED: {exc}; left unchanged. See {DOCS_URL}"
changed += status.startswith("updated")
log(f"{relative}: {status}")
if changed:
log(
f"done: {changed} file(s) changed. The lock files change at the next run of the workflow "
"Updates on notebook change (a notebook change, or run it by hand); the changes reach users with the app's next release."
)
else:
log("done: no repository changes were required.")
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
#!/usr/bin/env python3
"""Lock the merged requirements files: requirements*.txt -> requirements*.lock.txt.

merge_requirements.py writes the requirements of the notebooks (requirements.txt, requirements_gpu.txt and the per-OS
requirements-<os>.txt). Those files name the packages the notebooks need, but not the packages these depend on, so two
installations of the same release could get different versions. This script writes, next to each file, a lock file that lists
every package pip installs, each with its exact version, and post_install installs the lock file when the installer bundles one.

How a lock file is made:
* `uv pip compile --universal`: one lock file that is valid on Windows, macOS and Linux (a package that only one platform needs carries
an environment marker, e.g. `colorama==0.4.6 ; sys_platform == 'win32'`).
* `--python-version` is the Python of environment.yaml. uv reads it as the lowest Python of the resolution, so a few packages can appear
twice with a `python_full_version` marker: pip installs the line that matches the Python of the installer.
* The conda packages of environment.yaml (jupyterlab, pip, setuptools, certifi, ...) are installed by the installer itself, so they are
not written to the lock file (their own dependencies are), and a conda pin such as `jupyterlab=4.4.0` constrains the resolution.
* The per-OS files are resolved with requirements.lock.txt as constraints, so a package that both files need gets the same version.
* Pip options of the requirements file (`--extra-index-url` of the GPU file, ...) are copied to the top of the lock file.
* A package required from a git URL (`name @ git+https://...@v1.2.0`) keeps the reference exactly as the requirements file writes it, with
the commit uv resolved as a comment. uv writes the commit instead, and pip treats `...@v1.2.0` and `...@<commit>` as two different
requirements: when another git package requires the same package with the tag (MorphoBricks bricks depend on each other this way), pip
refuses the lock file (ResolutionImpossible) and the installer cannot install it.

A lock file is written again only when its requirements file, the Python or JupyterLab version (or this script's lock format) changed:
its header holds a fingerprint of those inputs. Re-running the script without such a change rewrites nothing, so a new release
installs the same versions as the previous one. To move every package to its newest version, delete the lock files and run the
workflow "Updates on notebook change" by hand (Actions > Run workflow).

If a file cannot be locked (uv is missing, or the requirements cannot be resolved), the script prints a warning, removes the
out-of-date lock file of that file (an installer must never install versions that no longer match the requirements) and goes on:
the installer then installs the plain requirements file. The script itself always exits 0 for such a failure.
"""
from __future__ import annotations

import argparse
import hashlib
import re
import subprocess
import sys
import tempfile
from pathlib import Path

import yaml

# Bump when the uv arguments or the output change, so that every lock file is written again.
LOCK_FORMAT = "3"
BASE_INPUT = "requirements.txt"
GPU_INPUT = "requirements_gpu.txt"
OS_INPUTS = ("requirements-linux.txt", "requirements-windows.txt", "requirements-macos.txt")
INPUTS = (BASE_INPUT, GPU_INPUT, *OS_INPUTS)
FINGERPRINT_PREFIX = "# lock-input: sha256:"
# Pip options that choose where packages come from: copied to the top of the lock file, so pip finds the locked versions where uv found them.
INDEX_OPTION = re.compile(r"^\s*(--index-url|--extra-index-url|-i|--find-links|-f|--trusted-host)(\s|=)")
CONDA_SPEC = re.compile(r"^\s*([A-Za-z0-9_.-]+)\s*(?:(==?)\s*([0-9][A-Za-z0-9_.+!*-]*))?(?:=\S+)?\s*$")
PLACEHOLDER = re.compile(r"^[A-Z_]+$")
# `name[extras] @ git+URL`, the URL up to the first space or `;` (environment marker).
GIT_REQUIREMENT = re.compile(r"^\s*([A-Za-z0-9][A-Za-z0-9._-]*)(\[[^\]]*\])?\s*@\s*(git\+[^\s;#]+)")


def lock_path(requirements: Path) -> Path:
"""requirements.txt -> requirements.lock.txt (same folder)."""
return requirements.with_name(requirements.stem + ".lock.txt")


def read_environment(path: Path) -> tuple[str | None, list[str], list[str]]:
"""(Python version, conda package names, pip constraints from exact conda pins) of environment.yaml."""
if not path.is_file():
return None, [], []
data = yaml.safe_load(path.read_text(encoding="utf-8")) or {}
python_version = None
names: list[str] = []
constraints: list[str] = []
for dependency in data.get("dependencies", []) or []:
if not isinstance(dependency, str):
continue # the `pip:` subsection of an environment file
match = CONDA_SPEC.match(dependency)
if match:
name, operator, version = match.group(1), match.group(2), match.group(3)
else:
# a spec with a range or alternatives (`pip>=24.2`): only its name is used
name, operator, version = re.split(r"[\s<>=!|,\[]", dependency.strip(), maxsplit=1)[0], None, None
if not name:
continue
if version and PLACEHOLDER.match(version):
version = None # the template itself (python=PYTHON_VERSION)
if name.lower() == "python":
python_version = version
continue
names.append(name)
if version:
# conda `name=1.2` means any 1.2.* release, `name==1.2` exactly 1.2
constraints.append(f"{name}=={version}" if operator == "==" else f"{name}=={version.removesuffix('.*')}.*")
return python_version, sorted(set(names), key=str.lower), constraints


def normalized_requirements(text: str) -> str:
"""The requirement and option lines of a requirements file (comments, such as the timestamp of merge_requirements.py, are ignored)."""
lines = []
for line in text.replace("\r\n", "\n").split("\n"):
stripped = line.strip()
if stripped and not stripped.startswith("#"):
lines.append(stripped)
return "\n".join(lines)


def fingerprint(requirements_text: str, python_version: str, constraints: list[str], no_emit: list[str], extra_constraints: str) -> str:
payload = "\n---\n".join(
[LOCK_FORMAT, normalized_requirements(requirements_text), python_version, "\n".join(constraints), "\n".join(no_emit), extra_constraints]
)
return hashlib.sha256(payload.encode("utf-8")).hexdigest()


def current_fingerprint(lock: Path) -> str | None:
if not lock.is_file():
return None
for line in lock.read_text(encoding="utf-8").splitlines():
if line.startswith(FINGERPRINT_PREFIX):
return line[len(FINGERPRINT_PREFIX):].strip()
return None


def run_uv(requirements: Path, python_version: str, constraint_files: list[Path], no_emit: list[str]) -> str:
"""The resolution of `requirements` (stdout of uv pip compile); raises RuntimeError with uv's message when it fails."""
command = [
sys.executable, "-m", "uv", "pip", "compile", str(requirements),
"--universal", "--python-version", python_version,
"--no-header", "--no-annotate", "--no-config", "--quiet",
# With an extra index (the PyTorch CPU or CUDA index), take each package at the best version of all indexes. uv's default
# takes a package from the first index that has it, so packages that the PyTorch index also hosts (torchmetrics, numpy, idna,
# ...) were locked at its older copies, and a pin of a newer version made the lock fail.
"--index-strategy", "unsafe-best-match",
]
for constraint in constraint_files:
command += ["--constraint", str(constraint)]
for name in no_emit:
command += ["--no-emit-package", name]
try:
result = subprocess.run(command, capture_output=True, text=True, encoding="utf-8", check=False)
except OSError as exc:
raise RuntimeError(f"uv could not be started: {exc}") from exc
if result.returncode != 0:
message = (result.stderr or result.stdout).strip() or f"exit code {result.returncode}"
raise RuntimeError(message)
return result.stdout


def uv_version() -> str:
try:
result = subprocess.run([sys.executable, "-m", "uv", "--version"], capture_output=True, text=True, check=False)
except OSError:
return "uv"
# "uv 0.13.3 (502f93ca6 2026-10-19 x86_64-pc-windows-msvc)" -> "uv 0.13.3": the build details differ between computers
return " ".join(result.stdout.split()[:2]) or "uv"


def canonical_name(name: str) -> str:
"""The package name as pip compares it (PEP 503): lower case, runs of `-`, `_` and `.` as one `-`."""
return re.sub(r"[-_.]+", "-", name).lower()


def keep_requested_git_references(requirements_text: str, packages: list[str]) -> list[str]:
"""`packages` (uv's lines) with each git package the requirements name directly written with the requested reference.

uv resolves `name @ git+URL@v1.2.0` to `name @ git+URL@<commit>`. pip compares the two URLs as text, so a lock line with the commit
conflicts with another git package that requires `name` with the tag. The lock line keeps the requested URL; the resolved commit
follows as a comment. A git package the requirements do not name directly (a dependency of a dependency) keeps uv's line.
"""
requested: dict[str, str] = {}
for line in requirements_text.replace("\r\n", "\n").split("\n"):
match = GIT_REQUIREMENT.match(line)
if match:
requested[canonical_name(match.group(1))] = match.group(3)
kept = []
for line in packages:
match = GIT_REQUIREMENT.match(line)
url = requested.get(canonical_name(match.group(1))) if match else None
if match and url and url != match.group(3):
start, end = match.span(3)
line = f"{line[:start]}{url}{line[end:]} # resolved: {match.group(3)}"
kept.append(line)
return kept


def render_lock(requirements: Path, resolved: str, digest: str, python_version: str, uv: str) -> str:
text = requirements.read_text(encoding="utf-8")
options = [line.strip() for line in text.splitlines() if INDEX_OPTION.match(line)]
packages = [line.rstrip() for line in resolved.replace("\r\n", "\n").split("\n") if line.strip() and not line.lstrip().startswith("#")]
packages = keep_requested_git_references(text, packages)
header = [
f"# Lock file of {requirements.name}: every package the installer installs with pip, with its exact version.",
f"# Written by .tools/python/lock_requirements.py ({uv}, universal resolution for Python {python_version}); do not edit it by hand.",
f"# It is written again only when {requirements.name}, the Python or JupyterLab version changes (see the fingerprint below).",
f"{FINGERPRINT_PREFIX}{digest}",
]
body = options + ([""] if options else []) + packages
return "\n".join(header + [""] + body) + "\n"


def warn(message: str) -> None:
# `::warning::` shows the message on the summary page of the GitHub Actions run.
print(f"::warning::{message}", flush=True)


def lock_file(
requirements: Path,
python_version: str,
constraints: list[str],
no_emit: list[str],
base_lock: Path | None,
work_dir: Path,
uv: str,
) -> str:
"""Lock one requirements file; returns what happened ("unchanged", "written", "failed")."""
lock = lock_path(requirements)
extra = base_lock.read_text(encoding="utf-8") if base_lock is not None and base_lock.is_file() else ""
digest = fingerprint(requirements.read_text(encoding="utf-8"), python_version, constraints, no_emit, normalized_requirements(extra))
if current_fingerprint(lock) == digest:
print(f"{lock}: up to date (unchanged)")
return "unchanged"
constraint_files = []
if constraints:
conda_constraints = work_dir / "conda_constraints.txt"
conda_constraints.write_text("\n".join(constraints) + "\n", encoding="utf-8")
constraint_files.append(conda_constraints)
if extra:
# Only the package lines: the pip options of the base lock are not constraints.
base_constraints = work_dir / "base_constraints.txt"
base_constraints.write_text(
"\n".join(line for line in extra.splitlines() if line.strip() and not line.startswith(("#", "-"))) + "\n", encoding="utf-8"
)
constraint_files.append(base_constraints)
try:
resolved = run_uv(requirements, python_version, constraint_files, no_emit)
except RuntimeError as exc:
warn(f"{requirements} could not be locked, so the installer installs it without a lock file: {exc}")
if lock.exists():
lock.unlink()
print(f"{lock}: removed (it no longer matches {requirements})")
return "failed"
with lock.open("w", encoding="utf-8", newline="\n") as handle:
handle.write(render_lock(requirements, resolved, digest, python_version, uv))
print(f"{lock}: written")
return "written"


def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description="Lock requirements*.txt into requirements*.lock.txt (every package with its exact version).")
parser.add_argument("--root", type=Path, default=Path("."), help="Folder with the requirements files and environment.yaml")
parser.add_argument("--environment", type=Path, default=None, help="Conda environment file (default: <root>/environment.yaml)")
args = parser.parse_args(argv)
root: Path = args.root
environment = args.environment or root / "environment.yaml"

python_version, conda_names, constraints = read_environment(environment)
if not python_version:
print(f"{environment}: no exact Python version (python=X.Y.Z), so nothing is locked.")
return 0
no_emit = [name for name in conda_names if name.lower() not in {"git"}]

# A lock file whose requirements file is gone is removed (the installer must not install it).
for name in INPUTS:
requirements = root / name
if not requirements.is_file() and lock_path(requirements).is_file():
lock_path(requirements).unlink()
print(f"{lock_path(requirements)}: removed ({requirements} does not exist)")

present = [root / name for name in INPUTS if (root / name).is_file()]
if not present:
print("No requirements files found: nothing to lock.")
return 0
uv = uv_version()
results = {}
with tempfile.TemporaryDirectory() as temporary:
work_dir = Path(temporary)
for requirements in present:
base_lock = lock_path(root / BASE_INPUT) if requirements.name in OS_INPUTS else None
results[requirements.name] = lock_file(requirements, python_version, constraints, no_emit, base_lock, work_dir, uv)
failed = [name for name, status in results.items() if status == "failed"]
print(f"Lock files: {len(results) - len(failed)} of {len(results)} requirements files locked" + (f"; not locked: {', '.join(failed)}" if failed else ""))
return 0


if __name__ == "__main__":
sys.exit(main())
Loading
Loading