diff options
author | lovetox <philipp@hoerist.com> | 2022-04-29 20:13:30 +0300 |
---|---|---|
committer | lovetox <philipp@hoerist.com> | 2022-05-06 19:21:54 +0300 |
commit | d2ced8b812feed43423f93addc1267a84cf6c806 (patch) | |
tree | babc196255d0631b9405768f79d49762b098ed03 | |
parent | 390218a85ee01b49dc700867d7b35538c2760575 (diff) |
Scripts: Add build_repository.py
-rw-r--r-- | scripts/build_repository.py | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/scripts/build_repository.py b/scripts/build_repository.py new file mode 100644 index 0000000..3c14f94 --- /dev/null +++ b/scripts/build_repository.py @@ -0,0 +1,92 @@ + +from typing import Any +from typing import Dict +from typing import Iterator + +import sys +import json +import logging +from collections import defaultdict +from pathlib import Path +from zipfile import ZipFile + + +FORMAT = '%(asctime)s %(message)s' +logging.basicConfig(format=FORMAT, level=logging.DEBUG) +log = logging.getLogger() + + +REQUIRED_KEYS: set[str] = { + 'authors', + 'description', + 'homepage', + 'config_dialog', + 'name', + 'platforms', + 'requirements', + 'short_name', + 'version' +} + +PACKAGE_INDEX: dict[str, Any] = { + 'metadata': { + 'repository_name': 'master', + 'image_url': 'images.zip', + }, + 'plugins': defaultdict(dict) +} + + +def is_manifest_valid(manifest: dict[str, Any]) -> bool: + manifest_keys = set(manifest.keys()) + return REQUIRED_KEYS.issubset(manifest_keys) + + +def iter_releases(release_folder: Path) -> Iterator[Dict[str, Any]]: + for path in release_folder.rglob('*.zip'): + with ZipFile(path) as release_zip: + try: + with release_zip.open('manifest.json') as file: + manifest = json.load(file) + yield manifest + except Exception: + log.error('Error loading manifest') + log.exception('') + + +def build_package_index(release_folder: Path) -> None: + log.info('Build package index') + for manifest in iter_releases(release_folder): + if not is_manifest_valid(manifest): + log.warning('Invalid manifest') + log.warning(manifest) + continue + + short_name = manifest.pop('short_name') + version = manifest.pop('version') + PACKAGE_INDEX['plugins'][short_name][version] = manifest + log.info('Found manifest: %s - %s', short_name, version) + + path = release_folder / 'package_index.json' + with path.open('w') as f: + json.dump(PACKAGE_INDEX, f) + + +def build_image_zip(release_folder: Path) -> None: + log.info('Build images.zip') + with ZipFile(release_folder / 'images.zip', mode='w') as image_zip: + for path in release_folder.iterdir(): + if not path.is_dir(): + continue + + image = path / f'{path.name}.png' + if not image.exists(): + continue + image_zip.write(image, arcname=image.name) + + +if __name__ == '__main__': + path = Path(sys.argv[1]) + build_package_index(path) + build_image_zip(path) + log.info('Finished') |