simplified code and added more special case support

This commit is contained in:
raphael60650 2021-06-18 16:58:16 +02:00
parent d91a4c9241
commit 1296ba6e50

View file

@ -13,13 +13,14 @@ def count_track(self):
for cup in ctconfig["cup"].values(): for cup in ctconfig["cup"].values():
if not (cup["locked"]): tracks.extend(cup["courses"].values()) if not (cup["locked"]): tracks.extend(cup["courses"].values())
tracks.extend(ctconfig["tracks_list"]) tracks.extend(ctconfig["tracks_list"])
tracks = [dict(t) for t in {tuple(d.items()) for d in tracks}]
total_track = len(tracks) total_track = len(tracks)
return tracks, total_track return tracks, total_track
def patch_autoadd(self): def patch_autoadd(self):
if os.path.exists("./file/auto-add"): shutil.rmtree("./file/auto-add") if os.path.exists("./file/auto-add"): shutil.rmtree("./file/auto-add")
if not (os.path.exists(self.path_mkwf + "/tmp/")): os.makedirs(self.path_mkwf + "/tmp/") if not os.path.exists(self.path_mkwf + "/tmp/"): os.makedirs(self.path_mkwf + "/tmp/")
subprocess.run(["./tools/szs/wszst", "AUTOADD", get_nodir(self.path_mkwf) + "/files/Race/Course/", subprocess.run(["./tools/szs/wszst", "AUTOADD", get_nodir(self.path_mkwf) + "/files/Race/Course/",
"--DEST", get_nodir(self.path_mkwf) + "/tmp/auto-add/"], "--DEST", get_nodir(self.path_mkwf) + "/tmp/auto-add/"],
creationflags=CREATE_NO_WINDOW, cwd=get_dir(self.path_mkwf), creationflags=CREATE_NO_WINDOW, cwd=get_dir(self.path_mkwf),
@ -34,54 +35,53 @@ def patch_track(self, tracks, total_track="?"):
error_count, error_max = 0, 3 error_count, error_max = 0, 3
for i, track in enumerate(tracks): for i, track in enumerate(tracks):
track_file = track["name"] track_file = get_trackname(track=track)
if "prefix" in track: track_file = track["prefix"] + " " + track_file
if "suffix" in track: track_file = track_file + " (" + track["suffix"] + ")"
track_wu8_file = f"./file/Track-WU8/{track_file}.wu8"
track_szs_file = f"./file/Track/{track_file}.szs"
while True: while True:
if len(process_list) < max_process: if len(process_list) < max_process:
process_list[(track_szs_file, track_wu8_file)] = None process_list[track_file] = None # Used for
self.Progress(statut=self.translate("Conversion des courses") + f"\n({i + 1}/{total_track})\n" + self.Progress(statut=self.translate("Conversion des courses") + f"\n({i + 1}/{total_track})\n" +
"\n".join([get_nodir(process[0]) for process in process_list]), add=1) "\n".join(process_list.keys()), add=1)
for track_file in [track_szs_file, track_wu8_file]: for _track in [get_track_szs(track_file), get_track_wu8(track_file)]:
if os.path.exists(track_file): if os.path.exists(_track):
if os.path.getsize(track_file) < 1000: # File under this size are corrupted if os.path.getsize(_track) < 1000: # File under this size are corrupted
os.remove(track_file) os.remove(_track)
dl_code = self.get_github_file(track_wu8_file) download_returncode = self.get_github_file(get_track_wu8(track_file))
if dl_code == -1: if download_returncode == -1: # can't download
error_count += 1 error_count += 1
if error_count > error_max: # Too much track wasn't correctly converted if error_count > error_max: # Too much track wasn't correctly converted
messagebox.showerror( messagebox.showerror(
self.translate("Erreur"), self.translate("Erreur"),
self.translate("Trop de course ont eu une erreur du téléchargement.")) self.translate("Trop de course ont eu une erreur du téléchargement."))
return return -1
else: else:
messagebox.showwarning(self.translate("Attention"), messagebox.showwarning(self.translate("Attention"),
self.translate("Impossible de télécharger cette course ! (") + self.translate("Impossible de télécharger cette course ! (") +
str(error_count) + "/" + str(error_max) + ")") str(error_count) + "/" + str(error_max) + ")")
if not (os.path.exists(track_szs_file)): if not (os.path.exists(get_track_szs(track_file))) or download_returncode == 3: # returncode 3 is track has been updated
process_list[(track_szs_file, track_wu8_file)] = subprocess.Popen([ if os.path.exists(get_track_wu8(track_file)):
"./tools/szs/wszst", "NORMALIZE", track_wu8_file, "--DEST", process_list[track_file] = subprocess.Popen([
"./tools/szs/wszst", "NORMALIZE", get_track_wu8(track_file), "--DEST",
"./file/Track/%N.szs", "--szs", "--overwrite", "--autoadd-path", "./file/Track/%N.szs", "--szs", "--overwrite", "--autoadd-path",
"./file/auto-add/"], creationflags=CREATE_NO_WINDOW, stderr=subprocess.PIPE) "./file/auto-add/"], creationflags=CREATE_NO_WINDOW, stderr=subprocess.PIPE)
else:
messagebox.showerror(self.translate("Erreur"),
self.translate("Impossible de convertir la course.\n"
"Réactiver le téléchargement des courses et réessayer."))
return -1
elif self.boolvar_del_track_after_conv.get(): os.remove(get_track_wu8(track_file))
break break
else: else:
for (track_szs_file, track_wu8_file), process in process_list.items(): for _track_file, process in process_list.items():
key = (track_szs_file, track_wu8_file)
if process is not None: if process is not None:
returncode = process.poll() if process.poll() is None: pass # if the process is still running
if returncode is None:
pass # if the process is still running
else: # process ended else: # process ended
process_list.pop(_track_file)
stderr = process.stderr.read() stderr = process.stderr.read()
if b"wszst: ERROR" in stderr: # Error occured if b"wszst: ERROR" in stderr: # Error occured
process_list.pop(key) os.remove(get_track_szs(_track_file))
os.remove(track_szs_file)
error_count += 1 error_count += 1
if error_count > error_max: # Too much track wasn't correctly converted if error_count > error_max: # Too much track wasn't correctly converted
messagebox.showerror( messagebox.showerror(
@ -92,15 +92,15 @@ def patch_track(self, tracks, total_track="?"):
messagebox.showwarning( messagebox.showwarning(
self.translate("Attention"), self.translate("Attention"),
self.translate("La course ") + self.translate("La course ") +
track_wu8_file + get_track_wu8(_track_file) +
self.translate(" n'a pas été correctement converti. (") + self.translate(" n'a pas été correctement converti. (") +
str(error_count) + "/" + str(error_max) + ")") str(error_count) + "/" + str(error_max) + ")")
break break
else: else:
if self.boolvar_del_track_after_conv.get(): os.remove(track_wu8_file) if self.boolvar_del_track_after_conv.get(): os.remove(get_track_wu8(_track_file))
process_list.pop(key)
break break
else: else:
process_list.pop(key) process_list.pop(_track_file)
break break
return 0