|
| 1 | +"""`dailybot upgrade` subcommand — self-update the installed CLI. |
| 2 | +
|
| 3 | +Auto-detects the installation method (pipx / uv tool / pip / Homebrew / |
| 4 | +PyInstaller binary / editable) and either: |
| 5 | +
|
| 6 | + - runs the right upgrade command in a subprocess (pipx / uv / pip), or |
| 7 | + - prints the command the user should run themselves (Homebrew / binary / |
| 8 | + editable installs, where invoking the package manager from inside the |
| 9 | + CLI is risky — sudo prompts, locks, dev iteration). |
| 10 | +
|
| 11 | +The command is intentionally honest about what it can and cannot do |
| 12 | +automatically. Mirrors the design of `bun upgrade` / `deno upgrade` / |
| 13 | +`oh-my-posh upgrade` for cases the CLI controls, and falls back to a |
| 14 | +"copy this command" hint everywhere else. |
| 15 | +""" |
| 16 | + |
| 17 | +import platform |
| 18 | +import shutil |
| 19 | +import subprocess |
| 20 | +import sys |
| 21 | +from pathlib import Path |
| 22 | + |
| 23 | +import click |
| 24 | + |
| 25 | +from dailybot_cli import __version__ |
| 26 | +from dailybot_cli.commands.version import _fetch_latest_pypi_version |
| 27 | +from dailybot_cli.display import ( |
| 28 | + print_error, |
| 29 | + print_info, |
| 30 | + print_success, |
| 31 | + print_warning, |
| 32 | +) |
| 33 | + |
| 34 | +_PACKAGE: str = "dailybot-cli" |
| 35 | + |
| 36 | +# Install methods we can reliably detect. Each maps to: |
| 37 | +# - a "label" for human-facing output |
| 38 | +# - a list of argv tokens (subprocess.run safe — no shell=True) when we can |
| 39 | +# execute the upgrade ourselves; or `None` if we should print a command |
| 40 | +# and let the user run it. |
| 41 | +# - a "manual command" string for the print-only path. |
| 42 | +_METHOD_LABELS: dict[str, str] = { |
| 43 | + "pipx": "pipx", |
| 44 | + "uv-tool": "uv tool", |
| 45 | + "pip": "pip", |
| 46 | + "homebrew": "Homebrew", |
| 47 | + "binary": "PyInstaller binary", |
| 48 | + "editable": "editable install (development)", |
| 49 | + "unknown": "unknown", |
| 50 | +} |
| 51 | + |
| 52 | + |
| 53 | +def _resolve_install_path() -> Path: |
| 54 | + """Return the on-disk path of the running `dailybot_cli` package.""" |
| 55 | + import dailybot_cli |
| 56 | + |
| 57 | + return Path(dailybot_cli.__file__).resolve().parent |
| 58 | + |
| 59 | + |
| 60 | +def _detect_install_method() -> str: |
| 61 | + """Best-effort detection of how this CLI was installed. |
| 62 | +
|
| 63 | + Returns one of: pipx, uv-tool, pip, homebrew, binary, editable, unknown. |
| 64 | +
|
| 65 | + The heuristics inspect the install path's directory components. They |
| 66 | + are deliberately conservative: when in doubt, fall back to "pip" (which |
| 67 | + is always safe to upgrade with `python -m pip install --upgrade ...`). |
| 68 | + """ |
| 69 | + # PyInstaller frozen build → can't trivially auto-replace itself. |
| 70 | + if getattr(sys, "frozen", False): |
| 71 | + return "binary" |
| 72 | + |
| 73 | + path = _resolve_install_path() |
| 74 | + parts_lower = [p.lower() for p in path.parts] |
| 75 | + |
| 76 | + # Editable install: pyproject.toml lives next to the package source. |
| 77 | + if (path.parent / "pyproject.toml").exists(): |
| 78 | + return "editable" |
| 79 | + |
| 80 | + # Homebrew formulas live under .../Cellar/<formula>/<version>/... |
| 81 | + if "cellar" in parts_lower and any("dailybot" in p for p in parts_lower): |
| 82 | + return "homebrew" |
| 83 | + |
| 84 | + # pipx isolated venvs live under .../pipx/venvs/<package>/... |
| 85 | + if "pipx" in parts_lower: |
| 86 | + return "pipx" |
| 87 | + |
| 88 | + # uv tool installs live under .../uv/tools/<package>/... (XDG_DATA_HOME) |
| 89 | + # or .../.local/share/uv/tools/<package>/... |
| 90 | + if "uv" in parts_lower and "tools" in parts_lower: |
| 91 | + return "uv-tool" |
| 92 | + |
| 93 | + # Generic pip install (system, user-site, or a regular venv). |
| 94 | + return "pip" |
| 95 | + |
| 96 | + |
| 97 | +def _build_upgrade_argv(method: str) -> list[str] | None: |
| 98 | + """Return argv for `subprocess.run` to perform the upgrade, or None. |
| 99 | +
|
| 100 | + `None` means "we don't auto-run for this method — print the command |
| 101 | + instead". `homebrew`, `binary`, `editable`, and `unknown` fall in |
| 102 | + that category for safety reasons documented above. |
| 103 | + """ |
| 104 | + if method == "pipx": |
| 105 | + # `pipx` itself drives the venv — no need to find it manually. |
| 106 | + if shutil.which("pipx"): |
| 107 | + return ["pipx", "upgrade", _PACKAGE] |
| 108 | + # pipx not on PATH but maybe the package was installed via |
| 109 | + # `python -m pipx`. Try that as a fallback. |
| 110 | + return [sys.executable, "-m", "pipx", "upgrade", _PACKAGE] |
| 111 | + |
| 112 | + if method == "uv-tool": |
| 113 | + if shutil.which("uv"): |
| 114 | + return ["uv", "tool", "upgrade", _PACKAGE] |
| 115 | + return None # uv not findable — bail to "manual" path |
| 116 | + |
| 117 | + if method == "pip": |
| 118 | + # Use the same Python that's running the CLI. Reliable across venvs. |
| 119 | + return [sys.executable, "-m", "pip", "install", "--upgrade", _PACKAGE] |
| 120 | + |
| 121 | + return None # homebrew, binary, editable, unknown |
| 122 | + |
| 123 | + |
| 124 | +def _manual_command_hint(method: str) -> str: |
| 125 | + """Human-readable upgrade command for methods we don't auto-run.""" |
| 126 | + if method == "homebrew": |
| 127 | + return "brew update && brew upgrade dailybot" |
| 128 | + if method == "binary": |
| 129 | + if platform.system() == "Windows": |
| 130 | + return "irm https://cli.dailybot.com/install.ps1 | iex" |
| 131 | + return "curl -sSL https://cli.dailybot.com/install.sh | bash" |
| 132 | + if method == "editable": |
| 133 | + return "git pull # then 'pip install -e .' if dependencies changed" |
| 134 | + if method == "uv-tool": |
| 135 | + return "uv tool upgrade dailybot-cli" |
| 136 | + return f"pip install --upgrade {_PACKAGE}" |
| 137 | + |
| 138 | + |
| 139 | +def _print_status(current: str, latest: str | None) -> None: |
| 140 | + """Print the version status header common to all paths.""" |
| 141 | + print_info(f"Current version: {current}") |
| 142 | + if latest is None: |
| 143 | + print_warning("Could not reach PyPI to check for the latest version.") |
| 144 | + return |
| 145 | + if latest == current: |
| 146 | + print_info(f"Latest version: {latest}") |
| 147 | + return |
| 148 | + print_info(f"Latest version: {latest} (update available)") |
| 149 | + |
| 150 | + |
| 151 | +@click.command(name="upgrade") |
| 152 | +@click.option( |
| 153 | + "--dry-run", |
| 154 | + is_flag=True, |
| 155 | + default=False, |
| 156 | + help="Print the upgrade command but do not execute it.", |
| 157 | +) |
| 158 | +@click.option( |
| 159 | + "--force", |
| 160 | + is_flag=True, |
| 161 | + default=False, |
| 162 | + help="Run the upgrade even when already on the latest version.", |
| 163 | +) |
| 164 | +def upgrade(dry_run: bool, force: bool) -> None: |
| 165 | + """Upgrade the Dailybot CLI to the latest version on PyPI. |
| 166 | +
|
| 167 | + \b |
| 168 | + Auto-detects how the CLI was installed and runs the right upgrade |
| 169 | + command. For installs managed by an external tool (Homebrew, the |
| 170 | + Linux/Windows binary installers, editable dev installs), the command |
| 171 | + you should run is printed instead — running those automatically is |
| 172 | + risky (sudo prompts, file locks, dev iteration). |
| 173 | +
|
| 174 | + \b |
| 175 | + Examples: |
| 176 | + dailybot upgrade # check + upgrade if newer is available |
| 177 | + dailybot upgrade --dry-run # show what would happen, do nothing |
| 178 | + dailybot upgrade --force # reinstall even if already latest |
| 179 | + """ |
| 180 | + print_info("Checking PyPI for the latest version...") |
| 181 | + latest: str | None = _fetch_latest_pypi_version() |
| 182 | + _print_status(current=__version__, latest=latest) |
| 183 | + |
| 184 | + if latest == __version__ and not force: |
| 185 | + print_success("You're already on the latest version. Nothing to do.") |
| 186 | + return |
| 187 | + |
| 188 | + method: str = _detect_install_method() |
| 189 | + label: str = _METHOD_LABELS.get(method, method) |
| 190 | + install_path: Path = _resolve_install_path() |
| 191 | + print_info(f"Install method: {label} ({install_path})") |
| 192 | + |
| 193 | + if method == "editable": |
| 194 | + print_warning( |
| 195 | + "This is an editable (development) install. " |
| 196 | + "Refusing to auto-upgrade — manage it with git instead:" |
| 197 | + ) |
| 198 | + click.echo(f"\n {_manual_command_hint(method)}\n") |
| 199 | + return |
| 200 | + |
| 201 | + argv: list[str] | None = _build_upgrade_argv(method) |
| 202 | + |
| 203 | + if argv is None: |
| 204 | + # Methods we don't auto-run: homebrew, binary, uv-tool-not-found, unknown. |
| 205 | + print_warning( |
| 206 | + f"This is a {label} install. We don't run the package manager " |
| 207 | + "automatically; run this command yourself:" |
| 208 | + ) |
| 209 | + click.echo(f"\n {_manual_command_hint(method)}\n") |
| 210 | + return |
| 211 | + |
| 212 | + pretty_cmd: str = " ".join(argv) |
| 213 | + if dry_run: |
| 214 | + click.echo(f"\nWould run: {pretty_cmd}\n") |
| 215 | + return |
| 216 | + |
| 217 | + print_info(f"Running: {pretty_cmd}") |
| 218 | + click.echo() # blank line so the subprocess output is easy to scan |
| 219 | + try: |
| 220 | + subprocess.run(argv, check=True) |
| 221 | + except subprocess.CalledProcessError as exc: |
| 222 | + print_error( |
| 223 | + f"Upgrade command failed with exit code {exc.returncode}. " |
| 224 | + f"You can try running it manually:\n {pretty_cmd}" |
| 225 | + ) |
| 226 | + raise SystemExit(exc.returncode) |
| 227 | + except FileNotFoundError: |
| 228 | + print_error( |
| 229 | + f"Could not find the executable for {label}. " |
| 230 | + f"Try the manual command:\n {_manual_command_hint(method)}" |
| 231 | + ) |
| 232 | + raise SystemExit(1) |
| 233 | + |
| 234 | + click.echo() |
| 235 | + print_success("Upgrade complete. Run 'dailybot --version' to confirm.") |
0 commit comments