-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathavsrepo.py
502 lines (454 loc) · 20 KB
/
avsrepo.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
## MIT License
##
## Copyright (c) 2018-2020 Fredrik Mellbin
##
## Permission is hereby granted, free of charge, to any person obtaining a copy
## of this software and associated documentation files (the "Software"), to deal
## in the Software without restriction, including without limitation the rights
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
## copies of the Software, and to permit persons to whom the Software is
## furnished to do so, subject to the following conditions:
##
## The above copyright notice and this permission notice shall be included in all
## copies or substantial portions of the Software.
##
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
## SOFTWARE.
import sys
import json
import hashlib
import urllib.request
import io
#import site
import os
import os.path
import subprocess
import tempfile
import argparse
import email.utils
import time
import zipfile
try:
import winreg
except ImportError:
print('{} is only supported on Windows.'.format(sys.executable))
sys.exit(1)
try:
import tqdm
except ImportError:
pass
if getattr(sys, 'frozen', False):
app_file = sys.executable
else:
app_file = __file__
is_64bits = sys.maxsize > 2**32
parser = argparse.ArgumentParser(description='A simple Avisynth package manager')
parser.add_argument('operation', choices=['install', 'update', 'upgrade', 'upgrade-all', 'uninstall', 'installed', 'available', 'paths'])
parser.add_argument('package', nargs='*', help='identifier, namespace or module to install, upgrade or uninstall')
parser.add_argument('-f', action='store_true', dest='force', help='force upgrade for packages where the current version is unknown')
parser.add_argument('-p', action='store_true', dest='portable', help='use paths suitable for portable installs')
parser.add_argument('-d', action='store_true', dest='skip_deps', help='skip installing dependencies')
parser.add_argument('-t', choices=['win32', 'win64'], default='win64' if is_64bits else 'win32', dest='target', help='binaries to install, defaults to python\'s architecture')
parser.add_argument('-b', dest='binary_path', help='custom binary install path')
parser.add_argument('-s', dest='script_path', help='custom script install path')
args = parser.parse_args()
args.portable = True # for now we are always in portable mode, no need for an extra -p
is_64bits = (args.target == 'win64')
if (args.operation in ['install', 'upgrade', 'uninstall']) == ((args.package is None) or len(args.package) == 0):
print('Package argument only required for install, upgrade and uninstall operations')
sys.exit(1)
file_dirname = os.path.dirname(os.path.abspath(app_file))
package_json_path = os.path.join(file_dirname, 'avspackages.json')
avs_script_path = file_dirname #if args.portable else site.getusersitepackages()
if args.script_path is not None:
avs_script_path = args.script_path
if args.portable:
base_path = file_dirname
plugin32_path = os.path.join(base_path, 'avisynth32', 'plugins')
plugin64_path = os.path.join(base_path, 'avisynth64', 'plugins')
avs_plugin_path = plugin64_path if is_64bits else plugin32_path
if args.binary_path is not None:
avs_plugin_path = args.binary_path
os.makedirs(avs_script_path, exist_ok=True)
os.makedirs(avs_plugin_path, exist_ok=True)
os.makedirs(os.path.dirname(package_json_path), exist_ok=True)
cmd7zip_path = os.path.join(file_dirname, '7z.exe')
if not os.path.isfile(cmd7zip_path):
try:
with winreg.OpenKeyEx(winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE\\7-Zip', reserved=0, access=winreg.KEY_READ) as regkey:
cmd7zip_path = os.path.join(winreg.QueryValueEx(regkey, 'Path')[0], '7z.exe')
except:
cmd7zip_path = '7z.exe'
installed_packages = {}
download_cache = {}
def fetch_ur1(url, desc = None):
with urllib.request.urlopen(url) as urlreq:
if ('tqdm' in sys.modules) and (urlreq.headers['content-length'] is not None):
size = int(urlreq.headers['content-length'])
remaining = size
data = bytearray()
with tqdm.tqdm(total=size, unit='B', unit_scale=True, unit_divisor=1024, desc=desc) as t:
while remaining > 0:
blocksize = min(remaining, 1024*128)
data.extend(urlreq.read(blocksize))
remaining = remaining - blocksize
t.update(blocksize)
return data
else:
print('Fetching: ' + url)
return urlreq.read()
def fetch_url_cached(url, desc):
data = download_cache.get(url, None)
if data is None:
data = fetch_ur1(url, desc)
download_cache[url] = data
return data
package_print_string = "{:25s} {:15s} {:11s} {:11s} {:s}"
package_list = None
try:
with open(package_json_path, 'r', encoding='utf-8') as pl:
package_list = json.load(pl)
if package_list['file-format'] != 2:
print('Package definition format is {} but only version 2 is supported'.format(package_list['file-format']))
package_list = None
package_list = package_list['packages']
except:
pass
def check_hash(data, ref_hash):
data_hash = hashlib.sha256(data).hexdigest()
return (data_hash == ref_hash, data_hash, ref_hash)
def get_bin_name(p):
if p['type'] == 'avsiScript':
return 'script'
elif p['type'] == 'avsPlugin':
if is_64bits:
return 'win64'
else:
return 'win32'
else:
raise Exception('Unknown install type')
def get_install_path(p):
if p['type'] == 'avsiScript':
return avs_script_path
elif p['type'] == 'avsPlugin':
return avs_plugin_path
else:
raise Exception('Unknown install type')
def get_package_from_id(id, required = False):
for p in package_list:
if p['identifier'] == id:
return p
if required:
raise Exception('No package with the identifier ' + id + ' found')
return None
def get_package_from_plugin_name(name, required = False):
for p in package_list:
if p['name'].casefold() == name.casefold():
return p
if required:
raise Exception('No package with the name ' + name + ' found')
return None
def get_package_from_namespace(namespace, required = False):
for p in package_list:
if 'namespace' in p:
if p['namespace'] == namespace:
return p
if required:
raise Exception('No package with the namespace ' + namespace + ' found')
return None
def get_package_from_modulename(modulename, required = False):
for p in package_list:
if 'modulename' in p:
if p['modulename'] == modulename:
return p
if required:
raise Exception('No package with the modulename ' + modulename + ' found')
return None
def get_package_from_name(name):
p = get_package_from_id(name)
if p is None:
p = get_package_from_namespace(name)
if p is None:
p = get_package_from_modulename(name)
if p is None:
p = get_package_from_plugin_name(name)
if p is None:
raise Exception('Package ' + name + ' not found')
return p
def is_package_installed(id):
return id in installed_packages
def is_package_upgradable(id, force):
lastest_installable = get_latest_installable_release(get_package_from_id(id, True))
if force:
return (is_package_installed(id) and (lastest_installable is not None) and (installed_packages[id] != lastest_installable['version']))
else:
return (is_package_installed(id) and (lastest_installable is not None) and (installed_packages[id] != 'Unknown') and (installed_packages[id] != lastest_installable['version']))
def detect_installed_packages():
if package_list is not None:
for p in package_list:
dest_path = get_install_path(p)
for v in p['releases']:
matched = True
exists = True
bin_name = get_bin_name(p)
if bin_name in v:
for f in v[bin_name]['files']:
try:
with open(os.path.join(dest_path, f), 'rb') as fh:
if not check_hash(fh.read(), v[bin_name]['files'][f][1])[0]:
matched = False
except FileNotFoundError:
exists = False
matched = False
if matched:
installed_packages[p['identifier']] = v['version']
break
elif exists:
installed_packages[p['identifier']] = 'Unknown'
else:
print('No valid package definitions found. Run update command first!')
sys.exit(1)
def print_package_status(p):
lastest_installable = get_latest_installable_release(p)
name = p['name']
if is_package_upgradable(p['identifier'], False):
name = '*' + name
elif is_package_upgradable(p['identifier'], True):
name = '+' + name
print(package_print_string.format(name, p['namespace'] if p['type'] == 'avsPlugin' else p['modulename'], installed_packages[p['identifier']] if p['identifier'] in installed_packages else '', lastest_installable['version'] if lastest_installable is not None else '', p['identifier']))
def list_installed_packages():
print(package_print_string.format('Name', 'Namespace', 'Installed', 'Latest', 'Identifier'))
for id in installed_packages:
print_package_status(get_package_from_id(id, True))
def list_available_packages():
print(package_print_string.format('Name', 'Namespace', 'Installed', 'Latest', 'Identifier'))
for p in package_list:
print_package_status(p)
def get_latest_installable_release(p):
bin_name = get_bin_name(p)
for rel in p['releases']:
if bin_name in rel:
return rel
return None
def can_install(p):
return get_latest_installable_release(p) is not None
def install_files(p):
dest_path = get_install_path(p)
bin_name = get_bin_name(p)
install_rel = get_latest_installable_release(p)
url = install_rel[bin_name]['url']
data = None
try:
data = fetch_url_cached(url, p['name'] + ' ' + install_rel['version'])
except:
print('Failed to download ' + p['name'] + ' ' + install_rel['version'] + ', skipping installation and moving on')
return (0, 1)
single_file = None
if len(install_rel[bin_name]['files']) == 1:
for key in install_rel[bin_name]['files']:
single_file = (key, install_rel[bin_name]['files'][key][0], install_rel[bin_name]['files'][key][1])
if (single_file is not None) and (single_file[1] == url.rsplit('/', 2)[-1]):
install_fn = single_file[0]
hash_result = check_hash(data, single_file[2])
if not hash_result[0]:
raise Exception('Hash mismatch for ' + install_fn + ' got ' + hash_result[1] + ' but expected ' + hash_result[2])
uninstall_files(p)
os.makedirs(os.path.join(dest_path, os.path.split(install_fn)[0]), exist_ok=True)
with open(os.path.join(dest_path, install_fn), 'wb') as outfile:
outfile.write(data)
else:
tffd, tfpath = tempfile.mkstemp(prefix='vsm')
tf = open(tffd, mode='wb')
tf.write(data)
tf.close()
result_cache = {}
for install_fn in install_rel[bin_name]['files']:
fn_props = install_rel[bin_name]['files'][install_fn]
result = subprocess.run([cmd7zip_path, "e", "-so", tfpath, fn_props[0]], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
result.check_returncode()
hash_result = check_hash(result.stdout, fn_props[1])
if not hash_result[0]:
raise Exception('Hash mismatch for ' + install_fn + ' got ' + hash_result[1] + ' but expected ' + hash_result[2])
result_cache[install_fn] = result.stdout
uninstall_files(p)
for install_fn in install_rel[bin_name]['files']:
os.makedirs(os.path.join(dest_path, os.path.split(install_fn)[0]), exist_ok=True)
with open(os.path.join(dest_path, install_fn), 'wb') as outfile:
outfile.write(result_cache[install_fn])
os.remove(tfpath)
installed_packages[p['identifier']] = install_rel['version']
print('Successfully installed ' + p['name'] + ' ' + install_rel['version'])
return (1, 0)
def install_package(name):
p = get_package_from_name(name)
if can_install(p):
inst = (0, 0, 0)
if not args.skip_deps:
if 'dependencies' in p:
for dep in p['dependencies']:
res = install_package(dep)
inst = (inst[0], inst[1] + res[0] + res[1], inst[2] + res[2])
if not is_package_installed(p['identifier']):
res = install_files(p)
inst = (inst[0] + res[0], inst[1], inst[2] + res[1])
return inst
else:
print('No binaries available for ' + args.target + ' in package ' + p['name'] + ', skipping installation')
return (0, 0, 1)
def upgrade_files(p):
if can_install(p):
inst = (0, 0, 0)
if 'dependencies' in p:
for dep in p['dependencies']:
if not is_package_installed(dep):
res = install_package(dep)
inst = (inst[0], inst[1] + res[0] + res[1], inst[2] + res[2])
res = install_files(p)
return (inst[0] + res[0], inst[1], inst[2] + res[1])
else:
print('No binaries available for ' + args.target + ' in package ' + p['name'] + ', skipping installation')
return (0, 0, 1)
def upgrade_package(name, force):
inst = (0, 0, 0)
p = get_package_from_name(name)
if not is_package_installed(p['identifier']):
print('Package ' + p['name'] + ' not installed, can\'t upgrade')
elif is_package_upgradable(p['identifier'], force):
res = upgrade_files(p)
return (res[0], 0, res[1])
elif not is_package_upgradable(p['identifier'], True):
print('Package ' + p['name'] + ' not upgraded, latest version installed')
else:
print('Package ' + p['name'] + ' not upgraded, unknown version must use -f to force replacement')
return inst
def upgrade_all_packages(force):
inst = (0, 0, 0)
installed_ids = list(installed_packages.keys())
for id in installed_ids:
if is_package_upgradable(id, force):
res = upgrade_files(get_package_from_id(id, True))
inst = (inst[0] + res[0], inst[1] + res[1], inst[2] + res[2])
return inst
def uninstall_files(p):
dest_path = get_install_path(p)
bin_name = get_bin_name(p)
installed_rel = None
if p['identifier'] in installed_packages:
for rel in p['releases']:
if rel['version'] == installed_packages[p['identifier']]:
installed_rel = rel
break
if installed_rel is not None:
for f in installed_rel[bin_name]['files']:
os.remove(os.path.join(dest_path, f))
def uninstall_package(name):
p = get_package_from_name(name)
if is_package_installed(p['identifier']):
if installed_packages[p['identifier']] == 'Unknown':
print('Can\'t uninstall unknown version package: ' + p['name'])
return (0, 0)
else:
uninstall_files(p)
print('Uninstalled package: ' + p['name'] + ' ' + installed_packages[p['identifier']])
return (1, 0)
else:
print('No files installed for ' + p['name'] + ', skipping uninstall')
return (0, 0)
def update_package_definition(url):
localmtimeval = 0
try:
localmtimeval = os.path.getmtime(package_json_path)
except:
pass
localmtime = email.utils.formatdate(localmtimeval + 10, usegmt=True)
req_obj = urllib.request.Request(url, headers={ 'If-Modified-Since': localmtime })
try:
with urllib.request.urlopen(req_obj) as urlreq:
remote_modtime = email.utils.mktime_tz(email.utils.parsedate_tz(urlreq.info()['Last-Modified']))
data = urlreq.read()
with zipfile.ZipFile(io.BytesIO(data), 'r') as zf:
with zf.open('avspackages.json') as pkgfile:
with open(package_json_path, 'wb') as dstfile:
dstfile.write(pkgfile.read())
os.utime(package_json_path, times=(remote_modtime, remote_modtime))
except urllib.error.HTTPError as httperr:
if httperr.code == 304:
print('Local definitions already up to date: ' + email.utils.formatdate(localmtimeval, usegmt=True))
else:
raise
else:
print('Local definitions updated to: ' + email.utils.formatdate(remote_modtime, usegmt=True))
def print_paths():
print('Paths:')
print('Definitions: ' + package_json_path)
print('Binaries: ' + avs_plugin_path)
print('Scripts: ' + avs_script_path)
if args.operation != 'update' and package_list is None:
print('Failed to open avspackages.json. Run update command.')
sys.exit(1)
for name in args.package:
try:
get_package_from_name(name)
except Exception as e:
print(e)
sys.exit(1)
if args.operation == 'install':
detect_installed_packages()
inst = (0, 0, 0)
for name in args.package:
res = install_package(name)
inst = (inst[0] + res[0], inst[1] + res[1], inst[2] + res[2])
if (inst[0] == 0) and (inst[1] == 0):
print('Nothing done')
elif (inst[0] > 0) and (inst[1] == 0):
print('{} {} installed'.format(inst[0], 'package' if inst[0] == 1 else 'packages'))
elif (inst[0] == 0) and (inst[1] > 0):
print('{} missing {} installed'.format(inst[1], 'dependency' if inst[1] == 1 else 'dependencies'))
else:
print('{} {} and {} additional {} installed'.format(inst[0], 'package' if inst[0] == 1 else 'packages', inst[1], 'dependency' if inst[1] == 1 else 'dependencies'))
if (inst[2] > 0):
print('{} {} failed'.format(inst[2], 'package' if inst[0] == 1 else 'packages'))
elif args.operation in ('upgrade', 'upgrade-all'):
detect_installed_packages()
inst = (0, 0, 0)
if args.operation == 'upgrade-all':
inst = upgrade_all_packages(args.force)
else:
for name in args.package:
res = upgrade_package(name, args.force)
inst = (inst[0] + res[0], inst[1] + res[1], inst[2] + res[2])
if (inst[0] == 0) and (inst[1] == 0):
print('Nothing done')
elif (inst[0] > 0) and (inst[1] == 0):
print('{} {} upgraded'.format(inst[0], 'package' if inst[0] == 1 else 'packages'))
elif (inst[0] == 0) and (inst[1] > 0):
print('{} missing {} installed'.format(inst[1], 'dependency' if inst[1] == 1 else 'dependencies'))
else:
print('{} {} upgraded and {} additional {} installed'.format(inst[0], 'package' if inst[0] == 1 else 'packages', inst[1], 'dependency' if inst[1] == 1 else 'dependencies'))
if (inst[2] > 0):
print('{} {} failed'.format(inst[2], 'package' if inst[0] == 1 else 'packages'))
elif args.operation == 'uninstall':
detect_installed_packages()
uninst = (0, 0)
for name in args.package:
res = uninstall_package(name)
uninst = (uninst[0] + res[0], uninst[1] + res[1])
if uninst[0] == 0:
print('No packages uninstalled')
else:
print('{} {} uninstalled'.format(uninst[0], 'package' if uninst[0] == 1 else 'packages'))
elif args.operation == 'installed':
detect_installed_packages()
list_installed_packages()
elif args.operation == 'available':
detect_installed_packages()
list_available_packages()
elif args.operation == 'update':
update_package_definition('https://vsdb.top/avspackages.zip')
elif args.operation == 'paths':
print_paths()