forked from zsol/dotslash-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_dotslash_file.py
149 lines (131 loc) · 4.45 KB
/
make_dotslash_file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import argparse
from typing import Final
import urllib.request
import json
from dataclasses import dataclass
@dataclass(frozen=True)
class Asset:
name: str
browser_download_url: str
state: str
size: int
@dataclass(frozen=True)
class Release:
name: str
tag_name: str
draft: bool
prerelease: bool
assets: list[Asset]
@dataclass(frozen=True)
class PlatformConfig:
marker: str
path: str
PLATFORMS: Final[dict[str, PlatformConfig]] = {
"linux-aarch64": PlatformConfig(
marker="aarch64-unknown-linux-gnu-lto-full",
path="python/install/bin/python",
),
"linux-x86_64": PlatformConfig(
marker="x86_64_v3-unknown-linux-gnu-pgo+lto-full",
path="python/install/bin/python",
),
"macos-aarch64": PlatformConfig(
marker="aarch64-apple-darwin-pgo+lto-full",
path="python/install/bin/python",
),
"macos-x86_64": PlatformConfig(
marker="x86_64-apple-darwin-pgo+lto-full",
path="python/install/bin/python",
),
# "windows-aarch64": PlatformConfig(...),
"windows-x86_64": PlatformConfig(
marker="x86_64-pc-windows-msvc-shared-pgo-full",
path="python/install/python.exe",
),
}
def fetch_latest_release() -> Release:
with urllib.request.urlopen(
"https://api.github.com/repos/astral-sh/python-build-standalone/releases/latest"
) as response:
if response.status != 200:
raise RuntimeError(f"Failed to fetch release info: {response.status}")
release_data = json.loads(response.read())
return Release(
name=release_data["name"],
tag_name=release_data["tag_name"],
draft=release_data["draft"],
prerelease=release_data["prerelease"],
assets=[
Asset(
name=asset["name"],
browser_download_url=asset["browser_download_url"],
state=asset["state"],
size=asset["size"],
)
for asset in release_data["assets"]
],
)
def find_asset_for_platform(release: Release, version: str, platform: str) -> Asset:
ret: list[Asset] = []
marker = PLATFORMS[platform].marker
for asset in release.assets:
if not asset.name.startswith(f"cpython-{version}."):
continue
if asset.name.endswith(".sha256"):
continue
if marker in asset.name:
ret.append(asset)
if len(ret) > 1:
raise ValueError(
f"More than one asset matches {marker!r} for {version=}, {platform=}. Candidates: {[a.name for a in ret]}"
)
if len(ret) == 0:
raise ValueError(
f"No assets found for {version=}, {platform=} in {release.name=}"
)
return ret[0]
def platform_descriptor(platform: str, asset: Asset) -> object:
extension = "tar.zst"
if not asset.browser_download_url.endswith(extension):
raise ValueError(
f"Asset for {platform=} isn't supported by dotslash: {asset.browser_download_url}"
)
with urllib.request.urlopen(f"{asset.browser_download_url}.sha256") as response:
if response.status != 200:
raise RuntimeError(
f"Failed to fetch digest for {asset.browser_download_url}: {response=}"
)
digest = bytes(response.read().strip()).decode()
return {
"size": asset.size,
"hash": "sha256",
"digest": digest,
"format": extension,
"path": PLATFORMS[platform].path,
"providers": [{"url": asset.browser_download_url}],
# this is needed on linux/macos so the interpreter can locate the stdlib and
# other runtime files; it's ignored on windows
"arg0": "underlying-executable",
}
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--cpython-version", default="3.13")
args = parser.parse_args()
version = args.cpython_version
assert isinstance(version, str)
rel = fetch_latest_release()
platform_descriptors = {
platform: platform_descriptor(
platform, find_asset_for_platform(rel, version, platform)
)
for platform in PLATFORMS.keys()
}
descriptor = {
"name": f"cpython-{version}",
"platforms": platform_descriptors,
}
print("#!/usr/bin/env dotslash")
print()
print(json.dumps(descriptor, indent=2, sort_keys=True))
if __name__ == "__main__":
main()