Skip to content

Commit

Permalink
fix-up 3
Browse files Browse the repository at this point in the history
  • Loading branch information
szilardszaloki committed Feb 28, 2025
1 parent c0d6cac commit a85b4ae
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 32 deletions.
44 changes: 21 additions & 23 deletions build/rust/download_rust_toolchain_aux.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -51,36 +47,38 @@ 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(
os.path.join(src_dir, build_rust_toolchain_aux.WASM32_UNKNOWN_UNKNOWN),
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, "
"it means you're likely on the rebase team "
"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


Expand Down
16 changes: 7 additions & 9 deletions script/deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 = [
Expand All @@ -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)

0 comments on commit a85b4ae

Please sign in to comment.