| 359 | |
| 360 | |
| 361 | def download_file( |
| 362 | ctx: Context, |
| 363 | url: str, |
| 364 | dest: pathlib.Path, |
| 365 | auth: tuple[str, str] | None = None, |
| 366 | headers: dict[str, str] | None = None, |
| 367 | ) -> pathlib.Path: |
| 368 | ctx.info(f"Downloading {dest.name!r} @ {url} ...") |
| 369 | if sys.platform == "win32": |
| 370 | # We don't want to use curl on Windows, it doesn't work |
| 371 | curl = None |
| 372 | else: |
| 373 | curl = shutil.which("curl") |
| 374 | if curl is not None: |
| 375 | command = [curl, "-sS", "-L"] |
| 376 | if headers: |
| 377 | for key, value in headers.items(): |
| 378 | command.extend(["-H", f"{key}: {value}"]) |
| 379 | command.extend(["-o", str(dest), url]) |
| 380 | ret = ctx.run(*command) |
| 381 | if ret.returncode: |
| 382 | ctx.error(f"Failed to download {url}") |
| 383 | ctx.exit(1) |
| 384 | return dest |
| 385 | wget = shutil.which("wget") |
| 386 | if wget is not None: |
| 387 | with ctx.chdir(dest.parent): |
| 388 | command = [wget, "--no-verbose"] |
| 389 | if headers: |
| 390 | for key, value in headers.items(): |
| 391 | command.append(f"--header={key}: {value}") |
| 392 | command.append(url) |
| 393 | ret = ctx.run(*command) |
| 394 | if ret.returncode: |
| 395 | ctx.error(f"Failed to download {url}") |
| 396 | ctx.exit(1) |
| 397 | return dest |
| 398 | # NOTE the stream=True parameter below |
| 399 | with ctx.web as web: |
| 400 | if headers: |
| 401 | web.headers.update(headers) |
| 402 | with web.get(url, stream=True, auth=auth) as r: |
| 403 | r.raise_for_status() |
| 404 | with dest.open("wb") as f: |
| 405 | for chunk in r.iter_content(chunk_size=8192): |
| 406 | if chunk: |
| 407 | f.write(chunk) |
| 408 | return dest |
| 409 | |
| 410 | |
| 411 | def get_platform_and_arch_from_slug(slug: str) -> tuple[str, str]: |