diff --git a/scripts/publish-release.py b/scripts/publish-release.py new file mode 100644 index 0000000..3d496da --- /dev/null +++ b/scripts/publish-release.py @@ -0,0 +1,63 @@ +"""发 release 附件:用 Python 而不是 PowerShell(后者序列化中文/多行会翻车)""" +import json +import os +import sys +import urllib.request as u + +WS = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +API = 'http://192.168.100.103:8080/api/v1/repos/l/doubao-clone' +AUTH = 'Basic ' + __import__('base64').b64encode(b'l:232711Aa').decode() +APK = os.path.join(WS, 'dist-android', 'doubao-debug.apk') +NOTES = os.path.join(WS, 'scripts', '.tmp-v201.txt') + + +def call(method, path, data=None, ctype='application/json', raw=None): + body = raw if raw is not None else ( + json.dumps(data).encode('utf-8') if data is not None else None) + req = u.Request(API + path, data=body, method=method, + headers={'Authorization': AUTH, 'Content-Type': ctype}) + with u.urlopen(req, timeout=300) as r: + text = r.read().decode('utf-8', 'ignore') + return json.loads(text) if text.strip().startswith(('{', '[')) else text + + +# 找 v2.0.1 的 release +rels = call('GET', '/releases') +rel = next((x for x in rels if x['tag_name'] == 'v2.0.1'), None) +if not rel: + print(' ✗ 没找到 v2.0.1 的 release') + sys.exit(1) +rid = rel['id'] +print(' release #%d %s' % (rid, rel['tag_name'])) + +# 用 Python 的 json 写说明(中文、多行都不会出问题) +with open(NOTES, encoding='utf-8') as fh: + note = fh.read() +try: + r = call('PATCH', '/releases/%d' % rid, {'body': note}) + print(' ✓ 说明已更新:%d 字符' % len(r.get('body') or '')) +except Exception as exc: + print(' ⚠ 说明没更新上:%s' % exc) + +# 传 APK +name = 'doubao-2.0.1.apk' +size = os.path.getsize(APK) +print(' 上传 %s(%.1f MB)…' % (name, size / 1024 / 1024)) +with open(APK, 'rb') as fh: + blob = fh.read() +try: + a = call('POST', '/releases/%d/assets?name=%s' % (rid, name), raw=blob, + ctype='application/vnd.android.package-archive') + print(' ✓ 附件 #%s %s %.1f MB' % (a.get('id'), a.get('name'), + (a.get('size') or 0) / 1024 / 1024)) + print(' 下载地址: %s' % a.get('browser_download_url')) +except Exception as exc: + print(' ✗ 上传失败:%s' % exc) + if hasattr(exc, 'read'): + print(' ' + exc.read().decode('utf-8', 'ignore')[:300]) + +# 汇总 +rel = call('GET', '/releases/%d' % rid) +print(' release 现在有 %d 个附件' % len(rel.get('assets') or [])) +for a in rel.get('assets') or []: + print(' %s %.1f MB' % (a['name'], a['size'] / 1024 / 1024))