Fix bug and reuse dir if possible

Signed-off-by: fly <merspieler@airmail.cc>
This commit is contained in:
fly 2021-05-14 17:13:49 +02:00
parent 1626816e2a
commit f872846244

View file

@ -151,8 +151,11 @@ def update_index(output, part, name, name_major):
update_parent_index(output, part, name_major)
update_parent_index(output, "", part)
# Always make sure, your tmp dir exists
run("mkdir -p " + tmp_dir, shell=True)
# Always make sure, your tmp dirs exists
for part in os.listdir(input_dir):
run("mkdir -p " + tmp_dir + "/" + part, shell=True)
previous_area = ""
running = True
while running:
try:
@ -168,24 +171,27 @@ while running:
area = get_area_name_by_tile(name)
area_major = get_area_name_by_tile(name, major=True)
for part in os.listdir(input_dir):
#run("rm -f " + output + "/" + part + "/" + area_major + "/" + area + ".txz", shell=True)
# If there is an archive, we need to extract it first and remove old data
if os.path.exists(output + "/" + part + "/" + area_major + "/" + area + ".txz"):
run("bash -c 'cd " + tmp_dir + " && tar -xf " + output + "/" + part + "/" + area_major + "/" + area + ".txz && rm -f " + area + "/*" + name + "*'", shell=True)
else:
run("mkdir -p " + tmp_dir + "/" + area, shell=True)
for part in os.listdir(input_dir):
tmp_dir_part = tmp_dir + "/" + part
# Check if we can reuse the already extracted structure
if previous_area != area:
run("rm -rf " + tmp_dir_part + "/" + area, shell=True)
# If there is an archive, we need to extract it first and remove old data
if os.path.exists(output + "/" + part + "/" + area_major + "/" + area + ".txz"):
run("bash -c 'cd " + tmp_dir_part + " && tar -xf " + output + "/" + part + "/" + area_major + "/" + area + ".txz && rm -f " + area + "/*" + name + "*'", shell=True)
run("mkdir -p " + tmp_dir_part + "/" + area, shell=True)
# If there's new scenery copy it into the tmp dir
if os.path.exists(input_dir + "/" + part + "/" + area_major + "/" + area + "/" + name + ".stg"):
run("cp " + input_dir + "/" + part + "/" + area_major + "/" + area + "/*" + name + "* " + tmp_dir + "/" + area + "/", shell=True)
run("cp " + input_dir + "/" + part + "/" + area_major + "/" + area + "/*" + name + "* " + tmp_dir_part + "/" + area + "/", shell=True)
# If the scenery is not empty, package it
if os.listdir(tmp_dir + "/" + area):
if os.listdir(tmp_dir_part + "/" + area):
run("mkdir -p " + output + "/" + part + "/" + area_major, shell=True)
cmd = "bash -c 'cd " + tmp_dir + " && tar -cJf " + output + "/" + area + ".txz " + area + "/'"
cmd = "bash -c 'cd " + tmp_dir_part + " && tar -cJf " + output + "/" + area + ".txz " + area + "/'"
with open(os.devnull, 'w') as fp:
package = Popen(cmd , shell=True, start_new_session=True, stdout=fp)
package.wait()
@ -193,10 +199,9 @@ while running:
update_index(output, part, area, area_major)
# Clean up
run("rm -r " + tmp_dir + "/" + area, shell=True)
print("Packaging " + name + " done")
previous_area = area
api_send_status(name, "packaged", api, api_token)
@ -205,7 +210,7 @@ while running:
running = False
try:
package.wait()
run("mv " + output + "/" + area + ".txz " + output + "/" + part + "/" + area_major + "/")
run("mv " + output + "/" + area + ".txz " + output + "/" + part + "/" + area_major + "/", shell=True)
update_index(output, area, name_major)
api_send_status(name, "packaged", api, api_token)
except KeyboardInterrupt: