Skip to content

Commit fea6656

Browse files
authored
Merge pull request #56 from guyush1/parallel-download-musl-toolchain
Download musl toolchains in parallel
2 parents 97f2b84 + f8e6fa3 commit fea6656

File tree

2 files changed

+39
-25
lines changed

2 files changed

+39
-25
lines changed

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ RUN apt update && apt install -y \
2525
patch \
2626
pkg-config \
2727
python3.12 \
28-
python3-requests \
28+
python3-aiohttp \
2929
libpython3-dev \
3030
texinfo \
3131
wget \

src/docker_utils/download_musl_toolchains.py

+38-24
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
#!/usr/bin/env python3.12
22

3-
import requests
3+
from typing import List
4+
from pathlib import Path
5+
46
import tarfile
7+
import tempfile
58
import os
69
import shutil
10+
import asyncio
711

8-
from pathlib import Path
12+
import aiohttp
913

1014
ARCHS = {
1115
"x86_64" : "https://more.musl.cc/11/x86_64-linux-musl/x86_64-linux-musl-cross.tgz",
@@ -17,39 +21,49 @@
1721
}
1822
CHUNK_SIZE = 65536
1923
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:
3435
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))
3652

3753
def add_to_path(curr_path: str, package_path: Path):
3854
new_path = str((package_path / "bin").resolve())
3955
if curr_path != "":
4056
return new_path + ":" + curr_path
4157
return new_path
4258

43-
4459
def main():
4560
os.mkdir(MUSL_TOOLCHAINS_DIR)
4661

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))
5367

5468
# Fix the x86_64 dynamic loader if needed:
5569
# Unfortunately, the internal gdb build scripts builds some binaries (that generate documentation)

0 commit comments

Comments
 (0)