diff --git a/tests/unit/python/utils/env/handlers/infinito/test_cache_conf.py b/tests/unit/python/utils/env/handlers/infinito/test_cache_conf.py index 6a37db8c4e..e0a35a4781 100644 --- a/tests/unit/python/utils/env/handlers/infinito/test_cache_conf.py +++ b/tests/unit/python/utils/env/handlers/infinito/test_cache_conf.py @@ -1,4 +1,4 @@ -"""Unit tests for :mod:`utils.env.handlers.infinito.cache_conf`.""" +"""Unit tests for :mod:`utils.env.handlers.infinito.cache.conf`.""" from __future__ import annotations @@ -10,7 +10,7 @@ from tempfile import TemporaryDirectory from unittest.mock import patch from utils.env.builder import BuildContext, EnvBuilder -from utils.env.handlers.infinito import cache_conf as handler +from utils.env.handlers.infinito.cache import conf as handler _BLANK = {handler.SOURCE_KEY: "", handler.KEY: ""} @@ -63,6 +63,11 @@ class TestPrimaryCheckout(unittest.TestCase): self.assertEqual(handler.primary_checkout(outside), outside) + def test_a_root_git_cannot_be_run_in_falls_back_to_itself(self) -> None: + missing = self.root / "gone" + + self.assertEqual(handler.primary_checkout(missing), missing) + class TestApply(unittest.TestCase): def setUp(self) -> None: diff --git a/tests/unit/python/utils/env/handlers/infinito/test_cache_stack.py b/tests/unit/python/utils/env/handlers/infinito/test_cache_stack.py index 5395738962..2ec5db4cdf 100644 --- a/tests/unit/python/utils/env/handlers/infinito/test_cache_stack.py +++ b/tests/unit/python/utils/env/handlers/infinito/test_cache_stack.py @@ -1,4 +1,4 @@ -"""Unit tests for :mod:`utils.env.handlers.infinito.cache_stack`.""" +"""Unit tests for :mod:`utils.env.handlers.infinito.cache.stack`.""" from __future__ import annotations @@ -8,7 +8,7 @@ from pathlib import Path from unittest.mock import patch from utils.env.builder import BuildContext, EnvBuilder -from utils.env.handlers.infinito import cache_stack as handler +from utils.env.handlers.infinito.cache import stack as handler _BLANK = {"CI": "", handler.KEY: ""} diff --git a/utils/env/handlers/__init__.py b/utils/env/handlers/__init__.py index 4bc99d4ab8..2dbaa55bd3 100644 --- a/utils/env/handlers/__init__.py +++ b/utils/env/handlers/__init__.py @@ -16,15 +16,11 @@ from . import ( passthrough, ) from .infinito import ( - cache_conf, - cache_stack, container, distros, docker_volume, domain, gpu_count, - i18n_libretranslate, - i18n_tuning, image, image_repository, inventory, @@ -37,11 +33,15 @@ from .infinito import ( ) from .infinito.ca import bundle_candidates as ca_bundle_candidates from .infinito.ca import cert_host as ca_cert_host +from .infinito.cache import conf as cache_conf +from .infinito.cache import stack as cache_stack from .infinito.dir import backups as dir_backups from .infinito.dir import secrets as dir_secrets from .infinito.dir import var_lib as dir_var_lib from .infinito.fork import account as fork_account from .infinito.fork import repository_url as fork_repository_url +from .infinito.i18n import libretranslate as i18n_libretranslate +from .infinito.i18n import tuning as i18n_tuning from .infinito.package_cache import admin_password as package_cache_admin_password from .infinito.package_cache import blobstore_max as package_cache_blobstore_max from .infinito.package_cache import direct_mem as package_cache_direct_mem diff --git a/utils/env/handlers/infinito/cache/__init__.py b/utils/env/handlers/infinito/cache/__init__.py new file mode 100644 index 0000000000..30e0e062cf --- /dev/null +++ b/utils/env/handlers/infinito/cache/__init__.py @@ -0,0 +1 @@ +"""Package-cache env handlers.""" diff --git a/utils/env/handlers/infinito/cache_conf.py b/utils/env/handlers/infinito/cache/conf.py similarity index 82% rename from utils/env/handlers/infinito/cache_conf.py rename to utils/env/handlers/infinito/cache/conf.py index 0d3b6127bc..219526e199 100644 --- a/utils/env/handlers/infinito/cache_conf.py +++ b/utils/env/handlers/infinito/cache/conf.py @@ -31,13 +31,16 @@ def primary_checkout(repo_root: Path) -> Path: Returns: The primary checkout's path, or ``repo_root`` when git cannot say. """ - common = subprocess.run( - ["git", "rev-parse", "--path-format=absolute", "--git-common-dir"], - cwd=repo_root, - capture_output=True, - text=True, - check=False, - ).stdout.strip() + try: + common = subprocess.run( + ["git", "rev-parse", "--path-format=absolute", "--git-common-dir"], + cwd=repo_root, + capture_output=True, + text=True, + check=False, + ).stdout.strip() + except OSError: + return repo_root if not common: return repo_root return Path(common).parent diff --git a/utils/env/handlers/infinito/cache_stack.py b/utils/env/handlers/infinito/cache/stack.py similarity index 100% rename from utils/env/handlers/infinito/cache_stack.py rename to utils/env/handlers/infinito/cache/stack.py diff --git a/utils/env/handlers/infinito/i18n/__init__.py b/utils/env/handlers/infinito/i18n/__init__.py new file mode 100644 index 0000000000..dd42ba3a81 --- /dev/null +++ b/utils/env/handlers/infinito/i18n/__init__.py @@ -0,0 +1 @@ +"""Gettext and LibreTranslate env handlers.""" diff --git a/utils/env/handlers/infinito/i18n_libretranslate.py b/utils/env/handlers/infinito/i18n/libretranslate.py similarity index 100% rename from utils/env/handlers/infinito/i18n_libretranslate.py rename to utils/env/handlers/infinito/i18n/libretranslate.py diff --git a/utils/env/handlers/infinito/i18n/tuning.py b/utils/env/handlers/infinito/i18n/tuning.py new file mode 100644 index 0000000000..9764b0f62d --- /dev/null +++ b/utils/env/handlers/infinito/i18n/tuning.py @@ -0,0 +1,34 @@ +"""INFINITO_I18N_BATCH_SIZE / INFINITO_I18N_LANES: client settings `make +i18n-tune` measured on this host, falling back to the shipped defaults.""" + +from __future__ import annotations + +import json +from pathlib import Path +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from utils.env.builder import BuildContext, EnvBuilder + +TUNING_FILE = Path("build") / "i18n-tuning.json" +BATCH_KEY = "INFINITO_I18N_BATCH_SIZE" +LANES_KEY = "INFINITO_I18N_LANES" +BATCH_COMMENT = "Texts per LibreTranslate request; `make i18n-tune` measures it." +LANES_COMMENT = "Concurrent LibreTranslate requests; 0 derives from the CPU count." + + +def measured(repo_root: Path) -> dict: + """Return the winning pair `make i18n-tune` recorded, empty when absent.""" + path = repo_root / TUNING_FILE + if not path.is_file(): + return {} + try: + return json.loads(path.read_text(encoding="utf-8")).get("best") or {} + except (OSError, ValueError): + return {} + + +def apply(eb: EnvBuilder, ctx: BuildContext) -> None: + best = measured(ctx.repo_root) + eb.setdefault(BATCH_KEY, str(best.get("batch_size", 20)), comment=BATCH_COMMENT) + eb.setdefault(LANES_KEY, str(best.get("lanes", 0)), comment=LANES_COMMENT)