|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import os |
| 4 | +import pathlib |
| 5 | +import shutil |
3 | 6 | import subprocess |
4 | 7 | import sys |
| 8 | +import time |
| 9 | +import urllib.request |
5 | 10 | from collections import deque |
6 | 11 |
|
7 | 12 |
|
8 | 13 | def escape_workflow_command(value: str) -> str: |
9 | 14 | return value.replace("%", "%25").replace("\r", "%0D").replace("\n", "%0A") |
10 | 15 |
|
11 | 16 |
|
| 17 | +def prefetch_windows_virtualenv() -> None: |
| 18 | + if sys.platform != "win32": |
| 19 | + return |
| 20 | + |
| 21 | + # Keep these values aligned with the cibuildwheel version pinned in the |
| 22 | + # workflow. cibuildwheel otherwise downloads this file only once, making a |
| 23 | + # transient GitHub error fail the entire Windows matrix. |
| 24 | + version = "20.35.2" |
| 25 | + cache_path = ( |
| 26 | + pathlib.Path(os.environ["LOCALAPPDATA"]) / "pypa" / "cibuildwheel" / "Cache" / f"virtualenv-{version}.pyz" |
| 27 | + ) |
| 28 | + if cache_path.exists(): |
| 29 | + return |
| 30 | + |
| 31 | + url = f"https://github.com/pypa/get-virtualenv/releases/download/{version}/virtualenv.pyz" |
| 32 | + cache_path.parent.mkdir(parents=True, exist_ok=True) |
| 33 | + temporary_path = cache_path.with_suffix(".tmp") |
| 34 | + |
| 35 | + for attempt in range(1, 7): |
| 36 | + try: |
| 37 | + print(f"Prefetching {url} (attempt {attempt}/6)", flush=True) |
| 38 | + with urllib.request.urlopen(url, timeout=60) as response, temporary_path.open("wb") as output: |
| 39 | + shutil.copyfileobj(response, output) |
| 40 | + temporary_path.replace(cache_path) |
| 41 | + return |
| 42 | + except Exception: |
| 43 | + temporary_path.unlink(missing_ok=True) |
| 44 | + if attempt == 6: |
| 45 | + raise |
| 46 | + time.sleep(2 ** (attempt - 1)) |
| 47 | + |
| 48 | + |
| 49 | +prefetch_windows_virtualenv() |
| 50 | + |
12 | 51 | process = subprocess.Popen( |
13 | 52 | [sys.executable, "-m", "cibuildwheel", "--output-dir", "wheelhouse"], |
14 | 53 | stdout=subprocess.PIPE, |
|
0 commit comments