crogram-pythub 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- crogram_pythub-0.1.0.dist-info/METADATA +148 -0
- crogram_pythub-0.1.0.dist-info/RECORD +27 -0
- crogram_pythub-0.1.0.dist-info/WHEEL +5 -0
- crogram_pythub-0.1.0.dist-info/entry_points.txt +3 -0
- crogram_pythub-0.1.0.dist-info/licenses/LICENSE +21 -0
- crogram_pythub-0.1.0.dist-info/top_level.txt +1 -0
- pythub/__init__.py +22 -0
- pythub/__main__.py +6 -0
- pythub/analyzer.py +142 -0
- pythub/bootloader/bootex.c +1310 -0
- pythub/bootloader/miniz_LICENSE +22 -0
- pythub/bootloader/miniz_common.h +146 -0
- pythub/bootloader/miniz_export.h +5 -0
- pythub/bootloader/miniz_tinfl.c +765 -0
- pythub/bootloader/miniz_tinfl.h +148 -0
- pythub/builder.py +421 -0
- pythub/cli.py +348 -0
- pythub/config.py +77 -0
- pythub/deps.py +90 -0
- pythub/exe_archive.py +414 -0
- pythub/formats.py +227 -0
- pythub/gui.py +690 -0
- pythub/native_builder.py +71 -0
- pythub/pypa.py +213 -0
- pythub/runtime.py +336 -0
- pythub/signing.py +104 -0
- pythub/utils.py +330 -0
pythub/native_builder.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"""PyInstaller 引擎:本机平台生成真正的单文件二进制(可选,需已安装 pyinstaller)。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
import shutil
|
|
7
|
+
import subprocess
|
|
8
|
+
import sys
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import List
|
|
11
|
+
|
|
12
|
+
from pythub.builder import BuildSpec
|
|
13
|
+
from pythub.runtime import current_platform
|
|
14
|
+
|
|
15
|
+
SUPPORTED_FMTS = {"onefile", "onedir", "app", "dir"}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def build_native(spec: BuildSpec, logger: logging.Logger) -> List[Path]:
|
|
19
|
+
if spec.platform != current_platform():
|
|
20
|
+
raise RuntimeError(
|
|
21
|
+
f"PyInstaller 引擎仅支持本机平台 ({current_platform()}),"
|
|
22
|
+
f"跨平台请使用默认 zipapp 引擎(--engine zipapp)。"
|
|
23
|
+
)
|
|
24
|
+
try:
|
|
25
|
+
import PyInstaller # noqa: F401
|
|
26
|
+
except ImportError:
|
|
27
|
+
raise RuntimeError("PyInstaller 引擎需要先安装: pip install pyinstaller")
|
|
28
|
+
|
|
29
|
+
if spec.target_fmt not in SUPPORTED_FMTS:
|
|
30
|
+
raise ValueError(
|
|
31
|
+
f"PyInstaller 引擎支持格式: {', '.join(sorted(SUPPORTED_FMTS))}"
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
out = spec.output / f"{spec.name}-{spec.python}-{spec.platform}-{spec.arch}"
|
|
35
|
+
out.mkdir(parents=True, exist_ok=True)
|
|
36
|
+
work = out / ".build"
|
|
37
|
+
work.mkdir(exist_ok=True)
|
|
38
|
+
|
|
39
|
+
onefile = spec.target_fmt == "onefile"
|
|
40
|
+
cmd = [
|
|
41
|
+
sys.executable, "-m", "PyInstaller",
|
|
42
|
+
"--noconfirm", "--clean",
|
|
43
|
+
"--name", spec.name,
|
|
44
|
+
"--distpath", str(out),
|
|
45
|
+
"--workpath", str(work / "work"),
|
|
46
|
+
"--specpath", str(work / "spec"),
|
|
47
|
+
]
|
|
48
|
+
if onefile:
|
|
49
|
+
cmd.append("--onefile")
|
|
50
|
+
else:
|
|
51
|
+
cmd.append("--onedir")
|
|
52
|
+
if spec.windowed and sys.platform == "darwin":
|
|
53
|
+
cmd.append("--windowed")
|
|
54
|
+
if spec.requirements:
|
|
55
|
+
for r in spec.requirements:
|
|
56
|
+
cmd += ["--hidden-import", r]
|
|
57
|
+
cmd.append(str((spec.project_dir / spec.entry).resolve()))
|
|
58
|
+
|
|
59
|
+
logger.info("执行 PyInstaller(本机平台 %s)...", spec.platform)
|
|
60
|
+
proc = subprocess.run(cmd, capture_output=True, text=True)
|
|
61
|
+
if proc.returncode != 0:
|
|
62
|
+
logger.error(proc.stdout[-3000:] if proc.stdout else "")
|
|
63
|
+
raise RuntimeError(f"PyInstaller 打包失败:\n{proc.stderr[-3000:]}")
|
|
64
|
+
|
|
65
|
+
artifacts = []
|
|
66
|
+
for pat in ([f"{spec.name}.app", spec.name] if sys.platform == "darwin" else [spec.name]):
|
|
67
|
+
p = out / pat
|
|
68
|
+
if p.exists():
|
|
69
|
+
artifacts.append(p)
|
|
70
|
+
shutil.rmtree(work, ignore_errors=True)
|
|
71
|
+
return artifacts
|
pythub/pypa.py
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
"""PyPI 打包与发布:stdlib 实现 sdist/wheel 构建与 multipart 上传。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import base64
|
|
6
|
+
import hashlib
|
|
7
|
+
import logging
|
|
8
|
+
import os
|
|
9
|
+
import tarfile
|
|
10
|
+
import urllib.error
|
|
11
|
+
import urllib.request
|
|
12
|
+
import uuid
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from typing import List, Optional
|
|
15
|
+
|
|
16
|
+
from pythub.config import app_config, load_requirements
|
|
17
|
+
|
|
18
|
+
REPOSITORIES = {
|
|
19
|
+
"pypi": "https://upload.pypi.org/legacy/",
|
|
20
|
+
"testpypi": "https://test.pypi.org/legacy/",
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
_WHEEL_TAG = "py3-none-any"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _normalize(name: str) -> str:
|
|
27
|
+
return name.replace("-", "_").replace(".", "_").lower()
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def build_package(project_dir: Path, out_dir: Path, logger: logging.Logger) -> List[Path]:
|
|
31
|
+
"""构建 sdist (.tar.gz) 与 wheel (.whl)。"""
|
|
32
|
+
cfg = app_config(project_dir)
|
|
33
|
+
name, version = cfg["name"], cfg["version"]
|
|
34
|
+
nname = _normalize(name)
|
|
35
|
+
out_dir.mkdir(parents=True, exist_ok=True)
|
|
36
|
+
|
|
37
|
+
files = _collect_project(project_dir)
|
|
38
|
+
|
|
39
|
+
# ---- sdist ----
|
|
40
|
+
sdist_name = f"{name}-{version}.tar.gz"
|
|
41
|
+
sdist_path = out_dir / sdist_name
|
|
42
|
+
with tarfile.open(sdist_path, "w:gz") as tf:
|
|
43
|
+
pkg_info = _pkg_info(cfg)
|
|
44
|
+
base = f"{name}-{version}"
|
|
45
|
+
import io
|
|
46
|
+
|
|
47
|
+
tf.addfile(
|
|
48
|
+
tarfile.TarInfo(f"{base}/PKG-INFO"),
|
|
49
|
+
io.BytesIO(pkg_info.encode("utf-8")),
|
|
50
|
+
)
|
|
51
|
+
for rel, abs_path in files:
|
|
52
|
+
tf.add(abs_path, arcname=f"{base}/{rel.as_posix()}")
|
|
53
|
+
reqs = load_requirements(project_dir)
|
|
54
|
+
if reqs:
|
|
55
|
+
import io
|
|
56
|
+
|
|
57
|
+
content = "\n".join(reqs).encode("utf-8")
|
|
58
|
+
ti = tarfile.TarInfo(f"{base}/requirements.txt")
|
|
59
|
+
ti.size = len(content)
|
|
60
|
+
tf.addfile(ti, io.BytesIO(content))
|
|
61
|
+
|
|
62
|
+
# ---- wheel ----
|
|
63
|
+
wheel_name = f"{nname}-{version}-{_WHEEL_TAG}.whl"
|
|
64
|
+
wheel_path = out_dir / wheel_name
|
|
65
|
+
records: List[tuple] = []
|
|
66
|
+
payload: List[tuple] = [] # (arcname, bytes)
|
|
67
|
+
for rel, abs_path in files:
|
|
68
|
+
data = abs_path.read_bytes()
|
|
69
|
+
arc = rel.as_posix()
|
|
70
|
+
payload.append((arc, data))
|
|
71
|
+
records.append((arc, data))
|
|
72
|
+
dist_info = f"{nname}-{version}.dist-info"
|
|
73
|
+
metadata = _pkg_info(cfg).encode("utf-8")
|
|
74
|
+
wheel_meta = (
|
|
75
|
+
"Wheel-Version: 1.0\nGenerator: pythub\nRoot-Is-Purelib: true\n"
|
|
76
|
+
f"Tag: {_WHEEL_TAG}\n"
|
|
77
|
+
).encode("utf-8")
|
|
78
|
+
payload.append((f"{dist_info}/METADATA", metadata))
|
|
79
|
+
payload.append((f"{dist_info}/WHEEL", wheel_meta))
|
|
80
|
+
payload.append((f"{dist_info}/top_level.txt", b""))
|
|
81
|
+
# RECORD
|
|
82
|
+
record_lines = []
|
|
83
|
+
for arc, data in records:
|
|
84
|
+
digest = base64.urlsafe_b64encode(hashlib.sha256(data).digest()).rstrip(b"=").decode()
|
|
85
|
+
record_lines.append(f"{arc},sha256={digest},{len(data)}")
|
|
86
|
+
record_lines.append(f"{dist_info}/METADATA,,")
|
|
87
|
+
record_lines.append(f"{dist_info}/WHEEL,,")
|
|
88
|
+
record_lines.append(f"{dist_info}/RECORD,,")
|
|
89
|
+
payload.append((f"{dist_info}/RECORD", "\n".join(record_lines).encode("utf-8")))
|
|
90
|
+
|
|
91
|
+
import zipfile
|
|
92
|
+
|
|
93
|
+
with zipfile.ZipFile(wheel_path, "w", zipfile.ZIP_DEFLATED) as zf:
|
|
94
|
+
for arc, data in payload:
|
|
95
|
+
zi = zipfile.ZipInfo(arc, date_time=(2024, 1, 1, 0, 0, 0))
|
|
96
|
+
zi.external_attr = 0o644 << 16
|
|
97
|
+
zf.writestr(zi, data)
|
|
98
|
+
|
|
99
|
+
logger.info("构建完成: %s / %s", sdist_path.name, wheel_path.name)
|
|
100
|
+
return [sdist_path, wheel_path]
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _collect_project(project_dir: Path):
|
|
104
|
+
"""收集包文件:包目录(含 __init__.py)与顶层 py 文件。"""
|
|
105
|
+
files = []
|
|
106
|
+
exclude = {"__pycache__", ".git", "build", "dist", ".venv", "venv", ".pythub"}
|
|
107
|
+
for root, dirs, fnames in os.walk(project_dir):
|
|
108
|
+
rel_root = Path(root).relative_to(project_dir)
|
|
109
|
+
dirs[:] = [d for d in dirs if d not in exclude and not d.endswith(".egg-info")]
|
|
110
|
+
for fn in fnames:
|
|
111
|
+
if fn.endswith((".pyc", ".pyo")) or fn == "pythub.toml":
|
|
112
|
+
continue
|
|
113
|
+
rel = rel_root / fn
|
|
114
|
+
if rel_root == Path(".") and fn in ("requirements.txt",):
|
|
115
|
+
continue
|
|
116
|
+
files.append((rel, Path(root) / fn))
|
|
117
|
+
return files
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def _pkg_info(cfg: dict) -> str:
|
|
121
|
+
lines = [
|
|
122
|
+
"Metadata-Version: 2.1",
|
|
123
|
+
f"Name: {cfg['name']}",
|
|
124
|
+
f"Version: {cfg['version']}",
|
|
125
|
+
f"Summary: {cfg.get('description', '')}",
|
|
126
|
+
f"Author: {cfg.get('author', '')}",
|
|
127
|
+
]
|
|
128
|
+
return "\n".join(lines) + "\n"
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
# ---------------------------------------------------------------- 发布
|
|
132
|
+
|
|
133
|
+
def publish(
|
|
134
|
+
files: List[Path],
|
|
135
|
+
token: str,
|
|
136
|
+
repository: str = "pypi",
|
|
137
|
+
logger: logging.Logger = None,
|
|
138
|
+
) -> bool:
|
|
139
|
+
"""上传发行文件到 PyPI(stdlib multipart 实现)。"""
|
|
140
|
+
url = REPOSITORIES.get(repository)
|
|
141
|
+
if not url:
|
|
142
|
+
raise ValueError(f"未知仓库: {repository}(可选: {', '.join(REPOSITORIES)})")
|
|
143
|
+
if not token:
|
|
144
|
+
raise ValueError("请提供 PyPI API Token(pypi- 开头)")
|
|
145
|
+
|
|
146
|
+
auth = base64.b64encode(f"__token__:{token}".encode()).decode()
|
|
147
|
+
boundary = uuid.uuid4().hex
|
|
148
|
+
|
|
149
|
+
parts: List[bytes] = []
|
|
150
|
+
|
|
151
|
+
def field(name: str, value: str) -> bytes:
|
|
152
|
+
return (
|
|
153
|
+
f"--{boundary}\r\n"
|
|
154
|
+
f'Content-Disposition: form-data; name="{name}"\r\n\r\n'
|
|
155
|
+
f"{value}\r\n"
|
|
156
|
+
).encode("utf-8")
|
|
157
|
+
|
|
158
|
+
parts.append(field(":action", "file_upload"))
|
|
159
|
+
parts.append(field("protocol_version", "1"))
|
|
160
|
+
|
|
161
|
+
for f in files:
|
|
162
|
+
data = f.read_bytes()
|
|
163
|
+
digest = hashlib.sha256(data).hexdigest()
|
|
164
|
+
ctype = "application/zip" if f.suffix == ".whl" else "application/gzip"
|
|
165
|
+
parts.append(field("name", _dist_name_from(f)))
|
|
166
|
+
parts.append(field("version", _dist_version_from(f)))
|
|
167
|
+
parts.append(field("sha256_digest", digest))
|
|
168
|
+
if f.suffix == ".whl":
|
|
169
|
+
parts.append(field("filetype", "bdist_wheel"))
|
|
170
|
+
parts.append(field("pyversion", "3"))
|
|
171
|
+
else:
|
|
172
|
+
parts.append(field("filetype", "sdist"))
|
|
173
|
+
parts.append(
|
|
174
|
+
(
|
|
175
|
+
f"--{boundary}\r\n"
|
|
176
|
+
f'Content-Disposition: form-data; name="content"; filename="{f.name}"\r\n'
|
|
177
|
+
f"Content-Type: {ctype}\r\n\r\n"
|
|
178
|
+
).encode("utf-8")
|
|
179
|
+
)
|
|
180
|
+
parts.append(data + b"\r\n")
|
|
181
|
+
|
|
182
|
+
parts.append(f"--{boundary}--\r\n".encode("utf-8"))
|
|
183
|
+
body = b"".join(parts)
|
|
184
|
+
|
|
185
|
+
req = urllib.request.Request(
|
|
186
|
+
url,
|
|
187
|
+
data=body,
|
|
188
|
+
headers={
|
|
189
|
+
"Content-Type": f"multipart/form-data; boundary={boundary}",
|
|
190
|
+
"Authorization": f"Basic {auth}",
|
|
191
|
+
"User-Agent": "pythub/0.1",
|
|
192
|
+
},
|
|
193
|
+
method="POST",
|
|
194
|
+
)
|
|
195
|
+
try:
|
|
196
|
+
with urllib.request.urlopen(req, timeout=120) as resp:
|
|
197
|
+
text = resp.read().decode("utf-8", "replace")
|
|
198
|
+
if logger:
|
|
199
|
+
logger.info("发布成功 (%s): %s", repository, text[:200])
|
|
200
|
+
return True
|
|
201
|
+
except urllib.error.HTTPError as e:
|
|
202
|
+
detail = e.read().decode("utf-8", "replace")
|
|
203
|
+
raise RuntimeError(f"发布失败 HTTP {e.code}: {detail[:1000]}") from e
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def _dist_name_from(path: Path) -> str:
|
|
207
|
+
return path.name.rsplit("-", 2)[0] if path.suffix == ".whl" else path.name.rsplit("-", 1)[0]
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def _dist_version_from(path: Path) -> str:
|
|
211
|
+
if path.suffix == ".whl":
|
|
212
|
+
return path.name.rsplit("-", 2)[1]
|
|
213
|
+
return path.name.rsplit("-", 1)[1].rsplit(".tar", 1)[0]
|
pythub/runtime.py
ADDED
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
"""嵌入式 Python 运行时管理。
|
|
2
|
+
|
|
3
|
+
从 python-build-standalone 项目下载目标平台/架构/版本的独立 CPython,
|
|
4
|
+
缓存于 ~/.cache/pythub/runtimes/,用于让产物自带解释器(目标机器免装 Python)。
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import logging
|
|
10
|
+
import os
|
|
11
|
+
import shutil
|
|
12
|
+
import sys
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from typing import Optional, Tuple
|
|
15
|
+
|
|
16
|
+
from pythub import SUPPORTED_PYTHON
|
|
17
|
+
from pythub.utils import (
|
|
18
|
+
PYTHUB_CACHE_DIR,
|
|
19
|
+
ProgressLogger,
|
|
20
|
+
download_file,
|
|
21
|
+
find_executable,
|
|
22
|
+
safe_extract,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
RELEASE_TAG = "20241016"
|
|
26
|
+
|
|
27
|
+
PYTHON_PATCH_VERSIONS = {
|
|
28
|
+
"3.10": "3.10.15",
|
|
29
|
+
"3.11": "3.11.10",
|
|
30
|
+
"3.12": "3.12.7",
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
# (platform, arch) -> python-build-standalone triplet
|
|
34
|
+
TRIPLETS = {
|
|
35
|
+
("macos", "x86_64"): "x86_64-apple-darwin",
|
|
36
|
+
("macos", "aarch64"): "aarch64-apple-darwin",
|
|
37
|
+
("linux", "x86_64"): "x86_64-unknown-linux-gnu",
|
|
38
|
+
("linux", "aarch64"): "aarch64-unknown-linux-gnu",
|
|
39
|
+
("windows", "x86_64"): "x86_64-pc-windows-msvc-shared",
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def detect_current() -> Tuple[str, str, str]:
|
|
44
|
+
import platform
|
|
45
|
+
import sys
|
|
46
|
+
|
|
47
|
+
if sys.platform == "darwin":
|
|
48
|
+
plat = "macos"
|
|
49
|
+
mach = platform.machine()
|
|
50
|
+
arch = "aarch64" if mach == "arm64" else ("x86_64" if mach == "x86_64" else mach)
|
|
51
|
+
elif sys.platform == "win32":
|
|
52
|
+
plat = "windows"
|
|
53
|
+
arch = "aarch64" if platform.machine().lower() in ("arm64", "aarch64") else "x86_64"
|
|
54
|
+
elif sys.platform.startswith("linux"):
|
|
55
|
+
plat = "linux"
|
|
56
|
+
mach = platform.machine()
|
|
57
|
+
arch = "aarch64" if mach == "aarch64" else "x86_64"
|
|
58
|
+
else:
|
|
59
|
+
raise RuntimeError(f"不支持的平台: {sys.platform}")
|
|
60
|
+
major, minor = sys.version_info[:2]
|
|
61
|
+
pyver = f"{major}.{minor}"
|
|
62
|
+
if pyver not in SUPPORTED_PYTHON:
|
|
63
|
+
pyver = min(SUPPORTED_PYTHON, key=lambda v: abs(int(v.split(".")[1]) - minor))
|
|
64
|
+
return plat, arch, pyver
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def current_platform() -> str:
|
|
68
|
+
return detect_current()[0]
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def patch_version(pyver: str) -> str:
|
|
72
|
+
if pyver not in PYTHON_PATCH_VERSIONS:
|
|
73
|
+
raise ValueError(f"不支持的 Python 版本: {pyver}(可用: {', '.join(SUPPORTED_PYTHON)})")
|
|
74
|
+
return PYTHON_PATCH_VERSIONS[pyver]
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def runtime_dir(platform: str, arch: str, pyver: str) -> Path:
|
|
78
|
+
full = patch_version(pyver)
|
|
79
|
+
return PYTHUB_CACHE_DIR / "runtimes" / f"cpython-{full}-{RELEASE_TAG}-{platform}-{arch}"
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def fetch_runtime(
|
|
83
|
+
platform: str,
|
|
84
|
+
arch: str,
|
|
85
|
+
pyver: str,
|
|
86
|
+
logger: Optional[logging.Logger] = None,
|
|
87
|
+
) -> Path:
|
|
88
|
+
"""获取(必要时下载)目标平台嵌入式解释器目录。
|
|
89
|
+
|
|
90
|
+
并发安全: 先在暂存目录完成下载/解压/瘦身并写入 .pythub-ok 标记,
|
|
91
|
+
再原子 rename 到最终位置;若另一进程抢先完成(缓存已就位),直接
|
|
92
|
+
复用其结果,避免半成品目录被并发进程误删/误用。"""
|
|
93
|
+
triplet = TRIPLETS.get((platform, arch))
|
|
94
|
+
if not triplet:
|
|
95
|
+
raise ValueError(f"不支持的 目标平台/架构: {platform}/{arch}")
|
|
96
|
+
full = patch_version(pyver)
|
|
97
|
+
cache = runtime_dir(platform, arch, pyver)
|
|
98
|
+
|
|
99
|
+
if (cache / ".pythub-ok").exists():
|
|
100
|
+
# 旧版缓存未瘦身则就地瘦身
|
|
101
|
+
marker = (cache / ".pythub-ok").read_text(encoding="utf-8")
|
|
102
|
+
if "slim" not in marker:
|
|
103
|
+
pruned = prune_runtime(cache, logger)
|
|
104
|
+
if logger and pruned:
|
|
105
|
+
logger.info("运行时缓存已瘦身: 移除 %s", ", ".join(pruned))
|
|
106
|
+
(cache / ".pythub-ok").write_text(
|
|
107
|
+
marker.rstrip("\n") + "\nslim\n", encoding="utf-8"
|
|
108
|
+
)
|
|
109
|
+
return cache
|
|
110
|
+
|
|
111
|
+
# 优先使用官方 stripped 变体(无调试符号,体积显著更小,且跨平台无需本地 strip)
|
|
112
|
+
archive_name = f"cpython-{full}+{RELEASE_TAG}-{triplet}-install_only_stripped.tar.gz"
|
|
113
|
+
url = f"https://github.com/indygreg/python-build-standalone/releases/download/{RELEASE_TAG}/{archive_name}"
|
|
114
|
+
|
|
115
|
+
import tempfile
|
|
116
|
+
|
|
117
|
+
with tempfile.TemporaryDirectory(prefix="pythub-dl-") as td:
|
|
118
|
+
tmp = Path(td)
|
|
119
|
+
archive = tmp / archive_name
|
|
120
|
+
if logger:
|
|
121
|
+
logger.info("下载嵌入式 Python %s (%s/%s) ...", pyver, platform, arch)
|
|
122
|
+
download_file(
|
|
123
|
+
url,
|
|
124
|
+
archive,
|
|
125
|
+
logger=logger,
|
|
126
|
+
progress=ProgressLogger(logger, f"Python {pyver}") if logger else None,
|
|
127
|
+
)
|
|
128
|
+
if logger:
|
|
129
|
+
logger.info("解压运行时...")
|
|
130
|
+
extract_runtime(archive, tmp / "extract")
|
|
131
|
+
root = tmp / "extract" / "python"
|
|
132
|
+
if not root.exists():
|
|
133
|
+
raise RuntimeError(f"压缩包结构异常,缺少 python/ 目录: {archive_name}")
|
|
134
|
+
cache.parent.mkdir(parents=True, exist_ok=True)
|
|
135
|
+
# 暂存目录(同父目录保证 rename 原子性),瘦身与标记先在暂存完成
|
|
136
|
+
staging = cache.parent / f".{cache.name}.tmp-{os.getpid()}"
|
|
137
|
+
if staging.exists():
|
|
138
|
+
shutil.rmtree(staging, ignore_errors=True)
|
|
139
|
+
shutil.move(str(root), str(staging))
|
|
140
|
+
pruned = prune_runtime(staging, logger)
|
|
141
|
+
if logger and pruned:
|
|
142
|
+
logger.info("运行时瘦身完成: 移除 %s", ", ".join(pruned))
|
|
143
|
+
(staging / ".pythub-ok").write_text(f"{full}\n{triplet}\nslim\n", encoding="utf-8")
|
|
144
|
+
|
|
145
|
+
# 就位: 另一并发进程可能已完成,双重检查后原子 rename
|
|
146
|
+
if cache.exists():
|
|
147
|
+
if (cache / ".pythub-ok").exists():
|
|
148
|
+
shutil.rmtree(staging, ignore_errors=True)
|
|
149
|
+
return cache
|
|
150
|
+
shutil.rmtree(cache) # 无标记的异常残留
|
|
151
|
+
try:
|
|
152
|
+
os.rename(staging, cache)
|
|
153
|
+
except OSError:
|
|
154
|
+
# rename 竞态失败: 若目标已完整就位则复用
|
|
155
|
+
if (cache / ".pythub-ok").exists():
|
|
156
|
+
shutil.rmtree(staging, ignore_errors=True)
|
|
157
|
+
return cache
|
|
158
|
+
raise
|
|
159
|
+
|
|
160
|
+
return cache
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
# ---------------------------------------------------------------- 运行时瘦身
|
|
164
|
+
|
|
165
|
+
# 解压时直接跳过的成员(运行应用用不到的构建期内容;同时规避压缩包内符号链接循环)
|
|
166
|
+
_SKIP_TOP_DIRS = ("include", "share", "libs", "pypy")
|
|
167
|
+
_SKIP_STDLIB_DIRS = (
|
|
168
|
+
"test", "idlelib", "lib2to3", "pydoc_data", "turtledemo", "ensurepip",
|
|
169
|
+
)
|
|
170
|
+
_SKIP_SITE_DIRS = ("pip", "setuptools", "pkg_resources", "wheel", "_distutils_hack")
|
|
171
|
+
_SKIP_SEGMENTS = ("__pycache__", "demos")
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def _runtime_member_keep(name: str) -> bool:
|
|
175
|
+
"""判断 tar 压缩包成员是否保留(相对运行时根 python/ 过滤)。"""
|
|
176
|
+
parts = [p for p in name.split("/") if p not in ("", ".")]
|
|
177
|
+
if not parts:
|
|
178
|
+
return False
|
|
179
|
+
if any(seg in _SKIP_SEGMENTS for seg in parts):
|
|
180
|
+
return False
|
|
181
|
+
if name.endswith(_PRUNE_FILE_SUFFIXES):
|
|
182
|
+
return False
|
|
183
|
+
rel = parts[1:] if parts[0] == "python" else parts
|
|
184
|
+
if rel and rel[0] in _SKIP_TOP_DIRS:
|
|
185
|
+
return False
|
|
186
|
+
if len(rel) >= 2 and rel[0] in ("lib", "Lib") and rel[1].startswith("python3"):
|
|
187
|
+
if len(rel) >= 3 and rel[2] in _SKIP_STDLIB_DIRS:
|
|
188
|
+
return False
|
|
189
|
+
if len(rel) >= 4 and rel[2] == "site-packages" and rel[3] in _SKIP_SITE_DIRS:
|
|
190
|
+
return False
|
|
191
|
+
return True
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def extract_runtime(archive: Path, dest: Path) -> None:
|
|
195
|
+
"""解压运行时 tar 包,边解压边跳过无需保留的成员(防符号链接循环、省 IO)。"""
|
|
196
|
+
import tarfile
|
|
197
|
+
|
|
198
|
+
dest.mkdir(parents=True, exist_ok=True)
|
|
199
|
+
with tarfile.open(archive) as tf:
|
|
200
|
+
members = [m for m in tf.getmembers() if _runtime_member_keep(m.name)]
|
|
201
|
+
tf.extractall(dest, members=members)
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
# 运行时根目录下可整体移除的目录(头文件/文档/静态链接库等构建期内容)
|
|
205
|
+
_PRUNE_ROOT_DIRS = ("include", "share", "libs", "pypy", "lib/pkgconfig")
|
|
206
|
+
|
|
207
|
+
# 标准库内可安全移除的目录(运行应用用不到)
|
|
208
|
+
_PRUNE_STDLIB_DIRS = (
|
|
209
|
+
"test", "idlelib", "lib2to3", "pydoc_data", "turtledemo", "ensurepip",
|
|
210
|
+
"site-packages/pip", "site-packages/setuptools", "site-packages/pkg_resources",
|
|
211
|
+
"site-packages/wheel", "site-packages/_distutils_hack",
|
|
212
|
+
"site-packages/setuptools-*.dist-info", "site-packages/pip-*.dist-info",
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
# bin 内无需保留的工具脚本(保留 python3 本体)
|
|
216
|
+
_BIN_PRUNE_PREFIXES = ("idle", "pydoc", "2to3", "pip", "wheel", "easy_install")
|
|
217
|
+
|
|
218
|
+
# 调试符号与静态库文件
|
|
219
|
+
_PRUNE_FILE_SUFFIXES = (".pdb", ".a", ".lib", ".pyo")
|
|
220
|
+
|
|
221
|
+
# setuptools 残留的 .pth(引用已移除的 _distutils_hack 会产生启动警告)
|
|
222
|
+
_PRUNE_PTH_FILES = ("distutils-precedence.pth",)
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def _find_stdlib_dirs(runtime: Path) -> list:
|
|
226
|
+
"""定位标准库目录(unix: lib/python3.X;windows: Lib)。"""
|
|
227
|
+
result = []
|
|
228
|
+
lib = runtime / "lib"
|
|
229
|
+
if lib.is_dir():
|
|
230
|
+
result += [d for d in lib.iterdir() if d.is_dir() and d.name.startswith("python3.")]
|
|
231
|
+
if (runtime / "Lib" / "os.py").exists():
|
|
232
|
+
result.append(runtime / "Lib")
|
|
233
|
+
return result
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def prune_runtime(runtime: Path, logger=None) -> list:
|
|
237
|
+
"""就地瘦身运行时:移除调试符号、测试套件、pip/setuptools、文档、__pycache__,
|
|
238
|
+
并 strip 二进制符号表(unix)。典型可减小 50%~80% 体积。返回移除内容类别列表。"""
|
|
239
|
+
import subprocess
|
|
240
|
+
|
|
241
|
+
is_windows = (runtime / "python.exe").exists()
|
|
242
|
+
removed_kinds = []
|
|
243
|
+
|
|
244
|
+
def rm_dir(d: Path) -> bool:
|
|
245
|
+
if d.is_dir():
|
|
246
|
+
shutil.rmtree(d, ignore_errors=True)
|
|
247
|
+
return True
|
|
248
|
+
return False
|
|
249
|
+
|
|
250
|
+
def rm_file(f: Path) -> bool:
|
|
251
|
+
try:
|
|
252
|
+
f.unlink()
|
|
253
|
+
return True
|
|
254
|
+
except OSError:
|
|
255
|
+
return False
|
|
256
|
+
|
|
257
|
+
# 1. 根目录构建期内容
|
|
258
|
+
n = 0
|
|
259
|
+
for d in _PRUNE_ROOT_DIRS:
|
|
260
|
+
if rm_dir(runtime / d):
|
|
261
|
+
n += 1
|
|
262
|
+
if n:
|
|
263
|
+
removed_kinds.append("include/share/静态库")
|
|
264
|
+
|
|
265
|
+
# 2. 标准库内运行期用不到的包
|
|
266
|
+
n = 0
|
|
267
|
+
for stdlib in _find_stdlib_dirs(runtime):
|
|
268
|
+
for pat in _PRUNE_STDLIB_DIRS:
|
|
269
|
+
for d in stdlib.glob(pat):
|
|
270
|
+
if rm_dir(d):
|
|
271
|
+
n += 1
|
|
272
|
+
if n:
|
|
273
|
+
removed_kinds.append("test/idlelib/pip等stdlib冗余包")
|
|
274
|
+
|
|
275
|
+
# 3. bin 工具脚本(保留 python 本体)
|
|
276
|
+
n = 0
|
|
277
|
+
for bindir in (runtime / "bin", runtime / "Scripts"):
|
|
278
|
+
if bindir.is_dir():
|
|
279
|
+
for f in bindir.iterdir():
|
|
280
|
+
if f.name.startswith(_BIN_PRUNE_PREFIXES):
|
|
281
|
+
n += rm_file(f)
|
|
282
|
+
if n:
|
|
283
|
+
removed_kinds.append("bin工具脚本")
|
|
284
|
+
|
|
285
|
+
# 4. 调试符号文件(.pdb/.a/.lib)、setuptools 残留 .pth、__pycache__、tcl/tk demos
|
|
286
|
+
for stdlib in _find_stdlib_dirs(runtime):
|
|
287
|
+
sp = stdlib / "site-packages"
|
|
288
|
+
if sp.is_dir():
|
|
289
|
+
for name in _PRUNE_PTH_FILES:
|
|
290
|
+
rm_file(sp / name)
|
|
291
|
+
n = 0
|
|
292
|
+
for root, dirs, files in os.walk(runtime):
|
|
293
|
+
for d in list(dirs):
|
|
294
|
+
if d == "__pycache__" or d == "demos":
|
|
295
|
+
n += rm_dir(Path(root) / d)
|
|
296
|
+
dirs.remove(d)
|
|
297
|
+
for f in files:
|
|
298
|
+
if f.endswith(_PRUNE_FILE_SUFFIXES):
|
|
299
|
+
n += rm_file(Path(root) / f)
|
|
300
|
+
if n:
|
|
301
|
+
removed_kinds.append("调试符号/__pycache__")
|
|
302
|
+
|
|
303
|
+
# 5. strip 二进制符号表(仅当运行时平台 == 当前主机平台时才可执行)
|
|
304
|
+
strip = find_executable("strip")
|
|
305
|
+
is_linux = bool(list((runtime / "lib").glob("libpython*.so*")))
|
|
306
|
+
is_darwin = bool(list((runtime / "lib").glob("libpython*.dylib*")))
|
|
307
|
+
host_match = (is_darwin and sys.platform == "darwin") or \
|
|
308
|
+
(is_linux and sys.platform.startswith("linux"))
|
|
309
|
+
if strip and not is_windows and host_match:
|
|
310
|
+
binaries = []
|
|
311
|
+
bdir = runtime / "bin"
|
|
312
|
+
if bdir.is_dir():
|
|
313
|
+
binaries += [p for p in bdir.iterdir() if p.is_file() and not p.is_symlink()]
|
|
314
|
+
binaries += list((runtime / "lib").glob("libpython*.dylib*"))
|
|
315
|
+
binaries += list((runtime / "lib").glob("libpython*.so*"))
|
|
316
|
+
for dyn in _find_stdlib_dirs(runtime):
|
|
317
|
+
dynload = dyn / "lib-dynload"
|
|
318
|
+
if dynload.is_dir():
|
|
319
|
+
binaries += list(dynload.glob("*.so"))
|
|
320
|
+
for b in binaries:
|
|
321
|
+
subprocess.run([strip, "-x", str(b)], capture_output=True)
|
|
322
|
+
if is_darwin and find_executable("codesign"):
|
|
323
|
+
# darwin 二进制需重新 ad-hoc 签名(Apple Silicon 强制要求)
|
|
324
|
+
for b in binaries:
|
|
325
|
+
subprocess.run(["codesign", "--force", "--sign", "-", str(b)],
|
|
326
|
+
capture_output=True)
|
|
327
|
+
removed_kinds.append("符号表(strip)")
|
|
328
|
+
|
|
329
|
+
return removed_kinds
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def interpreter_path(runtime: Path, platform: str) -> Path:
|
|
333
|
+
if platform == "windows":
|
|
334
|
+
return runtime / "python.exe"
|
|
335
|
+
py3 = runtime / "bin" / "python3"
|
|
336
|
+
return py3 if py3.exists() else runtime / "bin" / "python"
|