|
| 1 | +import asyncio |
| 2 | +import subprocess |
| 3 | +import sys |
| 4 | +import time |
| 5 | +from loguru import logger |
| 6 | +from pathlib import Path |
| 7 | +from shlex import split |
| 8 | + |
| 9 | +ROOT_DIR = Path(__file__).parent.parent |
| 10 | + |
| 11 | + |
| 12 | +def get_version() -> str: |
| 13 | + """Extract the version as current git commit hash""" |
| 14 | + result = subprocess.run( |
| 15 | + split("git rev-parse HEAD"), |
| 16 | + check=True, |
| 17 | + capture_output=True, |
| 18 | + cwd=ROOT_DIR, |
| 19 | + ) |
| 20 | + commit = result.stdout.decode().strip() |
| 21 | + assert len(commit) == 40, f"Invalid commit hash: {commit}" |
| 22 | + return commit[:8] |
| 23 | + |
| 24 | + |
| 25 | +def pull_latest_version() -> None: |
| 26 | + """ |
| 27 | + Pull the latest version from git. |
| 28 | + This uses `git pull --rebase`, so if any changes were made to the local repository, |
| 29 | + this will try to apply them on top of origin's changes. This is intentional, as we |
| 30 | + don't want to overwrite any local changes. However, if there are any conflicts, |
| 31 | + this will abort the rebase and return to the original state. |
| 32 | + The conflicts are expected to happen rarely since validator is expected |
| 33 | + to be used as-is. |
| 34 | + """ |
| 35 | + try: |
| 36 | + subprocess.run(split("git pull --rebase --autostash"), check=True, cwd=ROOT_DIR) |
| 37 | + except subprocess.CalledProcessError as exc: |
| 38 | + logger.error("Failed to pull, reverting: %s", exc) |
| 39 | + subprocess.run(split("git rebase --abort"), check=True, cwd=ROOT_DIR) |
| 40 | + |
| 41 | + |
| 42 | +def upgrade_packages() -> None: |
| 43 | + """ |
| 44 | + Upgrade python packages by running `pip install --upgrade -r requirements.txt`. |
| 45 | + Notice: this won't work if some package in `requirements.txt` is downgraded. |
| 46 | + Ignored as this is unlikely to happen. |
| 47 | + """ |
| 48 | + |
| 49 | + logger.info("Upgrading packages") |
| 50 | + try: |
| 51 | + subprocess.run( |
| 52 | + split(f"{sys.executable} -m pip install -e ."), |
| 53 | + check=True, |
| 54 | + cwd=ROOT_DIR, |
| 55 | + ) |
| 56 | + except subprocess.CalledProcessError as exc: |
| 57 | + logger.error("Failed to upgrade packages, proceeding anyway. %s", exc) |
| 58 | + |
| 59 | + |
| 60 | +async def autoupdate_loop() -> None: |
| 61 | + """ |
| 62 | + Async version of autoupdate that runs alongside the validator. |
| 63 | + Checks for updates every hour and applies them if available. |
| 64 | + """ |
| 65 | + current_version = latest_version = get_version() |
| 66 | + logger.info("Current version: %s", current_version) |
| 67 | + |
| 68 | + try: |
| 69 | + while True: |
| 70 | + await asyncio.sleep(3600) # Wait 1 hour between checks |
| 71 | + |
| 72 | + pull_latest_version() |
| 73 | + latest_version = get_version() |
| 74 | + logger.info("Latest version: %s", latest_version) |
| 75 | + |
| 76 | + if latest_version != current_version: |
| 77 | + logger.info( |
| 78 | + "Upgraded to latest version: %s -> %s", |
| 79 | + current_version, |
| 80 | + latest_version, |
| 81 | + ) |
| 82 | + upgrade_packages() |
| 83 | + current_version = latest_version |
| 84 | + |
| 85 | + except asyncio.CancelledError: |
| 86 | + logger.info("Autoupdate task cancelled") |
| 87 | + raise |
0 commit comments