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
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"]

View file

@ -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",

View file

@ -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")

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:
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)

View file

@ -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()

View file

@ -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")
except Exception as 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 just_check:
return is_update_available
except Exception as err:
print_cmd(f"Error checking pypi for new version: {err}")
if not is_update_available:
return False
cmd = utils.get_pip_install(["--upgrade", "aider-chat"])
if __name__ == "__main__":
check_version(print)
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