-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall_addon.py
68 lines (57 loc) · 2.29 KB
/
install_addon.py
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
#!/usr/bin/python3
import sys
import zipfile
import json
import hashlib
__license__ = 'zlib'
source = sys.argv[1]
library = sys.argv[2]
target = sys.argv[3]
if source == target:
raise SystemExit("Cannot use the same file for source and target")
def copyCampaign(cmpgn, context, names, md5hash, libcontent, target):
with zipfile.ZipFile(target, 'w', compression=zipfile.ZIP_DEFLATED, compresslevel=9) as target:
for n in names:
with target.open(n, 'w') as f:
with cmpgn.open(n, 'r') as s:
f.write(s.read())
with target.open("libraries/libraries.json", 'w') as f:
f.write(json.dumps(context, indent=4).encode('utf-8'))
with target.open(f"libraries/assets/{md5hash}", 'w') as f:
f.write(libcontent)
with open(library, 'rb') as _library:
libcontent = _library.read()
md5hash = hashlib.md5(libcontent).hexdigest()
with zipfile.ZipFile(library) as library:
manifest = json.loads(library.open('library.json').read().decode('utf-8'))
name = manifest['name']
with zipfile.ZipFile(source) as cmpgn:
names = cmpgn.namelist()
if 'libraries/libraries.json' in names:
names.remove('libraries/libraries.json')
context = json.loads(cmpgn.open('libraries/libraries.json').read().decode('utf-8'))
libraries = context['libraries']
else:
libraries = []
context = dict(libraries = libraries)
for idx, lbry in enumerate(libraries):
lib_name = lbry['details']['name']
old_md5hash = lbry['md5Hash']
if lib_name == name:
print("Library already in campaign")
if old_md5hash == md5hash:
print("Library already up to date")
raise SystemExit(0)
else:
print("Updating library")
names.remove(f"libraries/assets/{old_md5hash}")
libraries[idx]['details'] = manifest
libraries[idx]['md5Hash'] = md5hash
copyCampaign(cmpgn, context, names, md5hash, libcontent, target)
raise SystemExit(0)
print("Adding to library")
libraries.append(
dict(details=manifest,
md5Hash=md5hash))
copyCampaign(cmpgn, context, names, md5hash, libcontent, target)
raise SystemExit(0)