Files

46 lines
2.9 KiB
Python

#!/usr/bin/env python3
"""Write and sign <repo>/<channel>/install-p2p.txt: the torrent + BitTorrent engine OctoUpdater uses for a first install.
p2p_list.py --repo R --channel octo --release 20261124 --torrent T.torrent --torrent-url p2p/OctoWoW-20261124.torrent \
--aria2 aria2c.exe --aria2-url p2p/aria2c-1.37.0.exe
URLs are relative to the channel (or absolute). The torrent must list exactly the release's install.txt files in the
same order (make_torrent.py does); this is checked. Upload the torrent, aria2c.exe and its GPL source next to it.
OCTO-P2P 1 / RELEASE <id> / TORRENT <sha256> <size> <url> / ARIA2 <sha256> <size> <url> / END
"""
import argparse, hashlib, os, subprocess, sys
from pathlib import Path
from install_list import KEY, PUB
def bdec(b, i=0):
c = b[i:i + 1]
if c == b'i': j = b.index(b'e', i); return int(b[i + 1:j]), j + 1
if c in (b'l', b'd'):
i += 1; out = [] if c == b'l' else {}
while b[i:i + 1] != b'e':
if c == b'l': v, i = bdec(b, i); out.append(v)
else: k, i = bdec(b, i); v, i = bdec(b, i); out[k] = v
return out, i + 1
j = b.index(b':', i); n = int(b[i:j]); return b[j + 1:j + 1 + n], j + 1 + n
def main():
a = argparse.ArgumentParser()
for k in ('repo', 'release', 'torrent', 'torrent-url', 'aria2', 'aria2-url'): a.add_argument('--' + k, required=True)
a.add_argument('--channel', default='octo'); x = a.parse_args()
chan = Path(x.repo) / x.channel
inst = [l.split(' ', 3) for l in (chan / 'install.txt').read_text().splitlines() if l.startswith('F ')]
rel = [l for l in (chan / 'install.txt').read_text().splitlines() if l.startswith('RELEASE ')]
if rel != [f'RELEASE {x.release}']: sys.exit(f'install.txt is {rel}, not release {x.release}')
info = bdec(Path(x.torrent).read_bytes())[0][b'info']
tf = [('/'.join(p.decode() for p in f[b'path']), f[b'length']) for f in info[b'files']]
if tf != [(f[3], int(f[2])) for f in inst]: sys.exit('torrent files/order differ from install.txt')
def row(kind, path, url):
d = Path(path).read_bytes(); return f'{kind} {hashlib.sha256(d).hexdigest()} {len(d)} {url}'
out = ['OCTO-P2P 1', f'RELEASE {x.release}', row('TORRENT', x.torrent, x.torrent_url), row('ARIA2', x.aria2, x.aria2_url), 'END']
tmp = chan / 'install-p2p.txt.new'; tmp.write_text('\n'.join(out) + '\n')
subprocess.run(['openssl', 'dgst', '-sha256', '-sign', str(KEY), '-out', str(chan / 'install-p2p.txt.sig.new'), str(tmp)], check=True)
subprocess.run(['openssl', 'dgst', '-sha256', '-verify', str(PUB), '-signature', str(chan / 'install-p2p.txt.sig.new'), str(tmp)], check=True, capture_output=True)
os.replace(tmp, chan / 'install-p2p.txt'); os.replace(chan / 'install-p2p.txt.sig.new', chan / 'install-p2p.txt.sig')
print('signed', chan / 'install-p2p.txt', f'({len(tf)} files match install.txt)')
if __name__ == '__main__': main()