74 lines
3.6 KiB
Python
74 lines
3.6 KiB
Python
"""Build reproducible archives and catalog entries for every source resource."""
|
|
import hashlib
|
|
import json
|
|
import re
|
|
from pathlib import Path
|
|
import zipfile
|
|
|
|
|
|
def build(root):
|
|
root = Path(root)
|
|
sources = []
|
|
for kind in ('plugin', 'theme'):
|
|
for directory in sorted((root / (kind + 's')).iterdir()):
|
|
manifest = directory / (kind + '.json')
|
|
if manifest.is_file():
|
|
info = json.loads(manifest.read_text())
|
|
dependencies = info.get('dependencies', [])
|
|
if (info.get('id') != directory.name or not re.fullmatch(r'[a-z][a-z0-9-]*', directory.name)
|
|
or any(not isinstance(info.get(key), str) or not info[key].strip() for key in ('name', 'description', 'version'))):
|
|
raise ValueError(f'Invalid resource metadata: {manifest}')
|
|
if (not isinstance(dependencies, list) or len(dependencies) > 100
|
|
or any(not isinstance(key, str) or not re.fullmatch(r'[a-z][a-z0-9-]*', key) for key in dependencies)
|
|
or len(set(dependencies)) != len(dependencies) or info['id'] in dependencies):
|
|
raise ValueError(f'Invalid dependencies: {manifest}')
|
|
sources.append((kind, directory, info))
|
|
plugins = {info['id']: info for kind, _, info in sources if kind == 'plugin'}
|
|
visiting, done = set(), set()
|
|
def visit(key):
|
|
if key not in plugins:
|
|
raise ValueError(f'Unknown plugin dependency: {key}')
|
|
if key in visiting:
|
|
raise ValueError(f'Dependency cycle: {key}')
|
|
if key in done:
|
|
return
|
|
visiting.add(key)
|
|
for dependency in plugins[key].get('dependencies', []):
|
|
visit(dependency)
|
|
visiting.remove(key)
|
|
done.add(key)
|
|
for _, _, info in sources:
|
|
for dependency in info.get('dependencies', []):
|
|
visit(dependency)
|
|
archives = root / 'archives'
|
|
archives.mkdir(exist_ok=True)
|
|
entries = []
|
|
for kind, directory, info in sources:
|
|
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
|
|
if archive.exists() and not same:
|
|
raise ValueError(f'Published archive differs from source; bump the resource version: {archive.name}')
|
|
# 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])
|