mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-28 13:57:43 +00:00
Extends ``[tool.ruff.lint].select`` with six additional rule families:
``RUF`` (ruff-specific), ``SIM`` (flake8-simplify), ``C4``
(flake8-comprehensions), ``PERF`` (perflint), ``RET`` (flake8-return),
``PIE`` (flake8-pie). Adds ``RUF001/002/003`` to ``ignore`` because
those exist for homoglyph-attack detection, not English typography —
the project uses em/en-dashes and ``ℹ`` as a UI-hint glyph in CLI help
output deliberately.
Auto-fixed (~225, mechanical & behavior-preserving)
====================================================
- RUF005: ``a + [b]`` → ``[*a, b]``
- C408: ``dict(k=v)`` → ``{"k": v}``
- C420: ``dict.fromkeys(seq)`` shape simplifications
- SIM103/SIM110/SIM118: truthy-return / dict-key / dict-method
idiom simplifications
- RET504/RET505: drop unneeded final assigns / ``else`` after
``return``
- PIE810: combine ``str.startswith(a) or .startswith(b)``
into one call with a tuple
- RUF010: ``f"{str(x)}"`` → ``f"{x\!s}"``
- RUF100: removed unused ``# noqa`` directives that
became stale (mostly old ``# noqa: E402``
markers that ruff's smart E402 implementation
no longer flags after ``sys.path`` shims)
- RUF015 / RUF013 / RUF022 / RUF046 / RUF059: misc small wins
- I001: re-sorted import blocks where the new
imports above shifted them
Hand-fixed (~140, scripted where mechanical)
============================================
PERF401 (42): scripted AST rewrite of
``for x in iter: target.append(expr)`` (with optional ``if`` guard)
→ ``target.extend(expr for x in iter [if cond])``.
``list.extend(genexp)`` consumes the generator immediately, so the
rewrite is semantically equivalent to the original eager loop.
SIM117 (41): scripted AST combiner for nested ``with`` blocks where
the inner is the only statement of the outer (38 sites). The remaining
3 multi-line ``patch.object(...)`` chains were combined by hand using
the parenthesised ``with (\\n A,\\n B,\\n):`` form.
SIM102 (14): scripted AST combiner for nested ``if`` (10 sites). The
remaining 4 carry an inline ``# noqa: SIM102`` with rationale —
combining would either change semantics (the outer body has siblings
beyond the inner ``if``, e.g. ``cli/encrypt/inventory/__main__.py``
where ``encrypted_value = …`` runs regardless of ``ask_confirmation``)
or hurt readability (long literal whitelists).
RUF012 (9): annotated mutable class attributes with ``ClassVar[…]``
and added ``from typing import ClassVar``.
SIM105 (6): ``try/except: pass`` → ``with contextlib.suppress(…):``
with the necessary ``import contextlib`` added. Verified on
``utils/templating.py`` (3 occurrences in a ``finally`` block) —
cleanup behaviour is identical.
SIM115 (4): wrapped ``tempfile.NamedTemporaryFile(..., delete=False)``
call patterns in a ``with`` block; ``delete=False`` keeps the file
on disk after the block, so callers' ``return f.name`` still works.
PERF402, PERF403, B007, SIM108, SIM103: 5 misc fixes (loop→list-copy,
loop→dict-comprehension, unused-loop-var rename, ternary, return
expression).
Verification
============
- ``ruff check .`` → All checks passed
- ``ruff format --check .`` → 894 files already formatted
- 1621 unit tests → all pass
- Integration suite: 44 baseline failures, 1 skipped (unchanged)
- Sample-audited PERF401 rewrites in ``resolver.py``, ``inventory.py``,
``cache/files.py``, ``ressources/__main__.py``; SIM117 in
``test_main.py``; SIM105 in ``utils/templating.py``; SIM102 noqa
rationales in ``cli/encrypt/inventory/__main__.py``.
182 files changed, +993 / −960. With this and the prior strict-rule
commit the branch satisfies 11 ruff rule families end-to-end.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
186 lines
5.4 KiB
Python
186 lines
5.4 KiB
Python
from __future__ import annotations
|
|
|
|
import subprocess
|
|
import sys
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
|
|
from cli.core.colors import Fore, Style, color_text
|
|
from cli.core.discovery import resolve_command_module
|
|
from cli.core.git import git_clean_repo
|
|
from cli.core.help import (
|
|
print_global_help,
|
|
show_full_help_for_all,
|
|
show_help_for_directory,
|
|
)
|
|
from cli.core.run import RunConfig, open_log_file, run_command_once
|
|
|
|
from . import PROJECT_ROOT
|
|
|
|
|
|
@dataclass
|
|
class Flags:
|
|
log_dir: Path | None = None
|
|
git_clean: bool = False
|
|
infinite: bool = False
|
|
help_all: bool = False
|
|
|
|
|
|
def _first_non_flag_token(argv: list[str]) -> str | None:
|
|
"""
|
|
Return the first non-flag token after argv[0], but treat '--log <ARG>' as a flag
|
|
with a required argument (skip both tokens).
|
|
"""
|
|
i = 1
|
|
while i < len(argv):
|
|
token = argv[i]
|
|
|
|
# Skip flags and their args
|
|
if token == "--log":
|
|
# Skip '--log' and its argument if present
|
|
i += 2
|
|
continue
|
|
|
|
if token.startswith("-"):
|
|
i += 1
|
|
continue
|
|
|
|
return token
|
|
|
|
return None
|
|
|
|
|
|
def _parse_log_dir(argv: list[str]) -> Path | None:
|
|
"""
|
|
Parse and remove '--log <LOG_DIR>' from argv.
|
|
|
|
- The log path argument is mandatory when --log is present.
|
|
- Logging is only allowed for `deploy` commands. If used with a different
|
|
top-level command, it is silently ignored (but still removed from argv).
|
|
"""
|
|
if "--log" not in argv:
|
|
return None
|
|
|
|
i = argv.index("--log")
|
|
if i + 1 >= len(argv):
|
|
print(
|
|
color_text(
|
|
"Error: --log requires a path argument (e.g. --log /tmp/infinito-logs).",
|
|
Fore.RED,
|
|
)
|
|
)
|
|
raise SystemExit(1)
|
|
|
|
raw = argv[i + 1]
|
|
if raw.startswith("-"):
|
|
print(
|
|
color_text(
|
|
"Error: --log requires a path argument (e.g. --log /tmp/infinito-logs).",
|
|
Fore.RED,
|
|
)
|
|
)
|
|
raise SystemExit(1)
|
|
|
|
# Determine the first command token (skipping '--log <ARG>' properly)
|
|
first_cmd = _first_non_flag_token(argv)
|
|
|
|
# Always remove '--log <ARG>' from argv
|
|
del argv[i : i + 2]
|
|
|
|
# Keep previous behavior: only enable logging for "deploy"
|
|
if first_cmd != "deploy":
|
|
return None
|
|
|
|
return Path(raw).expanduser()
|
|
|
|
|
|
def parse_flags(argv: list[str]) -> Flags:
|
|
flags = Flags()
|
|
flags.log_dir = _parse_log_dir(argv)
|
|
|
|
flags.git_clean = "--git-clean" in argv and (argv.remove("--git-clean") or True)
|
|
flags.infinite = "--infinite" in argv and (argv.remove("--infinite") or True)
|
|
flags.help_all = "--help-all" in argv and (argv.remove("--help-all") or True)
|
|
|
|
return flags
|
|
|
|
|
|
def main() -> None:
|
|
argv = sys.argv[:] # keep sys.argv for external tools, but parse on a copy
|
|
flags = parse_flags(argv)
|
|
args = argv[1:]
|
|
|
|
cli_dir = PROJECT_ROOT / "cli"
|
|
# sanity: cli_dir should contain __init__.py and __main__.py of dispatcher
|
|
# but we do not hard-fail here
|
|
|
|
if flags.git_clean:
|
|
git_clean_repo()
|
|
|
|
# Global "show help for all commands" mode
|
|
if flags.help_all:
|
|
print_global_help(cli_dir)
|
|
print(color_text("Full detailed help for all subcommands:", Style.BRIGHT))
|
|
print()
|
|
show_full_help_for_all(cli_dir)
|
|
raise SystemExit(0)
|
|
|
|
# Global help
|
|
if not args or args[0] in ("-h", "--help"):
|
|
print_global_help(cli_dir)
|
|
raise SystemExit(0)
|
|
|
|
# Directory-specific help: "<path> -h"
|
|
if len(args) > 1 and args[-1] in ("-h", "--help"):
|
|
# First: if "<path>" is a real command, forward help to its argparse
|
|
module, remaining = resolve_command_module(cli_dir, args[:-1])
|
|
if module and not remaining:
|
|
subprocess.run([sys.executable, "-m", module, "--help"])
|
|
raise SystemExit(0)
|
|
|
|
# Otherwise: treat it as a folder overview
|
|
dir_parts = args[:-1]
|
|
if show_help_for_directory(cli_dir, dir_parts):
|
|
raise SystemExit(0)
|
|
|
|
# Resolve command module by package folders with __main__.py
|
|
module, remaining = resolve_command_module(cli_dir, args)
|
|
if not module:
|
|
print(color_text(f"Error: command '{' '.join(args)}' not found.", Fore.RED))
|
|
raise SystemExit(1)
|
|
|
|
# If user requested help for the resolved command, forward directly
|
|
if remaining and remaining[0] in ("-h", "--help"):
|
|
subprocess.run([sys.executable, "-m", module, remaining[0]])
|
|
raise SystemExit(0)
|
|
|
|
log_file = None
|
|
if flags.log_dir is not None:
|
|
log_file, log_path = open_log_file(flags.log_dir)
|
|
print(color_text(f"Tip: Log file created at {log_path}", Fore.GREEN))
|
|
|
|
full_cmd = [sys.executable, "-m", module, *remaining]
|
|
|
|
cfg = RunConfig(
|
|
log_enabled=flags.log_dir is not None,
|
|
)
|
|
|
|
try:
|
|
if flags.infinite:
|
|
print(color_text("Starting infinite execution mode...", Fore.CYAN))
|
|
count = 1
|
|
while True:
|
|
print(color_text(f"Run #{count}", Style.BRIGHT))
|
|
run_command_once(full_cmd, cfg, log_file)
|
|
count += 1
|
|
else:
|
|
run_command_once(full_cmd, cfg, log_file)
|
|
raise SystemExit(0)
|
|
except KeyboardInterrupt:
|
|
print()
|
|
print(color_text("Execution interrupted by user (Ctrl+C).", Fore.YELLOW))
|
|
raise SystemExit(130) from None
|
|
finally:
|
|
if log_file:
|
|
log_file.close()
|