jev-training-data/push_log.py

41 lines
1.8 KiB
Python

#!/usr/bin/env python3
"""push_log.py — 训练容器内把日志/文件回传 gitea(监控通道)
用法: python3 push_log.py <本地文件> <仓库内路径> (env: GITEA_USER/GITEA_TOKEN)
"""
import sys, os, base64, json, urllib.request, urllib.error
REPO = "dingbin.wang/jev-training-data"
MAX_PUSH = 200_000 # 日志只推尾部 200KB
def api(method, path, body=None):
url = "https://gitea.d-robotics.cc/api/v1" + path
data = json.dumps(body).encode() if body is not None else None
req = urllib.request.Request(url, data=data, method=method,
headers={"Content-Type": "application/json"})
auth = base64.b64encode(f"{os.environ['GITEA_USER']}:{os.environ['GITEA_TOKEN']}".encode()).decode()
req.add_header("Authorization", "Basic " + auth)
try:
with urllib.request.urlopen(req, timeout=30) as r:
return r.status, json.loads(r.read() or b"{}")
except urllib.error.HTTPError as e:
return e.code, json.loads(e.read() or b"{}")
def put_file(path, content: bytes, msg):
status, cur = api("GET", f"/repos/{REPO}/contents/{path}")
body = {"message": msg, "content": base64.b64encode(content).decode(), "branch": "main"}
if status == 200 and isinstance(cur, dict) and cur.get("sha"):
if cur.get("size") == len(content):
return "unchanged"
body["sha"] = cur["sha"]
s, r = api("PUT", f"/repos/{REPO}/contents/{path}", body)
if s not in (200, 201):
raise RuntimeError(f"PUT {path} -> {s} {json.dumps(r)[:200]}")
return s
if __name__ == "__main__":
local, remote = sys.argv[1], sys.argv[2]
data = open(local, "rb").read()
if len(data) > MAX_PUSH:
data = b"...[截断,仅尾部]...\n" + data[-MAX_PUSH:]
print(f"push {remote} ({len(data)//1024}KB) ->", put_file(remote, data, f"ci: {remote}"))