def stop(args): base = base_option(args) if "." in __name__: from .saxo import client else: from saxo import client try: client("quit", base=base) except: ... pidfile = os.path.join(base, "pid") if not os.path.exists(pidfile): common.error("There is no bot currently running") with open(pidfile, encoding="ascii") as f: text = f.read() pid = int(text.rstrip("\n")) # TODO: Make this less crude # This can give a ProcessLookupError: [Errno 3] No such process try: os.kill(pid, signal.SIGTERM) except ProcessLookupError: ... # os.kill(pid, signal.SIGKILL) return 0
def console(args): # TODO: Reduce code duplication base = base_option(args) # Save PEP 3122! if "." in __name__: from . import saxo else: import saxo path = os.environ.get("PATH", "") commands = os.path.join(base, "commands") os.environ["PATH"] = commands + os.pathsep + path os.environ["PYTHONPATH"] = saxo.path os.environ["SAXO_SHELL"] = "1" os.environ["SAXO_BASE"] = base os.environ["SAXO_BOT"] = "saxo" os.environ["SAXO_COMMANDS"] = commands os.environ["SAXO_NICK"] = os.environ["USER"] os.environ["SAXO_SENDER"] = os.environ["USER"] def do(cmd, arg): import subprocess cmd = os.path.join(os.environ["SAXO_COMMANDS"], cmd) try: octets = subprocess.check_output([cmd, arg]) except Exception as err: return "Error: %s" % err return octets.decode("utf-8") while True: sys.stdout.write("> ") sys.stdout.flush() try: i = sys.stdin.readline() except: break i = i.rstrip("\r\n") if " " in i: cmd, arg = i.split(" ", 1) else: cmd, arg = i, "" if cmd.startswith("."): saxo.client(cmd[1:], *eval("(%s)" % arg)) elif cmd.startswith("!"): print(do(cmd[1:], arg).rstrip("\r\n")) elif cmd == "saxo.path": print(saxo.path) elif cmd == "saxo.base": print(base) elif not cmd: break else: print("Unknown command:", repr(cmd))
def status(args): base = base_option(args) # Quit any previously connected instances if "." in __name__: from .saxo import client else: from saxo import client try: client("noop", base=base) except FileNotFoundError as err: debug("not running") except ConnectionRefusedError as err: debug("not running") else: debug("running")
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
def console(args): # TODO: Reduce code duplication base = base_option(args) # Save PEP 3122! if "." in __name__: from . import saxo else: import saxo path = os.environ.get("PATH", "") commands = os.path.join(base, "commands") # TODO: Reload symlinks # e.g. the pip-installation test changes them os.environ["PATH"] = commands + os.pathsep + path os.environ["PYTHONPATH"] = saxo.path os.environ["SAXO_SHELL"] = "1" os.environ["SAXO_BASE"] = base os.environ["SAXO_BOT"] = "saxo" os.environ["SAXO_COMMANDS"] = commands os.environ["SAXO_NICK"] = os.environ["USER"] os.environ["SAXO_SENDER"] = os.environ["USER"] def do(cmd, arg): import subprocess cmd = os.path.join(os.environ["SAXO_COMMANDS"], cmd) try: octets = subprocess.check_output([cmd, arg]) except Exception as err: return "Error: %s" % err return octets.decode("utf-8") while True: sys.stdout.write("> ") sys.stdout.flush() try: i = sys.stdin.readline() except: break i = i.rstrip("\r\n") if " " in i: cmd, arg = i.split(" ", 1) else: cmd, arg = i, "" if cmd.startswith("."): saxo.client(cmd[1:], *eval("(%s)" % arg)) elif cmd.startswith("!"): print(do(cmd[1:], arg).rstrip("\r\n")) elif cmd == "saxo.path": print(saxo.path) elif cmd == "saxo.base": print(base) elif not cmd: break else: print("Unknown command:", repr(cmd))