Files
computer-playbook/tasks/utils/migrate_meta_layout/__main__.py
T
kevinveenbirkenbachandClaude Opus 4.7 7dc6d6e491 chore(ruff): enable TC + N rules
TC (flake8-type-checking): typing-only imports moved into
``if TYPE_CHECKING:`` blocks across 8 source files via auto-fix
(117 hits). Reduces runtime import surface and rules out circular
imports for type-only deps. Project-wide ``from __future__ import
annotations`` (FA, already enabled) keeps annotations as strings,
so the moved imports never need to load at runtime.

N (pep8-naming): 32 hits handled.

Renames (semantic + lint):
- ``isSmallerThenMaximumBackupSize`` -> ``is_larger_than_maximum_backup_size``
  (snake_case + fixes both the typo "Then -> Than" and the
  semantic mismatch: the function returns ``current > maximum``)
- ``isDirectoryDeletable`` / ``deleteVersion`` /
  ``getAmountOfIteration`` / ``deleteIteration`` -> snake_case
- 8 test functions with ALL-CAPS env-var fragments
  (``test_..._DOMAIN_PRIMARY_...`` etc.) -> snake_case
- Mock alias ``VH`` -> ``vh`` in test_host_vars.py

Exception class renames (``N818`` Error suffix):
- ``TimeLimitExceededException`` -> ``TimeLimitExceededError``
- ``AttemptException`` -> ``AttemptError``
- ``PathNotFound`` -> ``PathNotFoundError`` (propagated across
  4 callers under tests/integration/lookups/config/)
- ``_ExitJson`` / ``_FailJson`` -> ``*_Error``

Refactor:
- ``METADATA = {...}`` local constant in two functions of
  ``plugins/filter/invokable_paths.py`` lifted to module-level
  ``_METADATA_KEYS``.
- ``from typing import TYPE_CHECKING`` in
  ``tasks/utils/migrate_meta_layout/__main__.py`` moved above
  the ``sys.path.insert`` hack to satisfy E402 (collateral from
  the TC auto-fix that landed it after the path mutation).

Per-file ignores in pyproject.toml for the legitimate cases the
rule cannot accept:
- ``pull-specific-host.py`` -> ``N999`` (Ansible-invoked role-level
  script, hyphenated filename pinned by the surrounding stack).
- 4 matrix-deploy files -> ``N806`` for the ``include_R`` /
  ``raw_include_R`` round-index notation; the uppercase ``R``
  here mirrors the formula in the docstring rather than naming
  a constant.
- ``plugins/filter/value_generator.py`` -> ``N806`` for the
  lazy-loaded class binding ``ValueGenerator =
  _value_generator_cls()``, which retains the imported class
  identifier per PEP 8.

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

111 lines
3.5 KiB
Python

"""Entry point: ``python3 -m tasks.utils.migrate_meta_layout``."""
from __future__ import annotations
import sys
from typing import TYPE_CHECKING
from . import PROJECT_ROOT
ROLES_DIR = PROJECT_ROOT / "roles"
NETWORKS_FILE = PROJECT_ROOT / "group_vars" / "all" / "08_networks.yml"
PORTS_FILE = PROJECT_ROOT / "group_vars" / "all" / "09_ports.yml"
sys.path.insert(0, str(PROJECT_ROOT))
from utils.roles.mapping import ROLE_FILE_META_MAIN # noqa: E402
from . import yaml_io # noqa: E402
from .indices import build_network_index, build_port_index # noqa: E402
from .role_builder import build as build_role_meta # noqa: E402
if TYPE_CHECKING:
from pathlib import Path
_PORT_BANDS = {
"local": {
"http": {"start": 8001, "end": 8099},
"websocket": {"start": 4001, "end": 4099},
"oauth2": {"start": 16480, "end": 16499},
"database": {"start": 3306, "end": 5432},
"ldap": {"start": 389, "end": 389},
},
"public": {
"ssh": {"start": 2201, "end": 2299},
"stun_turn": {"start": 3478, "end": 3499},
"stun_turn_tls": {"start": 5349, "end": 5399},
"federation": {"start": 8448, "end": 8499},
"ldaps": {"start": 636, "end": 636},
"relay": {"start": 20000, "end": 59999},
},
}
def _role_needs_migration(
role_dir: Path,
networks: dict,
ports_for_role: dict,
relays_for_role: dict,
) -> bool:
legacy_dirs = any(
(role_dir / sub).is_dir() for sub in ("config", "schema", "users")
)
meta_main_path = role_dir / ROLE_FILE_META_MAIN
meta_main = yaml_io.load(meta_main_path) if meta_main_path.is_file() else None
galaxy_info = meta_main.get("galaxy_info") if isinstance(meta_main, dict) else None
galaxy_info = galaxy_info if isinstance(galaxy_info, dict) else {}
legacy_galaxy = "run_after" in galaxy_info or "lifecycle" in galaxy_info
has_centralised = (
role_dir.name in networks
or role_dir.name in ports_for_role
or role_dir.name in relays_for_role
)
return legacy_dirs or legacy_galaxy or has_centralised
def _strip_centralised_files() -> None:
networks_doc = yaml_io.load(NETWORKS_FILE) or {}
if isinstance(networks_doc, dict):
defaults = networks_doc.get("defaults_networks")
if isinstance(defaults, dict) and "local" in defaults:
defaults.pop("local", None)
if defaults:
networks_doc["defaults_networks"] = defaults
else:
networks_doc.pop("defaults_networks", None)
networks_doc["PORT_BANDS"] = _PORT_BANDS
yaml_io.dump(NETWORKS_FILE, networks_doc)
if PORTS_FILE.is_file():
PORTS_FILE.unlink()
def main() -> int:
if not ROLES_DIR.is_dir():
print(f"roles directory not found at {ROLES_DIR}", file=sys.stderr)
return 1
networks = build_network_index(NETWORKS_FILE)
ports_for_role, relays_for_role = build_port_index(PORTS_FILE, ROLES_DIR)
touched = 0
for role_dir in sorted(p for p in ROLES_DIR.iterdir() if p.is_dir()):
if not _role_needs_migration(
role_dir, networks, ports_for_role, relays_for_role
):
continue
build_role_meta(
role_dir,
networks.get(role_dir.name),
ports_for_role.get(role_dir.name, {}),
relays_for_role.get(role_dir.name, {}),
)
touched += 1
print(f"migrated {touched} roles", file=sys.stderr)
_strip_centralised_files()
return 0
if __name__ == "__main__": # pragma: no cover
sys.exit(main())