|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +import stat |
| 4 | +import sys |
| 5 | +from argparse import ArgumentParser |
| 6 | +from pathlib import Path |
| 7 | +from tempfile import NamedTemporaryFile |
| 8 | +from typing import Optional, Tuple, Union |
| 9 | + |
| 10 | +import i18n |
| 11 | +import requests |
| 12 | +from brainframe.cli import __version__, config, frozen_utils, print_utils |
| 13 | +from packaging import version |
| 14 | + |
| 15 | +from .utils import command |
| 16 | + |
| 17 | +_RELEASES_URL_PREFIX = "https://{subdomain}aotu.ai" |
| 18 | +_BINARY_URL = "{prefix}/releases/brainframe-cli/brainframe" |
| 19 | +_LATEST_TAG_URL = "{prefix}/releases/brainframe-cli/latest" |
| 20 | + |
| 21 | + |
| 22 | +@command("self-update") |
| 23 | +def self_update(): |
| 24 | + _parse_args() |
| 25 | + |
| 26 | + if not frozen_utils.is_frozen(): |
| 27 | + print_utils.fail_translate("self-update.not-frozen") |
| 28 | + |
| 29 | + executable_path = Path(sys.executable) |
| 30 | + |
| 31 | + # Check if the user has permissions to overwrite the executable in its |
| 32 | + # current location |
| 33 | + if not os.access(executable_path, os.W_OK): |
| 34 | + error_message = i18n.t( |
| 35 | + "general.file-bad-write-permissions", path=executable_path |
| 36 | + ) |
| 37 | + error_message += "\n" |
| 38 | + error_message += i18n.t("general.retry-as-root") |
| 39 | + print_utils.fail(error_message) |
| 40 | + |
| 41 | + credentials = config.staging_credentials() |
| 42 | + |
| 43 | + prefix = _RELEASES_URL_PREFIX.format( |
| 44 | + subdomain="staging." if config.is_staging.value else "" |
| 45 | + ) |
| 46 | + binary_url = _BINARY_URL.format(prefix=prefix) |
| 47 | + |
| 48 | + current_version = version.parse(__version__) |
| 49 | + latest_version = _latest_version(prefix, credentials) |
| 50 | + |
| 51 | + if current_version >= latest_version: |
| 52 | + print_utils.fail_translate( |
| 53 | + "self-update.already-up-to-date", |
| 54 | + current_version=current_version, |
| 55 | + latest_version=latest_version, |
| 56 | + ) |
| 57 | + |
| 58 | + # Get the updated executable |
| 59 | + print_utils.translate("self-update.downloading") |
| 60 | + response = requests.get(binary_url, auth=credentials, stream=True) |
| 61 | + if not response.ok: |
| 62 | + print_utils.fail_translate( |
| 63 | + "self-update.error-downloading", |
| 64 | + status_code=response.status_code, |
| 65 | + error_message=response.text, |
| 66 | + ) |
| 67 | + |
| 68 | + with NamedTemporaryFile("wb") as new_executable: |
| 69 | + for block in response.iter_content(_BLOCK_SIZE): |
| 70 | + new_executable.write(block) |
| 71 | + new_executable.flush() |
| 72 | + |
| 73 | + # Set the result as executable |
| 74 | + current_stat = executable_path.stat() |
| 75 | + executable_path.chmod( |
| 76 | + current_stat.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH |
| 77 | + ) |
| 78 | + |
| 79 | + # Overwrite the existing executable with the new one |
| 80 | + # Really excited for copy3, coming this summer |
| 81 | + shutil.copy2(new_executable.name, executable_path) |
| 82 | + |
| 83 | + print() |
| 84 | + print_utils.translate( |
| 85 | + "self-update.complete", color=print_utils.Color.GREEN |
| 86 | + ) |
| 87 | + |
| 88 | + |
| 89 | +def _latest_version( |
| 90 | + url_prefix: str, credentials: Optional[Tuple[str, str]], |
| 91 | +) -> Union[version.LegacyVersion, version.Version]: |
| 92 | + latest_tag_url = _LATEST_TAG_URL.format(prefix=url_prefix) |
| 93 | + response = requests.get(latest_tag_url, auth=credentials) |
| 94 | + |
| 95 | + if not response.ok: |
| 96 | + print_utils.fail_translate( |
| 97 | + "self-update.error-getting-latest-version", |
| 98 | + status_code=response.status_code, |
| 99 | + error_message=response.text, |
| 100 | + ) |
| 101 | + |
| 102 | + return version.parse(response.text) |
| 103 | + |
| 104 | + |
| 105 | +def _parse_args(): |
| 106 | + parser = ArgumentParser( |
| 107 | + description=i18n.t("self-update.description"), |
| 108 | + usage=i18n.t("self-update.usage"), |
| 109 | + ) |
| 110 | + |
| 111 | + return parser.parse_args(sys.argv[2:]) |
| 112 | + |
| 113 | + |
| 114 | +_BLOCK_SIZE = 1024000 |
| 115 | +"""The block size to read files at. Chosen from this answer: |
| 116 | +https://stackoverflow.com/a/3673731 |
| 117 | +""" |
0 commit comments