Files
FileBow/scripts/build-source-bundles.py

61 lines
2.9 KiB
Python

"""Package a verified source collection into ordinary release ZIPs below 900 MiB."""
import argparse
import hashlib
import json
from pathlib import Path
import shutil
import subprocess
import sys
import zipfile
root = Path(__file__).resolve().parents[1]
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('directory', type=Path)
parser.add_argument('--output', type=Path, required=True)
args = parser.parse_args()
directory = args.directory.resolve()
output = args.output.resolve()
subprocess.run([sys.executable, str(root / 'scripts/verify-sources.py'), str(directory)], check=True)
manifest = json.loads((directory / 'SOURCE-MANIFEST.json').read_text('utf-8'))
version = manifest['filebow_version']
if version != json.loads((root / 'package.json').read_text('utf-8'))['version']:
raise SystemExit('Source collection version does not match package.json.')
output.mkdir(parents=True, exist_ok=True)
shutil.copy2(root / 'licenses/sources/BUILDING.md', directory / 'BUILDING.md')
groups = {}
for row in manifest['sources']:
family = row['file'].split('/')[0]
group = 'ffmpeg' if family.startswith('ffmpeg') else 'rust' if family == 'rust' else 'runtime'
groups.setdefault(group, []).append(row)
bundles = []
def write_bundle(name, files):
path = output / name
with zipfile.ZipFile(path, 'w', compression=zipfile.ZIP_STORED) as archive:
for relative in files:
archive.write(directory / relative, relative)
with zipfile.ZipFile(path) as archive:
if bad := archive.testzip():
raise SystemExit(f'ZIP integrity error: {bad}')
with path.open('rb') as stream:
digest = hashlib.file_digest(stream, 'sha256').hexdigest()
bundles.append({'file': name, 'bytes': path.stat().st_size, 'sha256': digest, 'members': files})
print(f'Created {name} ({path.stat().st_size:,} bytes)', flush=True)
for group, rows in groups.items():
part, size, members = 1, 0, []
for row in sorted(rows, key=lambda r: r['file']):
if row['bytes'] > 900 * 1024**2:
raise SystemExit(f"Source archive exceeds bundle size: {row['file']}")
if members and size + row['bytes'] > 900 * 1024**2:
write_bundle(f'FileBow-{version}-sources-{group}-{part}.zip', members)
part, size, members = part + 1, 0, []
members.append(row['file'])
size += row['bytes']
if members:
write_bundle(f'FileBow-{version}-sources-{group}-{part}.zip', members)
notes = ['SOURCE-MANIFEST.json', 'BUILDING.md', 'HEIF-BUILD-EVIDENCE.txt',
'CHROMIUM-DEPS.txt', 'FFMPEG-BUILD-CONFIGURATION.txt', 'FFMPEG-CACHE-ALIASES.json']
write_bundle(f'FileBow-{version}-sources-notes.zip', notes)
(output / 'SOURCE-BUNDLES.json').write_text(json.dumps({'version': version, 'bundles': bundles}, indent=2) + '\n', encoding='utf-8')
shutil.copy2(directory / 'SOURCE-MANIFEST.json', output / 'SOURCE-MANIFEST.json')
shutil.copy2(directory / 'BUILDING.md', output / 'BUILDING-SOURCES.md')