Files
computer-playbook/cli/administration/inventory/provision/project.py
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

47 lines
1.3 KiB
Python

from __future__ import annotations
import os
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from pathlib import Path
def detect_project_root(start_file: Path) -> Path:
"""
Detect repo root by walking upwards from a file path until we find a typical repo layout.
We require:
- cli/ directory
- roles/ directory
- group_vars/ directory
This avoids brittle parents[N] assumptions and fixes "cli/cli/..." path bugs.
"""
here = start_file.resolve()
for p in [here, *here.parents]:
if (
(p / "cli").is_dir()
and (p / "roles").is_dir()
and (p / "group_vars").is_dir()
):
return p
raise SystemExit(f"Could not detect project root from: {here}")
def build_env_with_project_root(project_root: Path) -> dict[str, str]:
"""
Return an environment dict where PYTHONPATH includes the project root.
This makes `utils` and other top-level packages importable in subprocesses.
"""
env = os.environ.copy()
root_str = str(project_root)
existing = env.get("PYTHONPATH")
if existing:
parts = existing.split(os.pathsep)
if root_str not in parts:
env["PYTHONPATH"] = root_str + os.pathsep + existing
else:
env["PYTHONPATH"] = root_str
return env