1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# Keep this file python 3.7 compatible because it is executed on the server
from typing import Any
from typing import Dict
from typing import Iterator
from typing import Set
from typing import Dict
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_path': '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:
if path.name == 'images.zip':
continue
log.info('Check path: %s', path)
try:
with release_zip.open('plugin-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')
|