worldbuild/worldbuild.py

111 lines
3.2 KiB
Python
Raw Normal View History

#! /usr/bin/python3
# Copyright (C) 2018-2023 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
from subprocess import run, Popen, STDOUT
import sys
import socket
import re
from time import sleep, strftime, time
import threading
import signal
from common import send_status, norm, get_south, get_west, get_east, get_north, get_area_name, api_send_status, api_get_job, api_get_options
import config
threads = 1
quiet = False
running = True
argc = len(sys.argv)
i = 1
first = 1
while i < argc:
if sys.argv[i] == "-T" or sys.argv[i] == "--threads":
i += 1
threads = int(sys.argv[i])
elif sys.argv[i] == "-q" or sys.argv[i] == "--quiet":
quiet = True
elif sys.argv[i] == "-h" or sys.argv[i] == "--help":
print("usage:worldbuild.py [OPTIONS]")
print("TODO")
print("")
print(" -T, --threads n Number of concurent threads")
print(" -q, --quiet Don't print messages")
print("")
print(" -h, --help Shows this help and exit")
sys.exit(0)
else:
print("Unknown option " + sys.argv[i])
sys.exit(1)
i += 1
if config.api == None:
print("Error: No API url given")
sys.exit(1)
if config.api_token == None:
print("Error: No API token given")
sys.exit(1)
if threads < 1:
print("Error: Invalid number of threads given")
sys.exit(1)
run("mkdir -p logs", shell=True)
def processThread():
global running
while running:
name = api_get_job(config.process_job_status, config.process_status_started, config.api, config.api_token, config.level)
if name == None and running:
sleep(60)
continue
options = api_get_options(name, config.api, config.api_token)
cmd, status = config.get_command(name, options)
# If config tells us a different status than we expect
if status != config.process_status_started:
api_send_status(name, status, config.api, config.api_token)
if cmd != None:
logpath = "logs/" + name + "/"
run("mkdir -p " + logpath, shell=True)
with open(logpath + name + "-" + strftime("%Y%m%d-%H%M") + ".log", "w") as log_file:
build = Popen(cmd, stdout=log_file, stderr=STDOUT, shell=True, start_new_session=True)
ret = build.wait()
status = config.check_result(ret)
api_send_status(name, status, config.api, config.api_token)
def exit(signal, frame):
global running
print("Stopping threads...")
running = False
for t in threadObjs:
t.join()
sys.exit(0)
signal.signal(signal.SIGINT, exit)
threadObjs = []
for i in range(0, threads):
t = threading.Thread(target=processThread)
t.start()
threadObjs.append(t)
while True:
sleep(315360000)