-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistribution.py
More file actions
217 lines (185 loc) · 8.72 KB
/
Distribution.py
File metadata and controls
217 lines (185 loc) · 8.72 KB
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
import os
import subprocess as sp
import Modules.constants as cs
import Modules.distribution as ds
import Modules.entry as et
import Modules.file as fl
import Modules.functions as ft
cwd = os.getcwd()
archive = os.path.join(cwd, "Archive")
contacts = fl.Folder(os.path.join(cwd, "Contacts"))
settings = fl.Folder(os.path.join(cwd, "Settings"))
# Collect distribution information
name = str(input("Enter the name of the distribution: "))
version = str(input(f"Enter the version number of {name}: "))
author = str(input(f"Enter the author(s) of {name}: "))
date = str(input(f"Enter the release date of {version} of {name}: "))
predecessors = []
for file in os.listdir(archive):
if file.startswith(name):
file = os.path.basename(file)
file = file.replace(f"{name} ", "")
file = file.replace(".txt", "")
predecessors.append(file)
if predecessors:
for x in range(len(predecessors)):
print(chr(x + 65), ". ", predecessors[x], sep = "")
while True:
choice = str(input("Which version is the predecessor? (Enter the corresponding option): "))
if len(choice) != 1:
print("This is not an option. Please try again.")
elif choice.isalpha() and ord(choice.upper()) - 65 in range(len(predecessors)):
choice = ord(choice.upper()) - 65
file = os.path.join(archive, f"{name} {predecessors[choice]}.txt")
predecessor = ds.Distribution(file, load_tracks = False)
previous_uuid = predecessor.uuid
break
else:
print("This is not an option. Please try again.")
else:
v = ft.question(f"Does {name} have a predecessor?")
if v:
previous_uuid = str(input(f"Enter the UUID of the previous version of {name}: "))
else:
previous_uuid = ""
# Open predecessor before continuing
if predecessors:
os.system(f"start notepad++ \"{predecessor.file.path}\"")
v = ft.question(f"Does {name} have a Wiimmfi region?")
if v:
region = str(input(f"Enter the region number of {name}: "))
else:
region = ""
url = str(input(f"Enter the Wiiki article URL of {name}: "))
# Define process mode
for mode, x in zip(cs.modes, range(len(cs.modes))):
print(f"{chr(x + 65)}. {mode}")
while True:
choice = str(input("What engine does the distribution use? (Enter the corresponding option): ")).upper()
if len(choice) != 1:
print("This is not an option. Please try again.")
elif choice.isalpha() and ord(choice.upper()) - 65 in range(len(cs.modes)):
choice = ord(choice.upper()) - 65
mode = cs.modes[choice]
break
else:
print("This is not an option. Please try again.")
compress = ft.question("Compress all SZS files?")
# Collect REL files and generate track listing if they exist
rel = []
for file in os.listdir(os.path.join(cwd, "Input")):
file = fl.File(os.path.join(cwd, "Input", file))
if file.filename.endswith("rel"):
rel.append(fl.REL(file.path))
if rel and all(static == rel[0] for static in rel):
tracklist = rel[0].tracklist()
else:
tracklist = cs.tracklist
if mode == "Mario Kart Wii":
# Rename all track files to "*tmp.szs" to avoid renaming issues
print("Renaming regular files...")
for file in os.listdir(os.path.join(cwd, "Input")):
file = fl.File(os.path.join(cwd, "Input", file))
if file.filename.endswith("szs") and file.filename[:-4].lower() in cs.filenames:
file.rename(file.filename[:-4] + "tmp.szs")
# Rename all track files to their new locations
for file in os.listdir(os.path.join(cwd, "Input")):
file = fl.File(os.path.join(cwd, "Input", file))
if file.filename.endswith("szs") and file.filename[:-7].lower() in cs.filenames:
old = cs.fcups[file.filename[:-7]]
new = cs.cfilenames[tracklist[old]]
file.rename(f"{new}.szs")
# Rename SZS files for proper sorting
if mode == "Pulsar":
print("Renaming custom files...")
for file in os.listdir(os.path.join(cwd, "Input")):
file = fl.File(os.path.join(cwd, "Input", file))
if file.filename.endswith("szs") and file.filename[:-4].isnumeric():
track = int(file.filename[:-4])
# 4 tracks per cup | Pulsar starts 8 cups ahead | Cup 0 does not exist
cup = track // 4 + 8 + 1
# 4 tracks per cup | Track 0 does not exist
slot = track % 4 + 1
file.rename(f"{cup}.{slot}.szs")
# Compress files and create distribution.txt
os.chdir(os.path.join(cwd, "Input"))
if compress:
print("Compressing files...")
sp.run(["wszst", "compress", "--szs", "--norm", "*.szs", "-o"])
sp.run(["wszst", "distrib", "."])
os.chdir(cwd)
distribution = fl.TXT(os.path.join(cwd, "Input", "distribution.txt"))
# Write contact and distribution information
print("Writing contact and distribution information...")
for line, contact in zip(list(range(112, 127, 3)), cs.ordered_contacts):
distribution.add_to_line(line, fl.CNT(os.path.join("Contacts", f"{contact}.cnt")).get_value())
distribution.rewrite(distribution.find("@NAME"), f"@NAME\t\t= {name}")
distribution.rewrite(distribution.find("@VERSION"), f"@VERSION\t= {version}")
distribution.rewrite(distribution.find("@AUTHORS"), f"@AUTHORS\t= {author}")
distribution.rewrite(distribution.find("@RELEASE-DATE"), f"@RELEASE-DATE\t= {date}")
distribution.rewrite(distribution.find("@PREDECESSOR"), f"@PREDECESSOR\t= {previous_uuid}")
distribution.rewrite(distribution.find("@WIIMMFI-REGION"), f"@WIIMMFI-REGION\t= {region}")
distribution.rewrite(distribution.find("@INFO-URL"), f"@INFO-URL\t= {url}")
# Remove standard SHA1s
begin = distribution.find("bt --------o -o---- 9047f6e9b77c6a44accb46c2237609b80e459fdc")
end = distribution.find("vs -----d--o -o---- 7142361ab93d3929f62aa715509fc8d1379afbd6")
distribution.remove(begin, end + 2)
# Prepare ID and family sets for distribution and predecessor
if predecessors:
print("Loading predecessor...")
old_families, old_IDs = predecessor.families_IDs()
else:
old_IDs = set(x for x in range(100_000))
old_families = set(x for x in range(100_000))
new_IDs = set()
# Collect and fill out track information in distribution file
print("Processing distribution...")
definition = distribution.read()[distribution.find("# for racing tracks") + 2:]
for x in range(len(definition)):
if definition[x] != "\n" and definition[x] != "":
# Split collected information
information = definition[x].split()
sha1 = information[0]
try:
filename = information[2]
except IndexError:
filename = information[1]
# Collect and write information, and move file accordingly
file = fl.File(os.path.join(cwd, "Input", f"{filename}.szs"))
entry = et.Entry(sha1, filename)
if entry.sha1_known():
entry.set_attributes(old_IDs, old_families, new_IDs)
new_IDs.add(entry.ID())
# Enable new, again and update mode if such a track is present
if entry.new == "N":
distribution.new = True
distribution.rewrite(distribution.find("@ENABLE-NEW"), "@ENABLE-NEW\t= 1")
if entry.again == "A":
distribution.again = True
distribution.rewrite(distribution.find("@ENABLE-AGAIN"), "@ENABLE-AGAIN\t= 1")
if entry.filler == "F":
distribution.filler = True
distribution.rewrite(distribution.find("@ENABLE-FILL"), "@ENABLE-FILL\t= 1")
if entry.update == "U":
distribution.update = True
distribution.rewrite(distribution.find("@ENABLE-UPDATE"), "@ENABLE-UPDATE\t= 1")
print(f"{file.filename: <30} is {entry.information()}.")
file.delete()
else:
print(f"{file.filename: <30} is unknown.")
file.move(os.path.join(cwd, "Output", file.filename))
distribution.rewrite(begin + x, str(entry))
# Clean repository
print("Cleaning repository...")
distribution.rename(f"{name} {version}.txt")
distribution.move(os.path.join(cwd, "Output", distribution.filename))
gitignore = fl.File(os.path.join(cwd, "Input", ".gitignore"))
gitignore.rename("tmp.gitignore")
gitignore.move_up(1)
fl.Folder(os.path.join(cwd, "Input")).empty()
gitignore.move_down(["Input"])
gitignore.rename(".gitignore")
os.system(f"start notepad++ \"{distribution.path}\"")
input("Press enter to move the file into the archive: ")
distribution.move(os.path.join(cwd, "Archive", distribution.filename))
input("All done!")