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