From 1b0b54c5f27be526c829dd7fc6772ed0a565eefd Mon Sep 17 00:00:00 2001 From: Zevaryx Date: Thu, 24 Jun 2021 19:13:10 -0600 Subject: [PATCH] Add CLI management system --- .gitignore | 3 ++ jarvis/__init__.py | 4 ++ run.py | 98 +++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 104 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 14ca96a..cb5b9f7 100644 --- a/.gitignore +++ b/.gitignore @@ -139,3 +139,6 @@ cython_debug/ # Config file config.yaml + +# PID files +*.pid diff --git a/jarvis/__init__.py b/jarvis/__init__.py index 10290fc..bdb4498 100644 --- a/jarvis/__init__.py +++ b/jarvis/__init__.py @@ -1,3 +1,4 @@ +from pathlib import Path from discord import Intents from discord.ext import commands from discord.utils import find @@ -18,6 +19,8 @@ jarvis_self = Process() async def on_ready(): print("Logged in as {0.user}".format(jarvis)) print("Connected to {} guild(s)".format(len(jarvis.guilds))) + with jarvis_self.oneshot(): + Path(f"jarvis.{jarvis_self.pid}.pid").touch() @jarvis.event @@ -45,3 +48,4 @@ def run(): + "{}&permissions=8&scope=bot".format(config.client_id) ) jarvis.run(config.token, bot=True, reconnect=True) + Path(f"jarvis.{jarvis_self.pid}.pid").unlink() diff --git a/run.py b/run.py index 37cd17e..0eac9c7 100644 --- a/run.py +++ b/run.py @@ -1,5 +1,101 @@ #!/bin/python3 import jarvis +import os +import signal +import git +from pathlib import Path +from multiprocessing import Process +from importlib import reload as ireload +from time import sleep + + +def run(): + ireload(jarvis) + jarvis.run() + + +jarvis_process = Process(target=run, name="jarvis") + + +def start_process(): + global jarvis_process + if jarvis_process.is_alive(): + jarvis_process.terminate() + jarvis_process.join(10) + jarvis_process = Process(target=run, name="jarvis") + jarvis_process.start() + + +def update(): + repo = git.Repo(".") + dirty = repo.is_dirty() + if dirty: + print(" Local system has uncommitted changes.") + current_hash = repo.head.object.hexsha + origin = repo.remotes.origin + origin.fetch() + if current_hash != origin.refs["main"].object.hexsha: + if dirty: + return 2 + origin.pull() + return 0 + return 1 + + +def cli(): + pid_file = Path(f"jarvis.{jarvis_process.pid}.pid") + while not pid_file.exists(): + sleep(0.2) + print( + """ + All systems online. + +Command List: + (R)eload + (U)pdate + (Q)uit + """ + ) + while True: + cmd = input("> ") + if cmd.lower() in ["q", "quit", "e", "exit"]: + pid_file.unlink() + break + if cmd.lower() in ["u", "update"]: + print("-> Updating core systems...") + status = update() + if status == 0: + start_process() + pid_file = Path(f"jarvis.{jarvis_process.pid}.pid") + while not pid_file.exists(): + sleep(0.2) + print(" Core systems successfully updated.") + elif status == 1: + print(" No core updates available.") + elif status == 2: + print(" Core updates available, but not applied.") + if cmd.lower() in ["r", "reload"]: + print(" Reloading core systems...") + pid_file.unlink() + start_process() + pid_file = Path(f"jarvis.{jarvis_process.pid}.pid") + while not pid_file.exists(): + sleep(0.2) + print(" All systems reloaded.") + if __name__ == "__main__": - jarvis.run() + print("J.A.R.V.I.S. initializing....") + print(" Updating core systems...") + status = update() + if status == 0: + print(" Core systems successfully updated") + elif status == 1: + print(" No core updates available.") + elif status == 2: + print(" Core updates available, but not applied.") + start_process() + cli() + if jarvis_process.is_alive(): + jarvis_process.terminate() + jarvis_process.join(30)