-
Notifications
You must be signed in to change notification settings - Fork 0
/
collectstatic.py
70 lines (52 loc) · 1.86 KB
/
collectstatic.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
import hashlib
import os
import shutil
DATA_DIR = "data"
COLOR_RESET = "\033[0m"
COLOR_OK = "\033[92m"
COLOR_STATUS = "\033[94m"
COLOR_DIM = "\033[90m"
ALLOWED_EXTENSIONS = set([
".png", ".jpg", ".bmp", ".tga",
".pcx", ".ppm", ".psd", ".wal", ".rgb",
".ogg", ".x", ".b3d", ".md2", ".obj"
])
def sha1(filename):
f = open(filename, "rb")
h = hashlib.sha1(f.read()).hexdigest()
f.close()
return h
def copy_static(path):
new_path = sha1(path)
new_dirs = os.path.join(DATA_DIR, new_path[0], new_path[1], new_path[2], new_path[3])
os.makedirs(new_dirs, exist_ok=True)
shutil.copyfile(path, os.path.join(new_dirs, new_path))
def collect_static(path):
print(COLOR_OK + "Collecting static from {}".format(path) + COLOR_RESET)
count = 0
for name in os.listdir(path):
file_path = os.path.join(path, name)
if os.path.isfile(file_path) and os.path.splitext(file_path)[1] in ALLOWED_EXTENSIONS:
copy_static(file_path)
count += 1
if count:
print(COLOR_STATUS + "copied {} file{}".format(count, "" if count == 1 else "s") + COLOR_RESET)
def collect_mod(path):
print(COLOR_DIM + "Entering {}".format(path) + COLOR_RESET)
if os.path.exists(os.path.join(path, "modpack.txt")):
collect_mods(path)
return
for media_dir in ["textures", "sounds", "media", "models"]:
media_path = os.path.join(path, media_dir)
if os.path.exists(media_path) and os.path.isdir(media_path):
collect_static(media_path)
def collect_mods(path):
print(COLOR_DIM + "Collecting mods in {}".format(path) + COLOR_RESET)
for mod in os.listdir(path):
mod_path = os.path.join(path, mod)
if os.path.isdir(mod_path) and not mod.startswith("."):
collect_mod(mod_path)
def main():
collect_mods("mods")
if __name__ == "__main__":
main()