forked from mcbookshelf/bookshelf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistribute.py
198 lines (176 loc) · 5.16 KB
/
distribute.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
import zipfile
import glob
import os
import argparse
DATAPACK_PATTERNS = [
"**/*.mcfunction",
"**/*.mcmeta",
"**/*.png",
"**/*.json",
"**/*.txt", # comments
"**/*.nbt", # structure files
]
WORLD_PATTERNS = [
"**/*.mca", # region files
]
RENAME_MODULES = {
"bs": "controller",
}
def get_files(
base_folder: str,
path_list: list[str],
patterns: list[str],
) -> list[str]:
files = []
folders = []
for path in path_list:
if os.path.isfile(os.path.join(base_folder, path)):
files.append(path)
elif os.path.isdir(os.path.join(base_folder, path)):
folders.append(path)
for pattern in patterns:
for folder in folders:
files.extend(
[
os.path.join(folder, file)
for file in glob.glob(
pattern,
root_dir=os.path.join(base_folder, folder),
recursive=True,
)
]
)
return files
def get_world_files() -> list[str]:
return get_files(
".",
[
"datapacks",
"region",
"icon.png",
"level.dat",
"LICENSE.md",
"README.md",
],
DATAPACK_PATTERNS + WORLD_PATTERNS,
)
def get_datapack_files() -> list[str]:
return get_files(
"datapacks/Bookshelf",
[
"data",
"icon.png",
"pack.mcmeta",
],
DATAPACK_PATTERNS,
)
def get_core_datapack_files() -> list[str]:
return get_files(
"datapacks/Bookshelf",
[
"data/bs.core",
"data/minecraft",
"icon.png",
"pack.mcmeta",
],
DATAPACK_PATTERNS,
)
def get_module_datapack_files(module_name: str) -> list[str]:
return get_files(
"datapacks/Bookshelf",
[
f"data/{module_name}",
"icon.png",
"pack.mcmeta",
],
DATAPACK_PATTERNS,
)
def create_archive(filename: str, base_folder: str, files: list[str]):
archive = zipfile.ZipFile(
filename,
mode="w",
compression=zipfile.ZIP_DEFLATED,
)
for file in files:
archive.write(
filename=os.path.join(base_folder, file),
arcname=file,
)
def list_modules() -> list[str]:
modules = os.listdir("datapacks/Bookshelf/data")
modules.remove("bs.core")
modules.remove("minecraft")
return modules
def create_world_archive(filename: str = 'build/World.zip'):
print("🏞 Creating world archive")
create_archive(
filename,
".",
get_world_files(),
)
def create_datapack_archive(filename: str = 'build/Bookshelf.zip'):
print("🗄 Creating datapack archive")
create_archive(
filename,
"datapacks/Bookshelf",
get_datapack_files(),
)
def create_core_module_archive(filename: str = 'build/Bookshelf-core.zip'):
print("🔩 Creating core module archive")
create_archive(
filename,
"datapacks/Bookshelf",
get_core_datapack_files(),
)
def create_modules_archive(filename: str = "build/Bookshelf-{}.zip"):
for module in list_modules():
print(f"🧩 Creating archive for module {module}")
module_name = RENAME_MODULES[module] if module in RENAME_MODULES else module
create_archive(
filename.format(module_name[3:] if module_name.startswith('bs.') else module_name),
"datapacks/Bookshelf",
get_module_datapack_files(module),
)
if __name__ == "__main__":
if not os.path.exists("./build"):
os.mkdir("build")
parser = argparse.ArgumentParser(
description="Pack Bookshelf in nice zip files",
)
parser.add_argument(
"-w", "--world", type=int, default=1,
help="Export world. Set to 0 to disable.", metavar="EXPORT",
)
parser.add_argument(
"-d", "--datapack", type=int, default=1,
help="Export entire datapack. Set to 0 to disable.", metavar="EXPORT",
)
parser.add_argument(
"-c", "--core", type=int, default=1,
help="Export core module. Set to 0 to disable.", metavar="EXPORT",
)
parser.add_argument(
"-m", "--modules", type=int, default=1,
help="Export every module. Set to 0 to disable.", metavar="EXPORT",
)
parser.add_argument(
"-v", "--version", type=str, required=False,
help="Specify the lib version in zip filename",
)
args = parser.parse_args()
if args.world:
create_world_archive(
f'build/World{"-" + args.version if args.version else ""}.zip',
)
if args.datapack:
create_datapack_archive(
f'build/Bookshelf{"-" + args.version if args.version else ""}.zip',
)
if args.core:
create_core_module_archive(
f'build/Bookshelf-core{"-" + args.version if args.version else ""}.zip',
)
if args.modules:
create_modules_archive(
'build/Bookshelf-{}'+f'{"-" + args.version if args.version else ""}.zip',
)