Automatically install the new aider version

This commit is contained in:
Paul Gauthier 2024-07-12 21:38:45 +01:00
parent 6c2a168327
commit 6c38306eab
6 changed files with 49 additions and 35 deletions

View file

@ -10,7 +10,7 @@ repos:
- id: black - id: black
args: ["--line-length", "100", "--preview"] args: ["--line-length", "100", "--preview"]
- repo: https://github.com/pycqa/flake8 - repo: https://github.com/pycqa/flake8
rev: 6.0.0 rev: 7.1.0
hooks: hooks:
- id: flake8 - id: flake8
args: ["--show-source"] args: ["--show-source"]

View file

@ -422,15 +422,16 @@ def get_parser(default_config_files, git_root):
help="Show the version number and exit", help="Show the version number and exit",
) )
group.add_argument( group.add_argument(
"--check-update", "--just-check-update",
action="store_true", action="store_true",
help="Check for updates and return status in the exit code", help="Check for updates and return status in the exit code",
default=False, default=False,
) )
group.add_argument( group.add_argument(
"--skip-check-update", "--check-update",
action="store_true", action=argparse.BooleanOptionalAction,
help="Skips checking for the update when the program runs", help="Check for new aider versions on launch",
default=True,
) )
group.add_argument( group.add_argument(
"--apply", "--apply",

View file

@ -97,24 +97,25 @@ pip_install_cmd = [
"https://download.pytorch.org/whl/cpu", "https://download.pytorch.org/whl/cpu",
] ]
pip_install_error = f""" pip_install_error = """
To use interactive /help you need to install HuggingFace embeddings: To use interactive /help you need to install HuggingFace embeddings:
pip install {' '.join(pip_install_cmd)} {cmd}
""" # noqa: E231 """ # noqa: E231
class Help: class Help:
def __init__(self, pip_install=False): def __init__(self, pip_install=False):
cmd = utils.get_pip_install(pip_install_cmd)
if pip_install: if pip_install:
utils.pip_install(pip_install_cmd) utils.run_install(cmd)
try: try:
from llama_index.core import Settings from llama_index.core import Settings
from llama_index.embeddings.huggingface import HuggingFaceEmbedding from llama_index.embeddings.huggingface import HuggingFaceEmbedding
except ImportError: except ImportError:
raise PipInstallHF(pip_install_error) raise PipInstallHF(pip_install_error.format(cmd=' '.join(cmd)))
os.environ["TOKENIZERS_PARALLELISM"] = "true" os.environ["TOKENIZERS_PARALLELISM"] = "true"
Settings.embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5") Settings.embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")

View file

@ -389,12 +389,12 @@ def main(argv=None, input=None, output=None, force_git_root=None, return_coder=F
if right_repo_root: if right_repo_root:
return main(argv, input, output, right_repo_root, return_coder=return_coder) return main(argv, input, output, right_repo_root, return_coder=return_coder)
if not args.skip_check_update: if args.just_check_update:
check_version(io.tool_error) update_available = check_version(io, just_check=True)
return 0 if not update_available else 1
if args.check_update: if args.check_update:
update_available = check_version(lambda msg: None) check_version(io)
return 0 if not update_available else 1
if args.models: if args.models:
models.print_matching_models(io, args.models) models.print_matching_models(io, args.models)

View file

@ -181,7 +181,8 @@ def split_chat_history_markdown(text, include_tool=False):
return messages return messages
def pip_install(args): def get_pip_install(args):
cmd = [ cmd = [
sys.executable, sys.executable,
"-m", "-m",
@ -189,10 +190,12 @@ def pip_install(args):
"install", "install",
] ]
cmd += args 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: try:
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, bufsize=1, universal_newlines=True) process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, bufsize=1, universal_newlines=True)
output = [] output = []
@ -200,7 +203,7 @@ def run_install_with_spinner(cmd):
for line in process.stdout: for line in process.stdout:
output.append(line) output.append(line)
print(f"\rInstalling... {next(spinner)}", end="", flush=True) print(f" Installing... {next(spinner)}", end="\r", flush=True)
return_code = process.wait() return_code = process.wait()

View file

@ -5,14 +5,16 @@ from pathlib import Path
import packaging.version import packaging.version
import aider import aider
from aider import utils
def check_version(print_cmd): def check_version(io, just_check=False):
fname = Path.home() / ".aider" / "caches" / "versioncheck" fname = Path.home() / ".aider" / "caches" / "versioncheck"
day = 60 * 60 * 24 day = 60 * 60 * 24
if fname.exists() and time.time() - fname.stat().st_mtime < day: if fname.exists() and time.time() - fname.stat().st_mtime < day:
return return
# To keep startup fast, avoid importing this unless needed
import requests import requests
try: try:
@ -24,24 +26,31 @@ def check_version(print_cmd):
is_update_available = packaging.version.parse(latest_version) > packaging.version.parse( is_update_available = packaging.version.parse(latest_version) > packaging.version.parse(
current_version 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: 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 return False
fname.parent.mkdir(parents=True, exist_ok=True)
fname.touch()
if __name__ == "__main__": if just_check:
check_version(print) 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