44 lines
2.0 KiB
Python
44 lines
2.0 KiB
Python
"""Build reproducible archives and catalog entries for every source resource."""
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
import zipfile
|
|
|
|
|
|
def build(root):
|
|
root = Path(root)
|
|
archives = root / 'archives'
|
|
archives.mkdir(exist_ok=True)
|
|
entries = []
|
|
for kind in ('plugin', 'theme'):
|
|
for directory in sorted((root / (kind + 's')).iterdir()):
|
|
manifest = directory / (kind + '.json')
|
|
if not manifest.is_file():
|
|
continue
|
|
info = json.loads(manifest.read_text())
|
|
archive = archives / f"{kind}-{info['id']}-{info['version']}.zip"
|
|
files = {}
|
|
for path in sorted(directory.rglob('*')):
|
|
relative = path.relative_to(directory)
|
|
if path.is_file() and not {'__pycache__', 'tests', '.pytest_cache', 'node_modules', '.git', '.venv'}.intersection(relative.parts) and path.suffix != '.pyc':
|
|
files[relative.as_posix()] = path.read_bytes()
|
|
same = False
|
|
if archive.exists():
|
|
with zipfile.ZipFile(archive) as existing:
|
|
same = {name: existing.read(name) for name in existing.namelist()} == files
|
|
# Retain exact published bytes and checksums for unchanged versions.
|
|
if not same:
|
|
with zipfile.ZipFile(archive, 'w', zipfile.ZIP_DEFLATED) as output:
|
|
for name, content in files.items():
|
|
item = zipfile.ZipInfo(name)
|
|
item.compress_type = zipfile.ZIP_DEFLATED
|
|
item.external_attr = 0o100644 << 16
|
|
output.writestr(item, content)
|
|
entries.append({**info, 'type': kind, 'archive': archive.relative_to(root).as_posix(), 'sha256': hashlib.sha256(archive.read_bytes()).hexdigest()})
|
|
(root / 'catalog.json').write_text(json.dumps({'schema_version': 1, 'resources': entries}, indent=2) + '\n')
|
|
return entries
|
|
|
|
|
|
if __name__ == '__main__':
|
|
build(Path(__file__).resolve().parents[1])
|