|
1 | 1 | #!/usr/bin/env python3.12
|
2 | 2 |
|
3 |
| -import requests |
| 3 | +from typing import List |
| 4 | +from pathlib import Path |
| 5 | + |
4 | 6 | import tarfile
|
| 7 | +import tempfile |
5 | 8 | import os
|
6 | 9 | import shutil
|
| 10 | +import asyncio |
7 | 11 |
|
8 |
| -from pathlib import Path |
| 12 | +import aiohttp |
9 | 13 |
|
10 | 14 | ARCHS = {
|
11 | 15 | "x86_64" : "https://more.musl.cc/11/x86_64-linux-musl/x86_64-linux-musl-cross.tgz",
|
|
17 | 21 | }
|
18 | 22 | CHUNK_SIZE = 65536
|
19 | 23 | MUSL_TOOLCHAINS_DIR = Path("/musl-toolchains")
|
20 |
| -ENTRYPOINT = "/entrypoint.sh" |
21 |
| - |
22 |
| -def download_file(url: str, filename: str): |
23 |
| - print(f"Downloading {filename}") |
24 |
| - with requests.get(url, stream=True) as r: |
25 |
| - r.raise_for_status() |
26 |
| - with open(filename, "wb") as f: |
27 |
| - for chunk in r.iter_content(chunk_size=CHUNK_SIZE): |
28 |
| - f.write(chunk) |
29 |
| - print(f"{filename} downloaded.") |
30 |
| - |
31 |
| -def extract_tarball(filename: str, dst: Path): |
32 |
| - print(f"Extracting {filename}") |
33 |
| - with tarfile.open(filename, "r:gz") as tar: |
| 24 | +ENTRYPOINT = Path("/entrypoint.sh") |
| 25 | + |
| 26 | +async def download_file(url: str, filename: str): |
| 27 | + async with aiohttp.ClientSession() as session: |
| 28 | + async with session.get(url) as response: |
| 29 | + with open(filename, 'wb') as f: |
| 30 | + async for data in response.content.iter_chunked(CHUNK_SIZE): |
| 31 | + f.write(data) |
| 32 | + |
| 33 | +def extract_tarfile(filename: str, dst: Path): |
| 34 | + with tarfile.open(filename, "r") as tar: |
34 | 35 | tar.extractall(path=dst)
|
35 |
| - print(f"{filename} extracted") |
| 36 | + |
| 37 | +async def download_tarfile(tar_url: str, extraction_dir: Path): |
| 38 | + with tempfile.NamedTemporaryFile() as named_tempfile: |
| 39 | + await download_file(tar_url, named_tempfile.name) |
| 40 | + |
| 41 | + # Tarfile extraction is still being done synchronously. |
| 42 | + extract_tarfile(named_tempfile.name, extraction_dir) |
| 43 | + |
| 44 | + print(f"Downloaded & Extracted: {tar_url!r}") |
| 45 | + |
| 46 | +async def download_archs() -> List[str]: |
| 47 | + print(f"Downloading toolchains for architectures: {', '.join(ARCHS.keys())}") |
| 48 | + |
| 49 | + async with asyncio.TaskGroup() as tg: |
| 50 | + for url in ARCHS.values(): |
| 51 | + tg.create_task(download_tarfile(url, MUSL_TOOLCHAINS_DIR)) |
36 | 52 |
|
37 | 53 | def add_to_path(curr_path: str, package_path: Path):
|
38 | 54 | new_path = str((package_path / "bin").resolve())
|
39 | 55 | if curr_path != "":
|
40 | 56 | return new_path + ":" + curr_path
|
41 | 57 | return new_path
|
42 | 58 |
|
43 |
| - |
44 | 59 | def main():
|
45 | 60 | os.mkdir(MUSL_TOOLCHAINS_DIR)
|
46 | 61 |
|
47 |
| - updated_path = "" |
48 |
| - for arch, url in ARCHS.items(): |
49 |
| - filename = url.split("/")[-1] |
50 |
| - download_file(url, filename) |
51 |
| - extract_tarball(filename, MUSL_TOOLCHAINS_DIR) |
52 |
| - updated_path = add_to_path(updated_path, MUSL_TOOLCHAINS_DIR / filename.removesuffix(".tgz")) |
| 62 | + asyncio.run(download_archs()) |
| 63 | + |
| 64 | + updated_path = "$PATH" |
| 65 | + for musl_arch_dir in os.scandir(MUSL_TOOLCHAINS_DIR): |
| 66 | + updated_path = add_to_path(updated_path, Path(musl_arch_dir.path)) |
53 | 67 |
|
54 | 68 | # Fix the x86_64 dynamic loader if needed:
|
55 | 69 | # Unfortunately, the internal gdb build scripts builds some binaries (that generate documentation)
|
|
0 commit comments