Prepare independent CLI builds and immutable Gitea release assets

This commit is contained in:
Anya Tokugawa committed 2026-11-06 02:21:28 +09:00
1 parent d3f4e668c4
commit e4fb4f309f
15 files changed
+396 -4

No files matched your search

+65
View File
@@ -0,0 +1,65 @@
"""Build native CLI artifacts. Invoke on each target OS; never cross-label."""
import hashlib
import importlib.metadata
import json
import os
from pathlib import Path
import platform
import re
import shutil
import subprocess
import sysconfig
import tarfile
ROOT = Path(__file__).resolve().parent.parent
def main():
os.chdir(ROOT)
version = (ROOT / 'VERSION').read_text().strip()
if not re.fullmatch(r'0|[1-9][0-9]*', version.split('.')[0]) or not re.fullmatch(r'(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)', version):
raise SystemExit('VERSION must be a stable SemVer version')
if f"VERSION = '{version}'" not in (ROOT / 'pages').read_text():
raise SystemExit('VERSION and pages disagree')
system = platform.system().lower()
arch = {'x86_64':'amd64','AMD64':'amd64','arm64':'arm64','aarch64':'arm64'}.get(platform.machine())
if system not in ('linux','darwin') or arch not in ('amd64','arm64'):
raise SystemExit('Native Windows credential/locking port is not yet validated; no unsupported binary will be published')
subprocess.run(['python','-m','PyInstaller','--clean','--noconfirm','--onefile','--name','pages','pages'],check=True)
binary = ROOT / 'dist/pages'
output = subprocess.check_output([str(binary),'--version'],text=True).strip()
if output != 'pages-cli '+version: raise SystemExit('Binary version mismatch')
subprocess.run([str(binary),'task','--help'],check=True,stdout=subprocess.DEVNULL)
subprocess.run([str(binary),'contacts','--help'],check=True,stdout=subprocess.DEVNULL)
release=ROOT/'release'
if release.exists():shutil.rmtree(release)
release.mkdir()
target=f'{system}-{arch}'
name=f'pages-cli-v{version}-{target}'
with tarfile.open(release/(name+'.tar.gz'),'w:gz') as tar:
tar.add(binary,arcname='pages')
for path in ('README.md','DISTRIBUTION.md','THIRD_PARTY.md','VERSION'):
tar.add(ROOT/path,arcname=path)
python_license=Path(sysconfig.get_path('stdlib'))/'LICENSE.txt'
if not python_license.is_file():raise SystemExit('Missing Python license')
tar.add(python_license,arcname='licenses/Python-LICENSE.txt')
dist=importlib.metadata.distribution('pyinstaller')
found=False
for path in dist.files or []:
if 'COPYING' in str(path) or 'LICENSE' in str(path):
candidate=Path(dist.locate_file(path))
if candidate.is_file():tar.add(candidate,arcname='licenses/PyInstaller/'+str(path));found=True
if not found:raise SystemExit('Missing PyInstaller license')
for copyright_file in Path('/usr/share/doc').glob('*/copyright'):
tar.add(copyright_file,arcname='licenses/system/'+copyright_file.parent.name+'/copyright')
shutil.copy2(binary,release/(name+('.elf' if system=='linux' else '')))
# The source script remains available to Python users independently of the bundle.
shutil.copy2(ROOT/'pages',release/f'pages-v{version}.py')
metadata={'version':version,'target':target,'python':platform.python_version(),'libc':platform.libc_ver(),'source_revision':os.environ.get('SOURCE_REVISION','')}
(release/(name+'.json')).write_text(json.dumps(metadata,indent=2)+'\n')
hashes=[]
for path in sorted(release.iterdir()):
if path.name.endswith('SHA256SUMS'): continue
hashes.append(hashlib.sha256(path.read_bytes()).hexdigest()+' '+path.name)
(release/(name+'-SHA256SUMS')).write_text('\n'.join(hashes)+'\n')
if __name__=='__main__':main()
+75
View File
@@ -0,0 +1,75 @@
"""Publish verified assets using the repository-scoped Gitea Actions token."""
import hashlib
import json
import os
from pathlib import Path
import re
import subprocess
import urllib.error
import urllib.parse
import urllib.request
import uuid
class SameOriginRedirect(urllib.request.HTTPRedirectHandler):
def redirect_request(self,req,fp,code,msg,headers,newurl):
if urllib.parse.urlsplit(newurl)[:2] != urllib.parse.urlsplit(req.full_url)[:2]:
raise RuntimeError('Refusing cross-origin release download')
return super().redirect_request(req,fp,code,msg,headers,newurl)
def main():
base=os.environ['GITEA_SERVER_URL'].rstrip('/')
if base!='https://git.anya.jp':raise SystemExit('Unexpected Gitea host')
repo=os.environ['GITEA_REPOSITORY']
if repo!='anya/pages-cli':raise SystemExit('Unexpected repository')
token=os.environ['GITEA_TOKEN']
if not token:raise SystemExit('Missing repository release token')
version=Path('VERSION').read_text().strip()
tag='v'+version
if os.environ['GITEA_REF']!='refs/tags/'+tag:raise SystemExit('Tag does not match VERSION')
revision=subprocess.check_output(['git','rev-parse','HEAD'],text=True).strip()
expected=os.environ['GITEA_SHA']
if revision!=expected:raise SystemExit('Checkout revision mismatch')
opener=urllib.request.build_opener(SameOriginRedirect())
def raw(path,method='GET',body=None,ctype='application/json'):
req=urllib.request.Request(base+path,data=body,method=method,headers={'Authorization':'token '+token,'Content-Type':ctype})
with opener.open(req,timeout=90) as r:return r.read()
def api(path,method='GET',body=None):
value=raw('/api/v1/repos/'+repo+path,method,None if body is None else json.dumps(body).encode())
return json.loads(value) if value else {}
files=sorted(p for p in Path('release').iterdir() if p.is_file())
manifests=[p for p in files if p.name.endswith('-SHA256SUMS')]
if len(manifests)!=1:raise SystemExit('Expected one target checksum manifest')
listed=set()
for line in manifests[0].read_text().splitlines():
digest,name=line.split(' ',1)
if not re.fullmatch(r'[a-zA-Z0-9_.-]+',name):raise SystemExit('Invalid asset name')
path=Path('release')/name
if hashlib.sha256(path.read_bytes()).hexdigest()!=digest:raise SystemExit('Asset checksum mismatch')
listed.add(name)
if {p.name for p in files} != listed|{manifests[0].name}:raise SystemExit('Unlisted release assets')
metadata=json.loads(next(p for p in files if p.suffix=='.json').read_text())
if metadata['source_revision']!=revision or metadata['version']!=version:raise SystemExit('Artifact provenance mismatch')
try:release=api('/releases/tags/'+tag)
except urllib.error.HTTPError as e:
if e.code!=404:raise
release=api('/releases','POST',{'tag_name':tag,'target_commitish':revision,'name':'pages-cli '+tag,'draft':True,'prerelease':False,'body':'Standalone PagesWiki CLI. Download the archive and its SHA256SUMS. See DISTRIBUTION.md for platform support and compatibility. macOS/Windows binaries are available only after their native builds have been validated.'})
rid=release['id']
assets={a['name']:a for a in api(f'/releases/{rid}/assets')}
for path in files:
content=path.read_bytes()
if path.name in assets:
download=urllib.parse.urlsplit(assets[path.name]['browser_download_url'])
if download[:2]!=urllib.parse.urlsplit(base)[:2]:raise SystemExit('Unexpected asset download host')
existing=raw(download.path+('?' + download.query if download.query else ''))
if hashlib.sha256(existing).digest()!=hashlib.sha256(content).digest():raise SystemExit('Refusing to replace existing release asset: '+path.name)
print('Unchanged:',path.name);continue
boundary='pages-cli-'+uuid.uuid4().hex
body=(f'--{boundary}\r\nContent-Disposition: form-data; name="attachment"; filename="{path.name}"\r\nContent-Type: application/octet-stream\r\n\r\n'.encode()+content+f'\r\n--{boundary}--\r\n'.encode())
raw('/api/v1/repos/'+repo+f'/releases/{rid}/assets?name='+urllib.parse.quote(path.name),'POST',body,'multipart/form-data; boundary='+boundary)
print('Uploaded:',path.name)
if release['draft']:api(f'/releases/{rid}','PATCH',{'draft':False})
print('Release:',base+'/'+repo+'/releases/tag/'+tag)
if __name__=='__main__':
try:main()
except urllib.error.HTTPError as e:raise SystemExit('Gitea release API returned HTTP '+str(e.code))
+48
View File
@@ -0,0 +1,48 @@
"""Exercise the actual packaged executable against a synthetic loopback API."""
import json
import os
from pathlib import Path
import subprocess
import sys
import tempfile
import threading
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
binary=str(Path(sys.argv[1]).resolve())
token='synthetic_session_'+('x'*40)
seen=[]
class Handler(BaseHTTPRequestHandler):
def log_message(self,*args):pass
def do_POST(self):
self.rfile.read(int(self.headers.get('Content-Length',0)))
if self.path=='/_/auth/login': self.reply({'access_token':token,'token_type':'Bearer','expires_in':3600})
elif self.path=='/_/auth/logout':self.reply({})
else:self.send_error(404)
def do_GET(self):
if self.headers.get('Authorization')!='Bearer '+token:self.send_error(401);return
seen.append(self.path)
self.reply({'items':[{'uuid':'synthetic-task','title':'Packaged CLI task','status':'todo'}]})
def reply(self,value):
self.send_response(200);self.send_header('Content-Type','application/json');self.end_headers();self.wfile.write(json.dumps(value).encode())
server=ThreadingHTTPServer(('127.0.0.1',0),Handler)
thread=threading.Thread(target=server.serve_forever,daemon=True);thread.start()
try:
with tempfile.TemporaryDirectory() as root:
env={**os.environ,'XDG_CONFIG_HOME':root}
def run(*args,stdin='',success=True):
p=subprocess.run([binary,*args],input=stdin,text=True,capture_output=True,env=env,timeout=15)
if token in p.stdout+p.stderr:raise AssertionError('Credential leaked')
if (p.returncode==0)!=success:raise AssertionError('Packaged command failed: '+str(args)+' '+p.stderr)
return p.stdout
run('configure','--origin',f'http://127.0.0.1:{server.server_port}','--username','synthetic')
run('login',stdin='synthetic-password\n')
if 'Packaged CLI task' not in run('task','list'):raise AssertionError('Task missing')
run('shell',stdin='pwd\nexit\n')
profile=Path(root)/'pages-cli/sessions/default.json'
if profile.stat().st_mode&0o077:raise AssertionError('Credential permissions')
profile.chmod(0o644);run('task','list',success=False);profile.chmod(0o600)
run('logout')
if not seen:raise AssertionError('No HTTP request')
print('Packaged CLI: configure/login/task-list/permissions/logout passed')
finally:server.shutdown();server.server_close();thread.join()