"""把当前版本的 Windows 压缩包发到公开仓库的 Release。 优先使用环境变量 GITEA_TOKEN。没有令牌时使用本机已经保存的 Git 登录。 """ from __future__ import annotations import json import os import subprocess from pathlib import Path import httpx ROOT = Path(__file__).resolve().parents[1] API = "https://git.asio.asia/api/v1/repos/nixevol/RaptDramaDump" ASSET = ROOT / "dist" / "VideosDownTools-windows-x64.zip" NOTES = "导入 DramaBox 凭据后会先读取个人信息,确认登录成功后才进入。" PART_SIZE = 40 * 1024 * 1024 def client() -> httpx.Client: token = os.environ.get("GITEA_TOKEN", "").strip() headers = {"Authorization": f"token {token}"} if token else {} auth = None if not token: env = os.environ.copy() env["GIT_TERMINAL_PROMPT"] = "0" proc = subprocess.run( ["git", "credential", "fill"], input="protocol=https\nhost=git.asio.asia\n\n", text=True, capture_output=True, env=env, check=False, ) username = "" password = "" for line in proc.stdout.splitlines(): if line.startswith("username="): username = line.split("=", 1)[1] elif line.startswith("password="): password = line.split("=", 1)[1] if not username or not password: raise SystemExit("没有可用的发布登录") auth = (username, password) return httpx.Client(headers=headers, auth=auth, timeout=60.0) def main() -> None: if not ASSET.is_file(): raise SystemExit("还没有压缩包") version = str(json.loads((ROOT / "src-tauri" / "tauri.conf.json").read_text(encoding="utf-8")).get("version") or "").strip() if not version: raise SystemExit("没有版本号") with client() as http: created = http.post( API + "/releases", json={ "tag_name": f"v{version}", "name": version, "target_commitish": "master", "body": NOTES, "draft": False, "prerelease": False, }, ) if created.status_code == 409: created = http.get(API + f"/releases/tags/v{version}") if created.status_code >= 300: raise SystemExit(f"创建版本失败 {created.status_code}") release = created.json() assets = release.get("assets") if isinstance(release.get("assets"), list) else [] names = {item.get("name") for item in assets if isinstance(item, dict)} if ASSET.name in names or any(str(name).startswith(ASSET.name + ".part") for name in names): print(f"已有附件 {version}") return _upload(http, release.get("id"), ASSET) print(f"已发布 {version}") def _upload(http: httpx.Client, release_id, path: Path) -> None: data = path.read_bytes() uploaded = _post_asset(http, release_id, path.name, data) if uploaded.status_code != 413: if uploaded.status_code >= 300: raise SystemExit(f"上传压缩包失败 {uploaded.status_code}") return count = (len(data) + PART_SIZE - 1) // PART_SIZE for index in range(count): part = data[index * PART_SIZE : (index + 1) * PART_SIZE] name = f"{path.name}.part{index + 1:03d}" uploaded = _post_asset(http, release_id, name, part) if uploaded.status_code >= 300: raise SystemExit(f"上传压缩包失败 {uploaded.status_code}") def _post_asset(http: httpx.Client, release_id, name: str, data: bytes) -> httpx.Response: return http.post( f"{API}/releases/{release_id}/assets", params={"name": name}, headers={"Content-Type": "application/octet-stream"}, content=data, timeout=600.0, ) if __name__ == "__main__": main()