Files
computer-playbook/cli/deploy/development/common.py
T
kevinveenbirkenbachandClaude Opus 4.7 26ef3ff504 chore(ruff): enable strict ruleset (E/F/I/B/UP, py311) — 2819 → 0
Adds ``[tool.ruff.lint]`` with ``select = ["E", "F", "I", "B", "UP"]``
and ``target-version = "py311"``. ``E501`` is ignored because
``ruff format`` already reflows what it can; the remaining long lines
are unsplittable string literals / URLs.

Auto-fixed (2753 issues, behavior-preserving):
- UP006/UP035: ``List[X]`` → ``list[X]``, ``Dict[X]`` → ``dict[X]``,
  ``from typing import …`` redirected where superseded by builtins.
- UP045: ``Optional[X]`` → ``X | None``.
- UP010/UP009/UP015: dropped Python-2-era pragmas (``from __future__
  import absolute_import, division, print_function``,
  ``# -*- coding: utf-8 -*-``), redundant ``open(..., "r")`` mode.
- UP004: dropped implicit ``class X(object):``.
- UP022/UP008/UP032: stdlib idiom modernization (``capture_output``,
  super-call simplification, etc.).
- B905: ``zip(...)`` → ``zip(..., strict=False)`` (preserves prior
  behavior; switching to ``strict=True`` is a separate semantic
  choice not made here).
- I001: import-block ordering.

Hand-fixed (66 issues):
- B904 × 54: appended ``from <bound>`` (when the enclosing ``except``
  binds a name) or ``from None`` (when it doesn't) so chained
  exceptions stay distinguishable from errors-during-exception-
  handling. Applied via a small AST-aware script that walked back to
  the enclosing ``except`` clause and matched the binding.
- B017 × 4: narrowed broad ``assertRaises(Exception)`` to the actual
  ``AnsibleError`` / ``AnsibleFilterError`` raised by the code under
  test.
- B023 × 2: bound captured loop variables via default args
  (``def _record(name, *, _path_real=path_real, ...)``) so each
  closure gets its own snapshot.
- B007 × 2: unused loop control variables renamed to ``_`` /
  rewritten to ``.values()``.
- UP007 × 2: ``Union[X, Y]`` → ``X | Y`` (plus dropping the now-unused
  ``from typing import Union``).
- B018 × 1: Odoo addon manifests are bare dict expressions at module
  level by design — suppressed with an inline ``# noqa: B018`` and
  rationale comment.
- UP031 × 1: printf-style format → f-string.

The 65 ``# noqa: …`` markers introduced by the auto-fixer all carry
real ruff codes (``B018``, ``E402``, ``F401``, etc.), so
``test_noqa_only_ruff_codes`` stays green.

Verification
============
- ``ruff check .`` → All checks passed
- ``ruff format --check .`` → 894 files already formatted
- ``tests.lint.repository.{test_project_root_import,
  test_no_direct_yaml_calls, test_noqa_only_ruff_codes}`` and
  ``tests.unit.utils.annotations.test_suppress`` → 20 tests OK
- 1621 unit tests → all pass
- Integration suite: 44 failures (pre-existing baseline, none from
  this change), 1 skipped.

Going forward, the new rules block regressions on E (style), F
(pyflakes), I (import order), B (likely-bug patterns), and UP
(modernization to py311) for any new code.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-07 16:57:20 +02:00

107 lines
3.6 KiB
Python

from __future__ import annotations
import os
from typing import TYPE_CHECKING
from . import PROJECT_ROOT
from .deps import apps_with_deps, resolve_run_after
if TYPE_CHECKING:
from .compose import Compose
# Mirrors INVENTORY_VARS_FILE from scripts/meta/env/inventory.sh; a unit
# test locks the two literals together so they cannot drift.
DEV_INVENTORY_VARS_FILE: str = os.environ.get(
"INVENTORY_VARS_FILE", "inventories/development/default.yml"
)
VALID_DISTROS: tuple[str, ...] = ("arch", "debian", "ubuntu", "fedora", "centos")
def compose_file_args() -> list[str]:
"""Compose `-f` flags shared by up and down flows."""
from .profile import Profile
out = ["-f", "compose.yml"]
if Profile().registry_cache_active():
out += ["-f", "compose/cache.override.yml"]
return out
def cache_env_overrides() -> dict[str, str]:
"""Env vars consumed strictly by compose/cache.override.yml."""
from .profile import Profile
if not Profile().registry_cache_active():
return {}
ca_dir = os.environ.get("INFINITO_PACKAGE_CACHE_FRONTEND_CA_DIR", "").strip()
if not ca_dir:
raise SystemExit(
"INFINITO_PACKAGE_CACHE_FRONTEND_CA_DIR is not set. Source "
"scripts/meta/env/cache/package.sh (or scripts/meta/env/all.sh) "
"before invoking cli.deploy.development."
)
return {
"INFINITO_REGISTRY_CACHE_PROXY_CONF": "./compose/registry-cache/proxy.conf",
"INFINITO_PACKAGE_CACHE_PIP_CONF": "./compose/package-cache/pip.conf",
"INFINITO_PACKAGE_CACHE_NPMRC": "./compose/package-cache/npmrc",
"INFINITO_PACKAGE_CACHE_APT_LIST": "./compose/package-cache/apt.list",
"INFINITO_PACKAGE_CACHE_FRONTEND_CA_FILE": f"{ca_dir}/ca.crt",
}
def resolve_distro() -> str:
"""Return INFINITO_DISTRO; raise SystemExit if missing or invalid."""
distro = os.environ.get("INFINITO_DISTRO", "").strip()
if not distro:
raise SystemExit(
"INFINITO_DISTRO is not set. Source scripts/meta/env/defaults.sh "
"or export INFINITO_DISTRO=<arch|debian|ubuntu|fedora|centos> "
"before invoking cli.deploy.development."
)
if distro not in VALID_DISTROS:
raise SystemExit(
f"INFINITO_DISTRO={distro!r} is not a valid distro. "
f"Valid: {', '.join(VALID_DISTROS)}."
)
return distro
def resolve_container() -> str:
"""Return INFINITO_CONTAINER; raise SystemExit if unset."""
container = os.environ.get("INFINITO_CONTAINER", "").strip()
if not container:
raise SystemExit(
"INFINITO_CONTAINER is not set. Source scripts/meta/env/defaults.sh "
"before invoking cli.deploy.development."
)
return container
def make_compose() -> Compose:
from .compose import Compose
distro = resolve_distro()
# Surface env-script gap here rather than as a cryptic compose error later.
resolve_container()
return Compose(repo_root=PROJECT_ROOT, distro=distro)
def resolve_deploy_ids_for_app(compose: Compose, app_id: str) -> list[str]:
deps = resolve_run_after(compose, app_id)
return apps_with_deps(app_id, deps_role_names=deps)
def resolve_deploy_ids_for_apps(compose: Compose, app_spec: str) -> list[str]:
"""Resolve deploy ids for one or more space- or comma-separated app ids."""
app_ids = [a.strip() for a in app_spec.replace(",", " ").split() if a.strip()]
result: list[str] = []
for app_id in app_ids:
for dep in resolve_deploy_ids_for_app(compose, app_id):
if dep not in result:
result.append(dep)
return result