testsAndMisc/python_pkg/vscode_optimizer/_optimize.py
Krzysztof kuhy Rudnicki e9ba82ed66
Some checks are pending
md-naming / markdown naming convention (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (mypy, mypy) (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (pylint, pylint) (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (rest, ) (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (shellcheck, shellcheck) (push) Waiting to run
Python tests / test (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Shell tests / shell-tests (push) Waiting to run
Shell tests / lib-tests (push) Waiting to run
Shell tests / arch-tests (push) Waiting to run
Put the kuhy credit on every python_pkg CLI
Twelve interactive CLIs end their --help on credit_line() from
kuhy-credit v0.3.1 (android_ui, workout_firebase_flow, app_icons,
archwiki_rag, artgate, code_tutor, js_anki, morning_routine,
phone_lease, random_jpg, sql_drills, vscode_optimizer). The repo has
no GUI, web app or game, so --help is the only surface.

morning_routine's unit runs /usr/bin/python, so its install.sh now
installs the requirements.txt pin there. Stdio/daemon entry points
stay without it; the evidence file lists each with the reason.

Also bumps Flutter to 3.50.1 and the stale Python pins.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 10:17:38 +01:00

107 lines
3.0 KiB
Python

"""Auto-optimize VS Code settings based on detected hardware.
Run as ``python3 -m python_pkg.vscode_optimizer [--dry-run] [--yes]``.
"""
from __future__ import annotations
import argparse
import sys
from typing import TYPE_CHECKING
from kuhy_credit import credit_line
from python_pkg.vscode_optimizer._config_io import (
_backup,
_discover_variants,
_read_flags,
_read_settings,
_write_flags,
_write_settings,
)
from python_pkg.vscode_optimizer._hardware import _detect_hardware
from python_pkg.vscode_optimizer._plan import _compute_opts, _gpu_flags
from python_pkg.vscode_optimizer._render import (
_BO,
_G,
_R,
_Y,
_hdr,
_out,
_show_hw,
_show_plan,
)
if TYPE_CHECKING:
from python_pkg.vscode_optimizer._types import _Hw, _Variant
def _apply_variant(
v: _Variant,
hw: _Hw,
ideal_flags: list[str],
*,
dry_run: bool,
auto_yes: bool,
) -> None:
"""Compute and apply optimizations for a single variant."""
_hdr(f"Optimizing: {v.name}")
current = _read_settings(v.settings)
opts = _compute_opts(hw, current)
old_flags = _read_flags(v.flags)
merged = list(dict.fromkeys(old_flags + ideal_flags))
_show_plan(opts, ideal_flags, old_flags)
flag_changed = merged != old_flags
if not opts and not flag_changed:
_out(f"\n {_G}Nothing to do for {v.name}.{_R}")
return
if dry_run:
_out(f"\n {_Y}(dry-run) No files modified.{_R}")
return
if not auto_yes:
ans = input(f"\n Apply changes to {v.name}? [y/N] ").strip()
if ans.lower() not in ("y", "yes"):
_out(" Skipped.")
return
if opts:
bak = _backup(v.settings)
if bak:
_out(f" Backup: {bak}")
_write_settings(v.settings, current, opts)
_out(f" {_G}\u2713 settings.json updated{_R}")
if flag_changed:
bak = _backup(v.flags)
if bak:
_out(f" Backup: {bak}")
_write_flags(v.flags, merged)
_out(f" {_G}\u2713 {v.flags.name} updated{_R}")
def main() -> None:
"""Entry point: detect hardware, compute optimizations, and apply."""
ap = argparse.ArgumentParser(
description="Auto-optimize VS Code for current hardware.",
epilog=credit_line(),
)
ap.add_argument("--dry-run", action="store_true", help="Preview without writing.")
ap.add_argument("--yes", "-y", action="store_true", help="Skip confirmation.")
args = ap.parse_args()
hw = _detect_hardware()
_show_hw(hw)
variants = _discover_variants()
if not variants:
_out(f"\n{_Y}No VS Code installation found.{_R}")
sys.exit(1)
_hdr("VS Code Installations")
for v in variants:
_out(f" {_G}\u2022{_R} {v.name} ({v.settings})")
ideal = _gpu_flags(hw)
for v in variants:
_apply_variant(v, hw, ideal, dry_run=args.dry_run, auto_yes=args.yes)
_hdr("Done")
_out(f" {_BO}Restart VS Code{_R} to apply the changes.\n")
if __name__ == "__main__":
main()