From 6c38306eabe202350bdb563a6df28b827514db59 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Fri, 12 Jul 2024 21:38:45 +0100 Subject: [PATCH] Automatically install the new aider version --- .pre-commit-config.yaml | 2 +- aider/args.py | 9 +++++---- aider/help.py | 9 +++++---- aider/main.py | 8 ++++---- aider/utils.py | 11 ++++++---- aider/versioncheck.py | 45 ++++++++++++++++++++++++----------------- 6 files changed, 49 insertions(+), 35 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9222c3e25..daec59aae 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -10,7 +10,7 @@ repos: - id: black args: ["--line-length", "100", "--preview"] - repo: https://github.com/pycqa/flake8 - rev: 6.0.0 + rev: 7.1.0 hooks: - id: flake8 args: ["--show-source"] diff --git a/aider/args.py b/aider/args.py index eadc2b75c..1d5774f7c 100644 --- a/aider/args.py +++ b/aider/args.py @@ -422,15 +422,16 @@ def get_parser(default_config_files, git_root): help="Show the version number and exit", ) group.add_argument( - "--check-update", + "--just-check-update", action="store_true", help="Check for updates and return status in the exit code", default=False, ) group.add_argument( - "--skip-check-update", - action="store_true", - help="Skips checking for the update when the program runs", + "--check-update", + action=argparse.BooleanOptionalAction, + help="Check for new aider versions on launch", + default=True, ) group.add_argument( "--apply", diff --git a/aider/help.py b/aider/help.py index bc93a6250..d93b411ff 100755 --- a/aider/help.py +++ b/aider/help.py @@ -97,24 +97,25 @@ pip_install_cmd = [ "https://download.pytorch.org/whl/cpu", ] -pip_install_error = f""" +pip_install_error = """ To use interactive /help you need to install HuggingFace embeddings: -pip install {' '.join(pip_install_cmd)} +{cmd} """ # noqa: E231 class Help: def __init__(self, pip_install=False): + cmd = utils.get_pip_install(pip_install_cmd) if pip_install: - utils.pip_install(pip_install_cmd) + utils.run_install(cmd) try: from llama_index.core import Settings from llama_index.embeddings.huggingface import HuggingFaceEmbedding except ImportError: - raise PipInstallHF(pip_install_error) + raise PipInstallHF(pip_install_error.format(cmd=' '.join(cmd))) os.environ["TOKENIZERS_PARALLELISM"] = "true" Settings.embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5") diff --git a/aider/main.py b/aider/main.py index 76722595a..8e52501f4 100644 --- a/aider/main.py +++ b/aider/main.py @@ -389,12 +389,12 @@ def main(argv=None, input=None, output=None, force_git_root=None, return_coder=F if right_repo_root: return main(argv, input, output, right_repo_root, return_coder=return_coder) - if not args.skip_check_update: - check_version(io.tool_error) + if args.just_check_update: + update_available = check_version(io, just_check=True) + return 0 if not update_available else 1 if args.check_update: - update_available = check_version(lambda msg: None) - return 0 if not update_available else 1 + check_version(io) if args.models: models.print_matching_models(io, args.models) diff --git a/aider/utils.py b/aider/utils.py index 3b4bdfc72..3efdb84da 100644 --- a/aider/utils.py +++ b/aider/utils.py @@ -181,7 +181,8 @@ def split_chat_history_markdown(text, include_tool=False): return messages -def pip_install(args): +def get_pip_install(args): + cmd = [ sys.executable, "-m", @@ -189,10 +190,12 @@ def pip_install(args): "install", ] cmd += args + return cmd - run_install_with_spinner(cmd) +def run_install(cmd): + print() + print("Installing: ", ' '.join(cmd)) -def run_install_with_spinner(cmd): try: process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, bufsize=1, universal_newlines=True) output = [] @@ -200,7 +203,7 @@ def run_install_with_spinner(cmd): for line in process.stdout: output.append(line) - print(f"\rInstalling... {next(spinner)}", end="", flush=True) + print(f" Installing... {next(spinner)}", end="\r", flush=True) return_code = process.wait() diff --git a/aider/versioncheck.py b/aider/versioncheck.py index f5098c5aa..ac8f647fd 100644 --- a/aider/versioncheck.py +++ b/aider/versioncheck.py @@ -5,14 +5,16 @@ from pathlib import Path import packaging.version import aider +from aider import utils -def check_version(print_cmd): +def check_version(io, just_check=False): fname = Path.home() / ".aider" / "caches" / "versioncheck" day = 60 * 60 * 24 if fname.exists() and time.time() - fname.stat().st_mtime < day: return + # To keep startup fast, avoid importing this unless needed import requests try: @@ -24,24 +26,31 @@ def check_version(print_cmd): is_update_available = packaging.version.parse(latest_version) > packaging.version.parse( current_version ) - - if is_update_available: - print_cmd( - f"Newer version v{latest_version} is available. To upgrade, run:" # noqa: E231 - ) - py = sys.executable - if "pipx" in py: - print_cmd("pipx upgrade aider-chat") - else: - print_cmd(f"{py} -m pip install --upgrade aider-chat") - - fname.parent.mkdir(parents=True, exist_ok=True) - fname.touch() - return is_update_available except Exception as err: - print_cmd(f"Error checking pypi for new version: {err}") + io.tool_error(f"Error checking pypi for new version: {err}") return False + fname.parent.mkdir(parents=True, exist_ok=True) + fname.touch() -if __name__ == "__main__": - check_version(print) + if just_check: + return is_update_available + + if not is_update_available: + return False + + cmd = utils.get_pip_install(["--upgrade", "aider-chat"]) + + text = f""" +Newer aider version v{latest_version} is available. To upgrade, run: + + {' '.join(cmd)} +""" + io.tool_error(text) + + if io.confirm_ask("Run pip install?"): + if utils.run_install(cmd): + io.tool_output("Re-run aider to use new version.") + sys.exit() + + return True