116 lines
3.7 KiB
Python
116 lines
3.7 KiB
Python
"""Self-management commands for bin."""
|
|
|
|
import os
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import click
|
|
|
|
|
|
# 手动安装的文件清单(pip 管理范围外,uninstall 时需要额外清理)
|
|
_MANUAL_FILES: dict[str, list[Path]] = {
|
|
"completions": [
|
|
Path.home() / ".local" / "bin" / "completions" / "_mytoolkit",
|
|
Path.home() / ".local" / "bin" / "completions" / "mytoolkit.bash",
|
|
],
|
|
}
|
|
|
|
|
|
def _get_project_root() -> Path:
|
|
"""Get mytoolkit project root from source tree."""
|
|
return Path(__file__).parent.parent.parent
|
|
|
|
|
|
def _run(cmd: list[str], cwd: Path | None = None, check: bool = True) -> None:
|
|
result = subprocess.run(cmd, cwd=str(cwd) if cwd else None, check=False)
|
|
if check and result.returncode != 0:
|
|
raise click.Exit(result.returncode)
|
|
|
|
|
|
def _install(root: Path) -> None:
|
|
"""Install or update using the current Python's pip (editable)."""
|
|
click.secho("Installing mytoolkit (editable)…", fg="cyan")
|
|
_run([sys.executable, "-m", "pip", "install", "-e", str(root), "--upgrade"])
|
|
|
|
entry_point = shutil.which("mytoolkit")
|
|
if entry_point:
|
|
click.secho(f"Entry point: {entry_point}", fg="green")
|
|
else:
|
|
click.secho("Warning: mytoolkit not found in PATH after install", fg="yellow")
|
|
|
|
_install_completions()
|
|
|
|
|
|
def _install_completions() -> None:
|
|
venv_toolkit = shutil.which("mytoolkit")
|
|
if not venv_toolkit:
|
|
click.secho("Warning: cannot generate completions (mytoolkit not in PATH)", fg="yellow")
|
|
return
|
|
|
|
comp_dir = Path.home() / ".local" / "bin" / "completions"
|
|
comp_dir.mkdir(parents=True, exist_ok=True)
|
|
shell = os.environ.get("SHELL", "")
|
|
if "zsh" in shell:
|
|
comp_file = comp_dir / "_mytoolkit"
|
|
result = subprocess.run(
|
|
[venv_toolkit],
|
|
env={**os.environ, "_MYTOOLKIT_COMPLETE": "zsh_source"},
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
if result.returncode == 0:
|
|
comp_file.write_text(result.stdout)
|
|
click.secho(f"Installed zsh completion: {comp_file}", fg="green")
|
|
else:
|
|
click.secho("Warning: failed to generate zsh completion", fg="yellow")
|
|
elif "bash" in shell:
|
|
comp_file = comp_dir / "mytoolkit.bash"
|
|
result = subprocess.run(
|
|
[venv_toolkit],
|
|
env={**os.environ, "_MYTOOLKIT_COMPLETE": "bash_source"},
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
if result.returncode == 0:
|
|
comp_file.write_text(result.stdout)
|
|
click.secho(f"Installed bash completion: {comp_file}", fg="green")
|
|
else:
|
|
click.secho("Warning: failed to generate bash completion", fg="yellow")
|
|
|
|
|
|
def _uninstall() -> None:
|
|
click.secho("Uninstalling mytoolkit package…", fg="cyan")
|
|
subprocess.run(
|
|
[sys.executable, "-m", "pip", "uninstall", "mytoolkit", "-y"],
|
|
check=False,
|
|
)
|
|
|
|
removed = 0
|
|
for paths in _MANUAL_FILES.values():
|
|
for f in paths:
|
|
if f.exists() or f.is_symlink():
|
|
f.unlink()
|
|
click.secho(f"Removed {f}", fg="green")
|
|
removed += 1
|
|
if removed == 0:
|
|
click.secho("No manual files to clean up.", fg="cyan")
|
|
|
|
|
|
@click.command(name="update")
|
|
def update_cmd():
|
|
"""Update mytoolkit from the local repository."""
|
|
root = _get_project_root()
|
|
click.secho("Updating mytoolkit…", fg="cyan")
|
|
_install(root)
|
|
click.secho("mytoolkit updated.", fg="green")
|
|
|
|
|
|
@click.command(name="uninstall")
|
|
def uninstall_cmd():
|
|
"""Uninstall mytoolkit."""
|
|
click.secho("Uninstalling mytoolkit…", fg="cyan")
|
|
_uninstall()
|
|
click.secho("mytoolkit removed.", fg="green")
|