duty-board-dog/main.py

48 lines
1.2 KiB
Python
Raw Normal View History

2023-10-12 18:30:55 +02:00
# SPDX-FileCopyrightText: 2023 Egor Guslyancev <electromagneticcyclone@disroot.org>
#
2023-12-04 10:13:56 +01:00
# SPDX-License-Identifier: AGPL-3.0-or-later
2023-10-12 18:30:55 +02:00
2023-12-13 20:52:30 +01:00
"Executable with autoupdate. Only runs bot module."
2023-10-27 15:31:53 +02:00
import os
2023-10-27 11:39:01 +02:00
import signal
2023-10-27 15:31:53 +02:00
import time
2023-10-27 11:39:01 +02:00
import subprocess
2023-12-18 19:24:25 +01:00
from sys import stderr
2023-10-12 18:30:55 +02:00
2023-12-13 20:52:30 +01:00
class Runner:
"Runs optimized bot script."
__proc = None
2023-10-12 18:30:55 +02:00
2023-12-13 20:52:30 +01:00
def stop(self):
"Stop the script."
os.kill(self.__proc.pid, signal.SIGTERM)
2023-10-12 18:30:55 +02:00
2023-12-13 20:52:30 +01:00
def start(self):
"Start the script."
self.__proc = subprocess.Popen(["python", "-OO", "bot.py"])
2023-10-27 11:46:37 +02:00
2023-10-12 18:30:55 +02:00
2023-12-13 20:52:30 +01:00
bot_runner = Runner()
if __name__ == "__main__":
2023-10-27 15:31:53 +02:00
branch = os.popen("git rev-parse --abbrev-ref HEAD").read()[:-1]
2023-12-13 20:52:30 +01:00
bot_runner.start()
2023-10-27 15:31:53 +02:00
while True:
2023-12-18 19:24:25 +01:00
stderr.write("main.py: Updater\n")
2023-10-31 10:29:39 +01:00
os.system("git fetch --quiet")
2023-12-13 20:52:30 +01:00
if (
os.popen("git log " + branch + "..origin/" + branch + " --oneline")
.read()
.strip()
!= ""
):
2023-12-18 19:24:25 +01:00
stderr.write("main.py: Updating from git\n")
2023-12-13 20:52:30 +01:00
bot_runner.stop()
2023-11-01 06:10:52 +01:00
os.system("git pull --quiet")
2023-10-31 10:29:39 +01:00
os.system("git gc --quiet --aggressive --prune=all")
2023-12-13 20:52:30 +01:00
bot_runner.start()
2023-10-28 20:39:58 +02:00
time.sleep(60 * 60 * 1)