def restart(args): import json base = base_option(args) os.environ["SAXO_BASE"] = base if "." in __name__: from .saxo import data, version else: from saxo import data, version sys_args = json.loads(data("args", command="script.py", check=False)) if sys_args[0] == "restart": debug("Recursion detected!", sys_args) debug("Not restarting") sys.exit(1) debug("Stopping existing instance...") stop(args) argv = [sys.argv[0]] + sys_args debug("Restarting using:", argv) main(argv, version)
def start(args): import json # Quit any previously connected instances if "." in __name__: from .saxo import client, data else: from saxo import client, data base = base_option(args) try: client("quit", base=base) except FileNotFoundError as err: ... except ConnectionRefusedError as err: ... except PermissionError as err: debug("Error: Unable to connect to internal socket", file=sys.stderr) debug("Check permissions on the config dir", file=sys.stderr) sys.stderr.flush() sys.exit() else: debug("Warning: Client may already have been running!") sys.stdout.flush() pidfile = os.path.join(base, "pid") if not args.foreground: # Save PEP 3122! if "." in __name__: from . import daemon else: import daemon if args.log: log = os.path.join(base, "log") if os.path.exists(log): import shutil modified = time.gmtime(os.path.getmtime("saxo")) log2 = time.strftime("log-%Y%m%d-%H%M%S.txt", modified) shutil.move(log, os.path.join(base, log2)) output = open(log, "w") else: output = open(os.devnull, "w") daemon.start(pidfile, output) else: # This is duplicated variatim from daemon.py import atexit with open(pidfile, "w") as f: f.write(str(os.getpid()) + "\n") def delete_pidfile(): if os.path.isfile(pidfile): os.remove(pidfile) atexit.register(delete_pidfile) if args.action == "start": # Otherwise you get recursion if args.action == "restart" os.environ["SAXO_BASE"] = base sys_args = json.dumps(args.original) data("args", sys_args, command="script.py", check=False) # Save PEP 3122! if "." in __name__: from . import irc else: import irc irc.start(base) return 0