45 lines
2.0 KiB
Python
45 lines
2.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Create a reproducible source-only review artifact, never a deployable package."""
|
|
import datetime
|
|
import gzip
|
|
import hashlib
|
|
import io
|
|
import json
|
|
import tarfile
|
|
from pathlib import Path
|
|
|
|
root = Path(__file__).resolve().parents[1]
|
|
report = json.loads((root/'docs/local-check-results.json').read_text())
|
|
epoch = int(datetime.datetime.fromisoformat(report['observed_at']).timestamp())
|
|
name = 'ald-transition-console-0.1.0-source-preview'
|
|
files = [root/'.gitignore', root/'CMakeLists.txt', root/'README.md']
|
|
for directory in ('src', 'schemas', 'data', 'resources', 'docs', 'tests', 'packaging', 'tools'):
|
|
files.extend(p for p in (root/directory).rglob('*')
|
|
if p.is_file() and '__pycache__' not in p.parts and p.suffix != '.pyc')
|
|
output = root/'artifacts'
|
|
output.mkdir(exist_ok=True)
|
|
archive = output/(name+'.tar.gz')
|
|
with archive.open('wb') as raw:
|
|
with gzip.GzipFile(filename='', mode='wb', fileobj=raw, mtime=epoch) as compressed:
|
|
with tarfile.open(fileobj=compressed, mode='w') as tar:
|
|
for path in sorted(files):
|
|
if path.is_symlink():
|
|
raise ValueError('Do not distribute symlinked source: '+str(path))
|
|
data = path.read_bytes()
|
|
info = tarfile.TarInfo(name+'/'+path.relative_to(root).as_posix())
|
|
info.size = len(data)
|
|
info.mtime = epoch
|
|
info.mode = 0o755 if path.name == 'rules' and 'debian' in path.parts else 0o644
|
|
info.uid = info.gid = 0
|
|
info.uname = info.gname = ''
|
|
tar.addfile(info, io.BytesIO(data))
|
|
digest = hashlib.sha256(archive.read_bytes()).hexdigest()
|
|
(output/'SHA256SUMS').write_text(digest+' '+archive.name+'\n')
|
|
with tarfile.open(archive) as tar:
|
|
members = tar.getmembers()
|
|
assert len(members) == len(files)
|
|
assert all(m.isfile() and not m.name.startswith('/') and '..' not in Path(m.name).parts for m in members)
|
|
print(f'Source preview: {len(files)} files, {archive.stat().st_size} bytes')
|
|
print(archive)
|
|
print('SHA256 '+digest)
|