jarvis-bot/run.py

101 lines
2.6 KiB
Python

#!/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__":
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)