diff --git a/build/rust/download_rust_toolchain_aux.py b/build/rust/download_rust_toolchain_aux.py index a5d87842428b..69ee7770b3dc 100755 --- a/build/rust/download_rust_toolchain_aux.py +++ b/build/rust/download_rust_toolchain_aux.py @@ -4,7 +4,6 @@ # License, v. 2.0. If a copy of the MPL was not distributed with this file, # You can obtain one at https://mozilla.org/MPL/2.0/. -import glob from urllib.error import HTTPError import os from pathlib import Path @@ -20,27 +19,24 @@ import build_rust RUST_TOOLCHAIN = build_rust.RUST_TOOLCHAIN_OUT_DIR -BRAVE_PREFIX = '.brave-' -PACKAGE = os.path.join( - RUST_TOOLCHAIN, f'{BRAVE_PREFIX}{build_rust_toolchain_aux.package_name()}') -PATTERN = os.path.join(RUST_TOOLCHAIN, f'{BRAVE_PREFIX}*') +BRAVE_RUST_TOOLCHAIN_AUX = os.path.join(RUST_TOOLCHAIN, + '.brave_rust_toolchain_aux') def is_download_needed(): - return os.getenv('SKIP_DOWNLOAD_RUST_TOOLCHAIN_AUX', - '0') != '1' and not Path(PACKAGE).exists() + if os.getenv('SKIP_DOWNLOAD_RUST_TOOLCHAIN_AUX', '0') == '1': + return False + package_name = '' + try: + with open(BRAVE_RUST_TOOLCHAIN_AUX) as file: + package_name = file.readline().rstrip() + except Exception: + pass + return package_name != build_rust_toolchain_aux.package_name() -def maybe_remove_old_package(): - if entries := glob.glob(PATTERN): - [entry] = entries - print(f'Removing {entry}') - os.unlink(entry) - - -def install_new_package(src_dir): - print(f'Installing {PACKAGE}') +def install_package(src_dir): rust_lld_path = os.path.join(RUST_TOOLCHAIN, 'bin', build_rust_toolchain_aux.RUST_LLD) wasm32_unknown_unknown_path = os.path.join( @@ -51,7 +47,7 @@ def install_new_package(src_dir): Path(rust_lld_path).unlink(missing_ok=True) shutil.rmtree(wasm32_unknown_unknown_path, ignore_errors=True) - # move new artifacts to their final places + # move new artifacts into their final places shutil.move(os.path.join(src_dir, build_rust_toolchain_aux.RUST_LLD), rust_lld_path) shutil.move( @@ -59,20 +55,23 @@ def install_new_package(src_dir): wasm32_unknown_unknown_path) +def save_package_name(): + with open(BRAVE_RUST_TOOLCHAIN_AUX, 'w+') as file: + file.write(f'{build_rust_toolchain_aux.package_name()}\n') + + def main(): if not is_download_needed(): print(f'Skipping {Path(__file__).stem}') return - maybe_remove_old_package() - with tempfile.TemporaryDirectory(dir=RUST_TOOLCHAIN) as temp_dir: try: deps.DownloadAndUnpack( f'{deps_config.DEPS_PACKAGES_URL}/rust-toolchain-aux/{build_rust_toolchain_aux.package_name()}', - temp_dir, - output_file=PACKAGE) - install_new_package(temp_dir) + temp_dir) + install_package(temp_dir) + save_package_name() except HTTPError as error: if error.code == 403: print("If you see this error message, " @@ -80,7 +79,6 @@ def main(): "and doing a Chromium bump - please visit " "https://ci.brave.com/view/rust and use your branch " "to build the required Rust auxiliary package.") - os.unlink(PACKAGE) raise diff --git a/script/deps.py b/script/deps.py index 889f8000b817..5b644aecb9b1 100755 --- a/script/deps.py +++ b/script/deps.py @@ -70,15 +70,14 @@ def EnsureDirExists(path): os.makedirs(path) -def DownloadAndUnpack(url, output_dir, output_file=None, path_prefix=None): +def DownloadAndUnpack(url, output_dir, path_prefix=None): """Download an archive from url and extract into output_dir. If path_prefix is not None, only extract files whose paths within the archive start with path_prefix.""" - with (open(output_file, 'w+b') if output_file else - tempfile.NamedTemporaryFile(delete=False)) as file: + with tempfile.NamedTemporaryFile(delete=False) as tmp_file: try: - DownloadUrl(url, file) - file.close() + DownloadUrl(url, tmp_file) + tmp_file.close() try: os.unlink(output_dir) except OSError: @@ -87,9 +86,9 @@ def DownloadAndUnpack(url, output_dir, output_file=None, path_prefix=None): EnsureDirExists(output_dir) if url.endswith('.zip'): assert path_prefix is None - extract_zip(file.name, output_dir) + extract_zip(tmp_file.name, output_dir) else: - t = tarfile.open(file.name, mode='r:*') + t = tarfile.open(tmp_file.name, mode='r:*') members = None if path_prefix is not None: members = [ @@ -98,5 +97,4 @@ def DownloadAndUnpack(url, output_dir, output_file=None, path_prefix=None): ] t.extractall(path=output_dir, members=members) finally: - if not output_file: - os.unlink(file.name) + os.unlink(tmp_file.name)