mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-28 22:06:31 +00:00
perf(image): bake the lint toolchain into locally built images
A new lint-docker stage runs utils.install.lint for every all-mode group that installs outside the source tree; the full stage builds on lint-${INFINITO_LINT_RUNNER}. default.env selects docker for developer machines, and the new lint_runner env handler pins host on GitHub Actions and act, so CI images stay without the toolchain. The Dockerfile keeps host as its ARG default because the CI image build runs push.sh from github.sha against the checked-out context, so published images stay unchanged as well. make install-lint inside the container drops from 173.97 s to 0.533 s. tests/utils/image_slice.py moves the Dockerfile slicing out of test_collections so the new bake test reads the same instructions.
🎯 Fix: 88 %
💥 Break: 8 %
💸 Cost: –
⏱️ Time: –
🛡️ Security: ~100 %
🏆 Score: 81
🚦 Gate: ⛔ fix 88 % < 90, break 8 % > 5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3fdfce419e
commit
452abb5c02
12 files changed
+257
-43
No files matched your search
@@ -2,15 +2,33 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
import functools
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
|
||||
from tests.utils import image_slice
|
||||
from utils.cache.files import read_text
|
||||
from utils.install.lint import __main__ as cli
|
||||
|
||||
_IN_TREE_GROUPS = frozenset({"javascript", "playwright"})
|
||||
_BAKE = re.compile(r"^RUN .*-m utils\.install\.lint (?P<groups>[a-z ]+)$")
|
||||
|
||||
|
||||
def _baked_groups() -> list[str]:
|
||||
for line in read_text(str(image_slice.DOCKERFILE)).splitlines():
|
||||
match = _BAKE.match(line.strip())
|
||||
if match:
|
||||
return match.group("groups").split()
|
||||
return []
|
||||
|
||||
|
||||
def _make_repo_root(tmp: str, *, stamp_age_offset: float | None = None) -> Path:
|
||||
root = Path(tmp)
|
||||
@@ -203,5 +221,69 @@ class TestGroupDispatch(unittest.TestCase):
|
||||
self.assertEqual(rc, 1)
|
||||
|
||||
|
||||
class TestImageBake(unittest.TestCase):
|
||||
def test_the_lint_stage_bakes_every_all_mode_group_outside_the_tree(self) -> None:
|
||||
called: list[str] = []
|
||||
with contextlib.ExitStack() as stack:
|
||||
for group, name in cli._GROUP_FN_NAMES.items():
|
||||
stack.enter_context(
|
||||
mock.patch.object(
|
||||
cli, name, side_effect=functools.partial(called.append, group)
|
||||
)
|
||||
)
|
||||
cli._install_all()
|
||||
self.assertEqual(
|
||||
_baked_groups(),
|
||||
[group for group in called if group not in _IN_TREE_GROUPS],
|
||||
"the Dockerfile's lint-docker stage must install every all-mode "
|
||||
"group except the ones the bind-mounted tree replaces, in order",
|
||||
)
|
||||
|
||||
def test_the_lint_stage_runs_the_full_stage_system_step_before_the_bake(
|
||||
self,
|
||||
) -> None:
|
||||
lint_runs = image_slice.stage_runs("lint-docker")
|
||||
self.assertEqual(
|
||||
lint_runs[:1],
|
||||
image_slice.stage_runs("full")[:1],
|
||||
"the lint-docker stage must open with the full stage's system "
|
||||
"package step, so the bake resolves packages in the order a "
|
||||
"runtime install does",
|
||||
)
|
||||
self.assertRegex(lint_runs[-1], _BAKE)
|
||||
|
||||
def test_the_installer_runs_on_the_slice_the_lint_stage_copies(self) -> None:
|
||||
tmp = TemporaryDirectory()
|
||||
self.addCleanup(tmp.cleanup)
|
||||
slice_root = Path(tmp.name)
|
||||
image_slice.materialise(slice_root, frozenset({"base", "lint-docker"}))
|
||||
|
||||
result = subprocess.run(
|
||||
[sys.executable, "-S", "-m", "utils.install.lint", "no-such-group"],
|
||||
cwd=str(slice_root),
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
)
|
||||
|
||||
self.assertNotIn(
|
||||
"ModuleNotFoundError",
|
||||
result.stderr,
|
||||
"utils.install.lint reached for a module the lint-docker stage "
|
||||
"does not copy; copy it there or keep the installer off it",
|
||||
)
|
||||
self.assertEqual(result.returncode, 1, result.stderr)
|
||||
self.assertIn("Usage:", result.stderr)
|
||||
|
||||
interpreters = subprocess.run(
|
||||
[sys.executable, "-S", "-c", "import utils.install.interpreters"],
|
||||
cwd=str(slice_root),
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
)
|
||||
self.assertEqual(interpreters.returncode, 0, interpreters.stderr)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -1,54 +1,15 @@
|
||||
import json
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
|
||||
from tests.utils import image_slice
|
||||
from utils import PROJECT_ROOT
|
||||
from utils.cache.files import read_text
|
||||
from utils.cache.yaml import load_yaml_any
|
||||
from utils.install.collections import declared_pins, unsatisfied
|
||||
|
||||
_DOCKERFILE = PROJECT_ROOT / "Dockerfile"
|
||||
_COPY = re.compile(r"^COPY\s+(?P<source>\S+)\s+\$\{INFINITO_SRC_DIR\}/(?P<target>\S+)$")
|
||||
|
||||
|
||||
def _materialise_image_slice(destination: Path) -> None:
|
||||
"""Reproduce what the image holds when it first runs the collection check.
|
||||
|
||||
The Dockerfile copies a handful of paths, runs ``scripts/install/ansible.sh``
|
||||
against them, and only afterwards copies the rest of the tree. Anything
|
||||
``utils.install.collections`` imports beyond that slice is absent in the
|
||||
layer that executes it, which no import inside a full checkout can show.
|
||||
The list is read from the Dockerfile rather than restated here, so a COPY
|
||||
added or dropped moves this probe with it.
|
||||
|
||||
The caller must run the slice under ``python -S``. This repository installs
|
||||
``utils`` into site-packages, so without it the interpreter resolves every
|
||||
submodule from the checkout however the slice is built, and the probe
|
||||
passes on a module that cannot import inside the image.
|
||||
|
||||
Args:
|
||||
destination: an empty directory to build the slice in.
|
||||
"""
|
||||
for line in read_text(str(_DOCKERFILE)).splitlines():
|
||||
if line.startswith("COPY . "):
|
||||
return
|
||||
match = _COPY.match(line.strip())
|
||||
if not match:
|
||||
continue
|
||||
source = PROJECT_ROOT / match.group("source")
|
||||
target = destination / match.group("target")
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
if source.is_dir():
|
||||
shutil.copytree(source, target, dirs_exist_ok=True)
|
||||
else:
|
||||
shutil.copy2(source, target)
|
||||
|
||||
|
||||
_REQUIREMENTS = (
|
||||
PROJECT_ROOT / "requirements" / "requirements.galaxy.yml",
|
||||
PROJECT_ROOT / "requirements" / "requirements.git.yml",
|
||||
@@ -133,7 +94,7 @@ class TestAgainstPyYAML(unittest.TestCase):
|
||||
tmp = TemporaryDirectory()
|
||||
self.addCleanup(tmp.cleanup)
|
||||
slice_root = Path(tmp.name)
|
||||
_materialise_image_slice(slice_root)
|
||||
image_slice.materialise(slice_root, frozenset({"base"}))
|
||||
requirements = slice_root / "requirements" / "requirements.galaxy.yml"
|
||||
self.assertTrue(
|
||||
requirements.is_file(),
|
||||
|
||||
Reference in new issue
Block a user