|
21 | 21 | PYTHON_VERSION_MARKER_FILE = ".build-python-version"
|
22 | 22 |
|
23 | 23 |
|
| 24 | +def download_and_unpack_archive(url: str, path: Path) -> None: |
| 25 | + """ |
| 26 | + Download the cross-build environment from the given URL and extract it to the given path. |
| 27 | +
|
| 28 | + Parameters |
| 29 | + ---------- |
| 30 | + url |
| 31 | + URL to download the cross-build environment from. |
| 32 | + path |
| 33 | + Path to extract the cross-build environment to. |
| 34 | + If the path already exists, raise an error. |
| 35 | + """ |
| 36 | + logger.info("Downloading Pyodide cross-build environment from %s", url) |
| 37 | + |
| 38 | + if path.exists(): |
| 39 | + raise FileExistsError(f"Path {path} already exists") |
| 40 | + |
| 41 | + try: |
| 42 | + resp = urlopen(url) |
| 43 | + data = resp.read() |
| 44 | + except Exception as e: |
| 45 | + raise ValueError( |
| 46 | + f"Failed to download cross-build environment from {url}" |
| 47 | + ) from e |
| 48 | + |
| 49 | + # FIXME: requests makes a verbose output (see: https://github.com/pyodide/pyodide/issues/4810) |
| 50 | + # r = requests.get(url) |
| 51 | + |
| 52 | + # if r.status_code != 200: |
| 53 | + # raise ValueError( |
| 54 | + # f"Failed to download cross-build environment from {url} (status code: {r.status_code})" |
| 55 | + # ) |
| 56 | + |
| 57 | + with NamedTemporaryFile(suffix=".tar") as f: |
| 58 | + f_path = Path(f.name) |
| 59 | + f_path.write_bytes(data) |
| 60 | + with warnings.catch_warnings(): |
| 61 | + # Python 3.12-3.13 emits a DeprecationWarning when using shutil.unpack_archive without a filter, |
| 62 | + # but filter doesn't work well for zip files, so we suppress the warning until we find a better solution. |
| 63 | + # https://github.com/python/cpython/issues/112760 |
| 64 | + warnings.simplefilter("ignore") |
| 65 | + shutil.unpack_archive(str(f_path), path) |
| 66 | + |
| 67 | + |
24 | 68 | class CrossBuildEnvManager:
|
25 | 69 | """
|
26 | 70 | Manager for the cross-build environment.
|
@@ -194,7 +238,7 @@ def install(
|
194 | 238 | download_path,
|
195 | 239 | )
|
196 | 240 | else:
|
197 |
| - self._download(download_url, download_path) |
| 241 | + download_and_unpack_archive(download_url, download_path) |
198 | 242 |
|
199 | 243 | try:
|
200 | 244 | # there is an redundant directory "xbuildenv" inside the xbuildenv archive
|
@@ -240,49 +284,6 @@ def _find_latest_version(self) -> str:
|
240 | 284 |
|
241 | 285 | return latest.version
|
242 | 286 |
|
243 |
| - def _download(self, url: str, path: Path) -> None: |
244 |
| - """ |
245 |
| - Download the cross-build environment from the given URL and extract it to the given path. |
246 |
| -
|
247 |
| - Parameters |
248 |
| - ---------- |
249 |
| - url |
250 |
| - URL to download the cross-build environment from. |
251 |
| - path |
252 |
| - Path to extract the cross-build environment to. |
253 |
| - If the path already exists, raise an error. |
254 |
| - """ |
255 |
| - logger.info("Downloading Pyodide cross-build environment from %s", url) |
256 |
| - |
257 |
| - if path.exists(): |
258 |
| - raise FileExistsError(f"Path {path} already exists") |
259 |
| - |
260 |
| - try: |
261 |
| - resp = urlopen(url) |
262 |
| - data = resp.read() |
263 |
| - except Exception as e: |
264 |
| - raise ValueError( |
265 |
| - f"Failed to download cross-build environment from {url}" |
266 |
| - ) from e |
267 |
| - |
268 |
| - # FIXME: requests makes a verbose output (see: https://github.com/pyodide/pyodide/issues/4810) |
269 |
| - # r = requests.get(url) |
270 |
| - |
271 |
| - # if r.status_code != 200: |
272 |
| - # raise ValueError( |
273 |
| - # f"Failed to download cross-build environment from {url} (status code: {r.status_code})" |
274 |
| - # ) |
275 |
| - |
276 |
| - with NamedTemporaryFile(suffix=".tar") as f: |
277 |
| - f_path = Path(f.name) |
278 |
| - f_path.write_bytes(data) |
279 |
| - with warnings.catch_warnings(): |
280 |
| - # Python 3.12-3.13 emits a DeprecationWarning when using shutil.unpack_archive without a filter, |
281 |
| - # but filter doesn't work well for zip files, so we suppress the warning until we find a better solution. |
282 |
| - # https://github.com/python/cpython/issues/112760 |
283 |
| - warnings.simplefilter("ignore") |
284 |
| - shutil.unpack_archive(str(f_path), path) |
285 |
| - |
286 | 287 | def _install_cross_build_packages(
|
287 | 288 | self, xbuildenv_root: Path, xbuildenv_pyodide_root: Path
|
288 | 289 | ) -> None:
|
|
0 commit comments