This repository has been archived by the owner on Jul 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupdate_index.py
executable file
·68 lines (54 loc) · 2.14 KB
/
update_index.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/env python3
import hashlib
from json import loads, dumps
from os import listdir, walk
from os.path import dirname, isdir, join, realpath
BASE_PATH = dirname(realpath(__file__))
def hash_directory(path):
hasher = hashlib.sha1()
filelist = []
for root, dirs, files in walk(path):
for file in files:
filelist.append(join(root, file))
filelist.sort()
for file in filelist:
with open(join(root, file)) as f:
hasher.update(f.read().encode("utf-8"))
return hasher.hexdigest()
if __name__ == "__main__":
new_index = {}
with open(join(BASE_PATH, "index.json")) as f:
old_index = loads(f.read().encode("utf-8"))
for plugin in listdir(BASE_PATH):
if not isdir(join(BASE_PATH, plugin)) or plugin == ".git":
continue
# read plugin manifest
with open(join(BASE_PATH, plugin, "manifest.json")) as f:
manifest = loads(f.read().encode("utf-8"))
# hash contents of plugin directory
dir_hash = hash_directory(join(BASE_PATH, plugin))
new_index[plugin] = {
"checksum": dir_hash,
"desc": manifest["desc"],
"version": manifest["version"],
}
if plugin in old_index:
if (
new_index[plugin]["checksum"] != old_index[plugin]["checksum"]
and not new_index[plugin]["version"] > old_index[plugin]["version"]
):
raise ValueError(
"contents for {} changed, but version wasn't incremented".format(
plugin
)
)
if new_index[plugin]["version"] > old_index[plugin]["version"]:
print("{plugin}: version {oldversion} -> {newversion}".format(
newversion=new_index[plugin]["version"],
plugin=plugin,
oldversion=old_index[plugin]["version"],
))
else:
print("{plugin}: added".format(plugin=plugin))
with open(join(BASE_PATH, "index.json"), "w") as f:
f.write(dumps(new_index, indent=4, sort_keys=True) + "\n")