Files
computer-playbook/cli/core/discovery.py
T
kevinveenbirkenbachandClaude Opus 4.7 0639fb2fa7 fix(deploy): unblock matrix-deploy of web-apps on dev runtime
Four independent bugs were each blocking the autonomous
fresh-purged-apps iteration of web-app-{openwebui,wordpress,nextcloud,
phpmyadmin,yourls}; bundling them so the pipeline keeps running.

- cli/core/discovery.py: stop walking argv prefixes at the first flag
  or path-shaped arg and ignore OSError so the matrix-init's
  `infinito create inventory <abs-path> --vars '<huge JSON>'` no longer
  hits ENAMETOOLONG inside `is_dir()`. New regression tests pin both.
- utils/cache/data.py: when this module moved to utils/cache/data.py,
  PROJECT_ROOT silently slid one level deep, pointing ROLES_DIR at a
  non-existent `<repo>/utils/roles`. Implicit lookups returned empty
  defaults so `lookup('users','contact')` failed at deploy time as if
  the user weren't defined. Use parents[2] and pin the invariant in a
  new regression test.
- roles/web-app-keycloak/vars/main.yml: KEYCLOAK_PER_APP_LDAP_ROLES_MAPPER
  inherited `subComponents`/`id` from the realm-import dictionary's base
  `ldap-roles` mapper. kcadm rejects those keys on `create components`
  ("Unrecognized field 'subComponents'"), with stderr swallowed by
  `2>/dev/null` in upsert_app_mapper.sh. Strip both keys exactly like
  update/05_ldap.yml already does for the shared mapper.
- scripts/tests/deploy/local/deploy/fresh-purged-app.sh: pass
  `RUNTIME=dev` through `--vars`, otherwise the host process running
  init falls back to `detect_runtime() == "host"` and the Playwright
  E2E gate (`RUNTIME in [dev, act, github]`) never fires.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-25 17:17:14 +02:00

114 lines
3.5 KiB
Python

from __future__ import annotations
import os
from dataclasses import dataclass
from pathlib import Path
from typing import List, Tuple
@dataclass(frozen=True)
class Command:
"""
A CLI command is a Python package directory under cli/ that contains __main__.py.
Example:
cli/build/tree/__main__.py -> parts=("build","tree"), module="cli.build.tree"
"""
parts: Tuple[str, ...] # relative path parts under cli/
module: str # python -m module
main_path: Path # filesystem path to __main__.py
@property
def folder(self) -> str | None:
if len(self.parts) <= 1:
return None
return "/".join(self.parts[:-1])
@property
def name(self) -> str:
return self.parts[-1]
@property
def subcommand(self) -> str:
return " ".join(self.parts)
def _is_valid_package_dir(path: Path) -> bool:
if not path.is_dir():
return False
if path.name == "__pycache__":
return False
return True
def discover_commands(cli_dir: Path) -> List[Command]:
"""
Recursively find all packages under cli_dir that contain __main__.py.
"""
commands: List[Command] = []
for root, dirnames, filenames in os.walk(cli_dir):
# prune __pycache__ eagerly
dirnames[:] = [d for d in dirnames if d != "__pycache__"]
if "__main__.py" not in filenames:
continue
root_path = Path(root)
if not _is_valid_package_dir(root_path):
continue
rel = root_path.relative_to(cli_dir)
if rel.parts == ():
# cli/ itself is NOT a command; cli/__main__.py is the dispatcher
continue
parts = tuple(rel.parts)
module = "cli." + ".".join(parts)
commands.append(
Command(parts=parts, module=module, main_path=root_path / "__main__.py")
)
commands.sort(key=lambda c: (c.folder or "", c.name))
return commands
def resolve_command_module(
cli_dir: Path, argv_parts: List[str]
) -> tuple[str | None, List[str]]:
"""
Resolve the longest argv prefix that matches a discovered command module.
Returns:
(module, remaining_args)
Example:
argv_parts=["deploy","container","--foo","bar"]
-> ("cli.deploy.container", ["--foo","bar"])
"""
# Subcommand path is always BEFORE the first flag (CLI convention) and
# cannot contain absolute paths or path separators. Capping the search
# window at the first "non-subcommand-shaped" arg keeps the loop from
# building paths that contain huge `--vars` JSON or absolute `--vars-file`
# values, which previously triggered ENAMETOOLONG inside `is_dir()`.
max_prefix = len(argv_parts)
for i, part in enumerate(argv_parts):
if not part or part.startswith("-") or "/" in part or "\\" in part:
max_prefix = i
break
# We resolve by checking for cli/<prefix>/__main__.py existing.
for n in range(max_prefix, 0, -1):
candidate_dir = cli_dir.joinpath(*argv_parts[:n])
try:
if candidate_dir.is_dir() and (candidate_dir / "__main__.py").is_file():
module = "cli." + ".".join(argv_parts[:n])
return module, argv_parts[n:]
except OSError:
# stat() may legitimately fail (ENAMETOOLONG, ENOENT on broken
# symlinks, EACCES, ...); none of these mean "this is the
# command", so just keep trying shorter prefixes.
continue
return None, argv_parts