mirror of
https://github.com/XiaoYouChR/Ghost-Downloader-3.git
synced 2026-12-06 23:05:34 +08:00
- 用 FileHandle 统一底层文件写入,Windows 优先走 C 扩展,不可用时回退纯 Python - FileWriter 改用单线程执行器连续提交批次,提高吞吐并降低延迟 - 补充 Windows 编译脚本,并接入打包和 CI 流程
93 lines
2.9 KiB
Python
93 lines
2.9 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
import platform
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import sysconfig
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
REPO = Path(__file__).resolve().parent.parent
|
|
SOURCE = REPO / "app" / "platform" / "_sysio.c"
|
|
|
|
|
|
def findMsvcScript(arch: str) -> Path | None:
|
|
programFilesX86 = os.environ.get("ProgramFiles(x86)", r"C:\Program Files (x86)")
|
|
vswhere = Path(programFilesX86) / "Microsoft Visual Studio" / "Installer" / "vswhere.exe"
|
|
if not vswhere.is_file():
|
|
return None
|
|
component = (
|
|
"Microsoft.VisualStudio.Component.VC.Tools.ARM64"
|
|
if arch == "arm64"
|
|
else "Microsoft.VisualStudio.Component.VC.Tools.x86.x64"
|
|
)
|
|
result = subprocess.run(
|
|
[
|
|
str(vswhere),
|
|
"-latest",
|
|
"-products",
|
|
"*",
|
|
"-requires",
|
|
component,
|
|
"-property",
|
|
"installationPath",
|
|
],
|
|
capture_output=True,
|
|
text=True,
|
|
check=False,
|
|
)
|
|
installDir = result.stdout.strip()
|
|
if not installDir:
|
|
return None
|
|
script = Path(installDir) / "VC" / "Auxiliary" / "Build" / "vcvarsall.bat"
|
|
return script if script.is_file() else None
|
|
|
|
|
|
def buildSysio() -> Path:
|
|
if sys.platform != "win32":
|
|
raise RuntimeError("app.platform._sysio C extension is Windows-only")
|
|
|
|
extSuffix = sysconfig.get_config_var("EXT_SUFFIX") or ".pyd"
|
|
output = SOURCE.with_name(f"_sysio{extSuffix}")
|
|
includeDir = Path(sysconfig.get_path("include"))
|
|
libsDir = Path(sys.base_prefix) / "libs"
|
|
pyLib = f"python{sys.version_info.major}{sys.version_info.minor}.lib"
|
|
arch = {"win-arm64": "arm64", "win32": "x86"}.get(sysconfig.get_platform().lower(), "x64")
|
|
|
|
with tempfile.TemporaryDirectory(prefix="gd3_build_sysio_") as tmpdir:
|
|
tmp = Path(tmpdir)
|
|
clCmd = (
|
|
f'cl /nologo /O2 /MD /LD /W4 /utf-8 '
|
|
f'/I"{includeDir}" "{SOURCE}" '
|
|
f'/Fe:"{output}" /Fo:"{tmp / "_sysio.obj"}" '
|
|
f'/link /LIBPATH:"{libsDir}" /IMPLIB:"{tmp / "_sysio.lib"}" {pyLib}'
|
|
)
|
|
if shutil.which("cl") is not None and os.environ.get("VSCMD_ARG_TGT_ARCH", "").lower() == arch:
|
|
command = clCmd
|
|
else:
|
|
msvcScript = findMsvcScript(arch)
|
|
if msvcScript is None:
|
|
raise RuntimeError("MSVC cl.exe not found; install Visual Studio C++ Build Tools")
|
|
vcvarsArch = (
|
|
"amd64_arm64"
|
|
if arch == "arm64" and platform.machine().lower() not in ("arm64", "aarch64")
|
|
else arch
|
|
)
|
|
command = f'call "{msvcScript}" {vcvarsArch} >nul && {clCmd}'
|
|
|
|
subprocess.run(command, shell=True, check=True)
|
|
|
|
return output
|
|
|
|
|
|
def main() -> int:
|
|
output = buildSysio()
|
|
print(f"Built {output}")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|