Files
computer-playbook/utils/roles/validation/resources.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

128 lines
3.7 KiB
Python

from __future__ import annotations
from typing import TYPE_CHECKING, Any
from humanfriendly import parse_size
from utils.annotations.message import warning
from utils.cache.yaml import load_yaml_any
from utils.roles.entity_name import get_entity_name
from utils.roles.mapping import ROLE_FILE_META_SERVICES
from . import PROJECT_ROOT
if TYPE_CHECKING:
from collections.abc import Iterable
from pathlib import Path
def _roles_root() -> Path:
return (PROJECT_ROOT / "roles").resolve()
def _deep_get(dct: dict, path: list[str]) -> Any:
cur: Any = dct
for key in path:
if not isinstance(cur, dict) or key not in cur:
return None
cur = cur[key]
return cur
def _load_yaml_file(path: Path) -> dict:
data = load_yaml_any(str(path), default_if_missing={})
return data if isinstance(data, dict) else {}
def _parse_storage_to_gb(value: Any) -> float:
if isinstance(value, (int, float)):
return float(value)
size_bytes = parse_size(str(value))
return size_bytes / (1000**3)
def filter_roles_by_min_storage(
*,
role_names: Iterable[str],
required_storage: str | float,
emit_warnings: bool = False,
) -> list[str]:
roles_root_path = _roles_root()
out: list[str] = []
try:
required_gb = _parse_storage_to_gb(required_storage)
except Exception as exc:
raise ValueError(
f"Invalid required_storage value: {required_storage!r}"
) from exc
for role_name in role_names:
role_dir = (roles_root_path / role_name).resolve()
if not role_dir.is_dir():
if emit_warnings:
warning(
f"Role directory not found: {role_dir}",
title="min_storage validation",
)
continue
entity_name = get_entity_name(role_name)
if not entity_name:
if emit_warnings:
warning(
f"Could not derive entity_name from role_name '{role_name}'.",
title="min_storage validation",
)
continue
cfg_path = role_dir / ROLE_FILE_META_SERVICES
if not cfg_path.is_file():
continue
try:
cfg = _load_yaml_file(cfg_path)
except Exception as exc:
if emit_warnings:
warning(
f"Failed to parse YAML: {cfg_path} ({exc})",
title="min_storage validation",
)
continue
service_cfg = _deep_get(cfg, [entity_name])
if service_cfg is None or not isinstance(service_cfg, dict):
out.append(role_name)
continue
min_storage_val = service_cfg.get("min_storage")
if min_storage_val is None:
if emit_warnings:
warning(
f"Missing key services.{entity_name}.min_storage in {cfg_path} (treating as 0GB)",
title="min_storage validation",
)
out.append(role_name)
continue
try:
min_storage_gb = _parse_storage_to_gb(min_storage_val)
except Exception as exc:
if emit_warnings:
warning(
f"Invalid min_storage value in {cfg_path}: {min_storage_val!r} ({exc})",
title="min_storage validation",
)
continue
if min_storage_gb <= required_gb:
out.append(role_name)
elif emit_warnings:
warning(
f"{role_name} requires {min_storage_gb:.1f}GB but runner provides only {required_gb:.1f}GB",
title="min_storage validation",
)
return out