Debian Codegraph @

tools/update_launchers.py

raw symbols SCIP
1 from __future__ import annotations
2
3 import asyncio
4 import io
5 import pathlib
6 import sys
7 import tarfile
8 from typing import Any
9
10 import httpx
11
12 DISTLIB_URL = "https://pypi.org/simple/distlib"
13 VENDOR_DIR = (
14 pathlib.Path(__file__)
15 .parent.parent.joinpath("src", "installer", "_scripts")
16 .resolve()
17 )
18
19 LAUNCHERS = [
20 "t32.exe",
21 "t64.exe",
22 "t_arm.exe",
23 "t64-arm.exe",
24 "w32.exe",
25 "w64.exe",
26 "w_arm.exe",
27 "w64-arm.exe",
28 ]
29
30
31 async def _get_distlib_page(client: httpx.AsyncClient) -> Any:
32 resp = await client.get(
33 DISTLIB_URL,
34 headers={"ACCEPT": "application/vnd.pypi.simple.v1+json"},
35 follow_redirects=True,
36 )
37 return resp.json()
38
39
40 def _get_link_from_response(json_response: dict[str, Any]) -> tuple[str, str] | None:
41 version = max(version_str.split(".") for version_str in json_response["versions"])
42 filename = f"distlib-{'.'.join(version)}.tar.gz"
43 for file_info in json_response["files"]:
44 if file_info["filename"] == filename:
45 return file_info["url"], filename
46 return None
47
48
49 async def _download_distlib(client: httpx.AsyncClient) -> bytes | None:
50 distlib_page = await _get_distlib_page(client)
51 data = None
52 if pair := _get_link_from_response(distlib_page):
53 url, filename = pair
54 print(f" Fetching {filename}")
55 resp = await client.get(url)
56 data = await resp.aread()
57 return data
58
59
60 def _get_launcher_path(names: list[str], launcher: str) -> str | None:
61 if paths := [name for name in names if launcher in name]:
62 return paths[0]
63 return None
64
65
66 def _unpack_launchers_to_dir(distlib_tar: bytes) -> None:
67 print("Unpacking launchers")
68 with tarfile.open(fileobj=io.BytesIO(distlib_tar)) as file:
69 for launcher_name in LAUNCHERS:
70 if (path := _get_launcher_path(file.getnames(), launcher_name)) and (
71 launcher := file.extractfile(path)
72 ):
73 print(f" Unpacking {launcher_name}")
74 VENDOR_DIR.joinpath(launcher_name).write_bytes(launcher.read())
75
76
77 async def main() -> None:
78 print(f"Downloading into {VENDOR_DIR} ...")
79 async with httpx.AsyncClient() as client:
80 data = await _download_distlib(client)
81 if data is not None:
82 _unpack_launchers_to_dir(data)
83 print("Scripts update failed!")
84
85
86 def _patch_windows() -> None:
87 # https://github.com/encode/httpx/issues/914
88 if sys.platform.startswith("win"):
89 asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
90
91
92 if __name__ == "__main__":
93 _patch_windows()
94 asyncio.run(main())