diff --git a/dev/scripts/gen_config.py b/dev/scripts/gen_config.py index c9743cb..387e8fc 100644 --- a/dev/scripts/gen_config.py +++ b/dev/scripts/gen_config.py @@ -185,7 +185,7 @@ def check_modules() -> list[str]: ) opt_deps = set(pyproject.get("project", {}).get("optional-dependencies", {}).keys()) # Groups that are aggregates or cloud providers, not Python modules - meta_groups = {"all", "lake", "dev", "aws", "gcp", "azure"} + meta_groups = {"all", "lake", "dev", "aws", "gcp", "azure", "notebooks"} errors = [] for mod in sorted(module_names): diff --git a/infra/images/notebooks.Dockerfile b/infra/images/notebooks.Dockerfile index 6e8322a..73be3f8 100644 --- a/infra/images/notebooks.Dockerfile +++ b/infra/images/notebooks.Dockerfile @@ -72,43 +72,16 @@ RUN uv build --wheel # ---- stage 2b: notebook requirements ---------------------------------------- # # Notebooks import src/ (bind-mounted, on PYTHONPATH), so the image needs the -# stack's base dependencies plus pyproject's `notebooks` extra. Expand its -# stack[...] self-references into plain requirements here; the heavy install -# layer below then rebuilds only when that list changes, not on every -# pyproject edit. `uv add` keeps just the last spec it sees per package, so -# repeated names (duckdb comes from four extras) fold into one requirement. +# stack's base dependencies plus pyproject's `notebooks` extra, expanded by +# infra/images/notebooks/notebook_requirements.py. Generating the list in +# its own stage means the heavy install layer below rebuilds only when the +# list changes, not on every pyproject edit. (A script file, not a RUN +# heredoc: hadolint can't parse heredocs.) FROM ghcr.io/astral-sh/uv:0.13.2-python3.13-trixie-slim AS nbreqs -COPY pyproject.toml /pyproject.toml -RUN python3 - > /notebooks-requirements.txt <<'PY' -import re, tomllib - -project = tomllib.load(open("/pyproject.toml", "rb"))["project"] -extras = project["optional-dependencies"] -reqs, seen = list(project["dependencies"]), set() - -def walk(extra): - if extra in seen: - return - seen.add(extra) - for req in extras[extra]: - if m := re.fullmatch(r"stack\[([\w,-]+)\]", req): - for sub in m.group(1).split(","): - walk(sub) - else: - reqs.append(req) - -walk("notebooks") -merged = {} -for req in reqs: - name, ext, spec = re.fullmatch(r"([\w.-]+)(?:\[([^\]]*)\])?\s*([^;]*)", req).groups() - _, e, s = merged.setdefault(re.sub(r"[-_.]+", "-", name).lower(), [name, set(), []]) - e.update(x.strip() for x in (ext or "").split(",") if x.strip()) - s.extend(c.strip() for c in spec.split(",") if c.strip() and c.strip() not in s) -for name, e, s in merged.values(): - print(name + (f"[{','.join(sorted(e))}]" if e else "") + ",".join(s)) -PY -RUN cat /notebooks-requirements.txt +COPY pyproject.toml infra/images/notebooks/notebook_requirements.py / +RUN python3 /notebook_requirements.py /pyproject.toml > /notebooks-requirements.txt \ + && cat /notebooks-requirements.txt # ---- stage 3: runtime ------------------------------------------------------- diff --git a/infra/images/notebooks/notebook_requirements.py b/infra/images/notebooks/notebook_requirements.py new file mode 100644 index 0000000..6b84b44 --- /dev/null +++ b/infra/images/notebooks/notebook_requirements.py @@ -0,0 +1,42 @@ +"""Print the notebooks image's requirements from pyproject.toml. + +The base dependencies plus the `notebooks` extra, with its stack[...] +self-references expanded into plain requirements. `uv add` keeps just the +last spec it sees per package, so repeated names (duckdb comes from four +extras) fold into one requirement with every clause kept. + + python3 notebook_requirements.py pyproject.toml > requirements.txt +""" + +import re +import sys +import tomllib + +project = tomllib.load(open(sys.argv[1], "rb"))["project"] +extras = project["optional-dependencies"] +reqs, seen = list(project["dependencies"]), set() + + +def walk(extra: str) -> None: + if extra in seen: + return + seen.add(extra) + for req in extras[extra]: + if m := re.fullmatch(r"stack\[([\w,-]+)\]", req): + for sub in m.group(1).split(","): + walk(sub) + else: + reqs.append(req) + + +walk("notebooks") +merged: dict[str, list] = {} +for req in reqs: + name, ext, spec = re.fullmatch( + r"([\w.-]+)(?:\[([^\]]*)\])?\s*([^;]*)", req + ).groups() + _, e, s = merged.setdefault(re.sub(r"[-_.]+", "-", name).lower(), [name, set(), []]) + e.update(x.strip() for x in (ext or "").split(",") if x.strip()) + s.extend(c.strip() for c in spec.split(",") if c.strip() and c.strip() not in s) +for name, e, s in merged.values(): + print(name + (f"[{','.join(sorted(e))}]" if e else "") + ",".join(s))