2020-02-13 15:16:55 +00:00
|
|
|
#! /usr/bin/python3
|
|
|
|
# Copyright (C) 2018-2020 Merspieler, merspieler _at_ airmail.cc
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License as
|
|
|
|
# published by the Free Software Foundation; either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but
|
|
|
|
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import hashlib
|
|
|
|
import re
|
2020-02-13 16:46:37 +00:00
|
|
|
import socket
|
2020-02-13 15:16:55 +00:00
|
|
|
from subprocess import run, Popen, STDOUT
|
|
|
|
from time import sleep
|
|
|
|
|
2021-04-18 00:00:22 +00:00
|
|
|
from common import norm, get_area_name_by_tile, api_get_job, api_send_status
|
2020-03-05 19:04:43 +00:00
|
|
|
|
2020-02-13 16:46:37 +00:00
|
|
|
host = socket.gethostname()
|
|
|
|
port = 12345
|
2021-03-10 19:32:11 +00:00
|
|
|
api = None
|
|
|
|
api_token = None
|
2021-04-18 00:00:22 +00:00
|
|
|
input_dir = ""
|
2020-02-13 15:16:55 +00:00
|
|
|
output = ""
|
2021-04-18 00:00:22 +00:00
|
|
|
tmp_dir = "/tmp/o2c-packager"
|
2020-02-13 15:16:55 +00:00
|
|
|
|
|
|
|
argc = len(sys.argv)
|
|
|
|
i = 1
|
|
|
|
first = 1
|
|
|
|
while i < argc:
|
2021-04-18 00:00:22 +00:00
|
|
|
if sys.argv[i] == "-o" or sys.argv[i] == "--output":
|
2020-02-13 15:16:55 +00:00
|
|
|
i += 1
|
|
|
|
output = sys.argv[i]
|
2021-04-18 00:00:22 +00:00
|
|
|
if sys.argv[i] == "-i" or sys.argv[i] == "--input":
|
|
|
|
i += 1
|
|
|
|
input_dir = sys.argv[i]
|
2021-03-10 19:32:11 +00:00
|
|
|
elif sys.argv[i] == "-a" or sys.argv[i] == "--api":
|
|
|
|
i += 1
|
|
|
|
api = sys.argv[i]
|
|
|
|
elif sys.argv[i] == "-t" or sys.argv[i] == "--api-token":
|
|
|
|
i += 1
|
|
|
|
api_token = sys.argv[i]
|
2020-02-13 15:16:55 +00:00
|
|
|
elif sys.argv[i] == "-h" or sys.argv[i] == "--help":
|
|
|
|
print("usage: worldbuild-packager.py [OPTIONS]")
|
|
|
|
print("Retrives done tiles from the manager and packages them")
|
|
|
|
print("")
|
2021-04-18 00:00:22 +00:00
|
|
|
print(" -i --input Input directory. Path MUST be absolute")
|
2021-03-10 19:32:11 +00:00
|
|
|
print(" -o, --output Output directory. Path MUST be absolute")
|
|
|
|
print(" -a, --api Manager api url")
|
|
|
|
print(" -t, --api-token Manager api token")
|
|
|
|
print(" -h, --help Shows this help and exit")
|
2020-02-13 15:16:55 +00:00
|
|
|
sys.exit(0)
|
|
|
|
else:
|
|
|
|
if first == 1:
|
|
|
|
first = 0
|
|
|
|
name = sys.argv[i]
|
|
|
|
else:
|
|
|
|
print("Unknown option " + sys.argv[i])
|
|
|
|
sys.exit(1)
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
if output == "":
|
|
|
|
print("ERROR: No output directory given")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2021-04-18 00:00:22 +00:00
|
|
|
if input_dir == "":
|
|
|
|
print("ERROR: No input directory given")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if not api:
|
|
|
|
print("ERROR: No api given")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if not api_token:
|
|
|
|
print("ERROR: No api token given")
|
2021-04-21 13:51:04 +00:00
|
|
|
sys.exit(1)
|
2021-04-18 00:00:22 +00:00
|
|
|
|
2020-02-13 15:16:55 +00:00
|
|
|
def get_sha1(fname):
|
2020-02-13 16:46:37 +00:00
|
|
|
if os.path.isfile(fname):
|
|
|
|
sha1 = hashlib.sha1()
|
|
|
|
with open(fname, 'rb') as f:
|
|
|
|
while True:
|
|
|
|
data = f.read(65536)
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
sha1.update(data)
|
|
|
|
return sha1.hexdigest()
|
|
|
|
else:
|
|
|
|
return None
|
2020-02-13 15:16:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
def read_index(fname):
|
|
|
|
index = {}
|
|
|
|
with open(fname) as f:
|
|
|
|
for line in f:
|
2021-01-13 20:45:19 +00:00
|
|
|
match = re.match("(version|time|path|d:.*):(.*)", line)
|
2020-02-13 15:16:55 +00:00
|
|
|
if match != None:
|
|
|
|
index[match.group(1)] = match.group(2)
|
|
|
|
else:
|
2021-01-13 20:45:19 +00:00
|
|
|
match = re.match("(.*:.*):(.*:.*)", line)
|
2020-02-13 15:16:55 +00:00
|
|
|
if match != None:
|
|
|
|
index[match.group(1)] = match.group(2)
|
|
|
|
return index
|
|
|
|
|
|
|
|
def write_index(fname, index):
|
|
|
|
with open(fname, 'w') as f:
|
|
|
|
for element in index:
|
2020-02-13 16:46:37 +00:00
|
|
|
f.write(element + ":" + str(index[element]) + "\n")
|
2020-02-13 15:16:55 +00:00
|
|
|
|
2021-01-13 20:45:19 +00:00
|
|
|
def update_parent_index(base, path, changed_dir):
|
|
|
|
if os.path.isfile(base + "/" + path + "/.dirindex"):
|
|
|
|
index = read_index(base + "/" + path + "/.dirindex")
|
2020-02-13 15:16:55 +00:00
|
|
|
else:
|
|
|
|
index = {}
|
|
|
|
index["version"] = 1
|
2021-01-13 20:45:19 +00:00
|
|
|
if path != "":
|
2021-01-26 21:30:15 +00:00
|
|
|
index["path"] = path
|
2021-01-13 20:45:19 +00:00
|
|
|
else:
|
2021-01-26 21:30:15 +00:00
|
|
|
index["path"] = ""
|
2020-02-13 15:16:55 +00:00
|
|
|
|
2021-01-13 20:45:19 +00:00
|
|
|
sha = get_sha1(base + "/" + path + "/" + changed_dir + "/.dirindex")
|
2020-02-13 16:46:37 +00:00
|
|
|
if sha != None:
|
2021-01-13 20:45:19 +00:00
|
|
|
index["d:" + changed_dir] = sha
|
2020-02-13 15:16:55 +00:00
|
|
|
|
2021-01-13 20:45:19 +00:00
|
|
|
write_index(base + "/" + path + "/.dirindex", index)
|
2020-02-13 15:16:55 +00:00
|
|
|
|
|
|
|
|
2021-01-13 20:45:19 +00:00
|
|
|
def update_index(output, part, name, name_major):
|
|
|
|
if os.path.isfile(output + "/" + part + "/" + name_major + "/.dirindex"):
|
|
|
|
index = read_index(output + "/" + part + "/" + name_major + "/.dirindex")
|
|
|
|
else:
|
|
|
|
index = {}
|
|
|
|
index["version"] = 1
|
2021-01-26 21:30:15 +00:00
|
|
|
index["path"] = part + "/" + name_major
|
2020-02-13 15:16:55 +00:00
|
|
|
|
2021-01-13 20:45:19 +00:00
|
|
|
sha = get_sha1(output + "/" + part + "/" + name_major + "/" + name + ".txz")
|
|
|
|
if sha != None:
|
|
|
|
index["t:" + name + ".txz"] = sha + ":" + str(os.path.getsize(output + "/" + part + "/" + name_major + "/" + name + ".txz"))
|
|
|
|
|
|
|
|
write_index(output + "/" + part + "/" + name_major + "/.dirindex", index)
|
2020-02-13 15:16:55 +00:00
|
|
|
|
2021-01-13 20:45:19 +00:00
|
|
|
update_parent_index(output, part, name_major)
|
|
|
|
update_parent_index(output, "", part)
|
2020-02-13 15:16:55 +00:00
|
|
|
|
2021-05-14 15:13:49 +00:00
|
|
|
# Always make sure, your tmp dirs exists
|
|
|
|
for part in os.listdir(input_dir):
|
|
|
|
run("mkdir -p " + tmp_dir + "/" + part, shell=True)
|
|
|
|
|
|
|
|
previous_area = ""
|
2020-02-13 15:16:55 +00:00
|
|
|
running = True
|
|
|
|
while running:
|
|
|
|
try:
|
2021-06-18 21:57:57 +00:00
|
|
|
tiles = "None"
|
|
|
|
while tiles == "None":
|
|
|
|
tiles = api_get_job("done", "packaging", api, api_token, none_exit=False, all_in_parent=1)
|
|
|
|
if tiles == "None":
|
2020-03-17 17:44:28 +00:00
|
|
|
print("No job got asigned. Trying again in one hour.")
|
|
|
|
sleep(3600)
|
2020-02-13 15:16:55 +00:00
|
|
|
|
2021-06-18 21:57:57 +00:00
|
|
|
area = get_area_name_by_tile(tiles[0])
|
|
|
|
area_major = get_area_name_by_tile(tiles[0], major=True)
|
2020-02-13 15:16:55 +00:00
|
|
|
|
2021-06-18 21:57:57 +00:00
|
|
|
print("Packaging " + area + "...")
|
2021-05-14 15:13:49 +00:00
|
|
|
|
2021-04-18 00:00:22 +00:00
|
|
|
for part in os.listdir(input_dir):
|
2021-05-14 15:13:49 +00:00
|
|
|
tmp_dir_part = tmp_dir + "/" + part
|
2020-02-13 15:16:55 +00:00
|
|
|
|
2021-05-14 15:13:49 +00:00
|
|
|
# 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"):
|
2021-06-18 21:57:57 +00:00
|
|
|
run("bash -c 'cd " + tmp_dir_part + " && tar -xf " + output + "/" + part + "/" + area_major + "/" + area + ".txz'", shell=True)
|
|
|
|
for name in tiles:
|
|
|
|
name = str(name)
|
|
|
|
run("bash -c 'cd " + tmp_dir_part + " && rm -f " + area + "/*" + name + "*'", shell=True)
|
2021-05-14 15:13:49 +00:00
|
|
|
run("mkdir -p " + tmp_dir_part + "/" + area, shell=True)
|
2020-02-13 15:16:55 +00:00
|
|
|
|
2021-04-18 00:00:22 +00:00
|
|
|
# If there's new scenery copy it into the tmp dir
|
2021-06-18 21:57:57 +00:00
|
|
|
for name in tiles:
|
|
|
|
name = str(name)
|
|
|
|
if os.path.exists(input_dir + "/" + part + "/" + area_major + "/" + area + "/" + name + ".stg"):
|
|
|
|
run("cp " + input_dir + "/" + part + "/" + area_major + "/" + area + "/*" + name + "* " + tmp_dir_part + "/" + area + "/", shell=True)
|
2020-02-13 15:16:55 +00:00
|
|
|
|
2021-04-18 00:00:22 +00:00
|
|
|
# If the scenery is not empty, package it
|
2021-05-14 15:13:49 +00:00
|
|
|
if os.listdir(tmp_dir_part + "/" + area):
|
2021-04-18 00:00:22 +00:00
|
|
|
run("mkdir -p " + output + "/" + part + "/" + area_major, shell=True)
|
2020-02-13 16:46:37 +00:00
|
|
|
|
2021-05-14 15:13:49 +00:00
|
|
|
cmd = "bash -c 'cd " + tmp_dir_part + " && tar -cJf " + output + "/" + area + ".txz " + area + "/'"
|
2021-01-13 20:45:19 +00:00
|
|
|
with open(os.devnull, 'w') as fp:
|
|
|
|
package = Popen(cmd , shell=True, start_new_session=True, stdout=fp)
|
|
|
|
package.wait()
|
2021-05-08 08:21:45 +00:00
|
|
|
run("mv " + output + "/" + area + ".txz " + output + "/" + part + "/" + area_major + "/", shell=True)
|
2020-02-13 15:16:55 +00:00
|
|
|
|
2021-04-18 00:00:22 +00:00
|
|
|
update_index(output, part, area, area_major)
|
|
|
|
|
2020-02-13 16:46:37 +00:00
|
|
|
|
2021-06-18 21:57:57 +00:00
|
|
|
print("Packaging " + area + " done")
|
2021-05-14 15:13:49 +00:00
|
|
|
previous_area = area
|
2020-02-13 15:16:55 +00:00
|
|
|
|
2021-06-18 21:57:57 +00:00
|
|
|
for name in tiles:
|
|
|
|
name = str(name)
|
|
|
|
api_send_status(name, "packaged", api, api_token)
|
2020-02-13 15:16:55 +00:00
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print("Graceful shutdown triggered. To force immedate stop, press Ctrl+C again")
|
|
|
|
running = False
|
|
|
|
try:
|
|
|
|
package.wait()
|
2021-05-14 15:13:49 +00:00
|
|
|
run("mv " + output + "/" + area + ".txz " + output + "/" + part + "/" + area_major + "/", shell=True)
|
2021-06-18 22:57:16 +00:00
|
|
|
update_index(output, area, area_major)
|
2021-04-18 00:00:22 +00:00
|
|
|
api_send_status(name, "packaged", api, api_token)
|
2020-02-13 15:16:55 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
# TODO doesn't work
|
|
|
|
print("Forcing shutdown...")
|
|
|
|
package.terminate()
|
|
|
|
sleep(5)
|
|
|
|
package.kill()
|
|
|
|
sys.exit(0)
|