def start(base): # TODO: Check when two clients are running common.exit_cleanly() # http://stackoverflow.com/questions/11423225 # IGN rather than DFL, otherwise Popen.communicate can quit saxo signal.signal(signal.SIGPIPE, signal.SIG_IGN) opt = configparser.ConfigParser(interpolation=None) config = os.path.join(base, "config") if not os.path.isfile(config): error("missing config file in: `%s`" % config, E_NO_CONFIG) opt.read(config) # TODO: Defaulting? # TODO: Warn if the config file is widely readable? common.populate(saxo_path, base) sockname = os.path.join(base, "client.sock") serve(sockname, incoming) os.chmod(sockname, 0o600) # NOTE: If using os._exit, this doesn't work def remove_sock(sockname): if os.path.exists(sockname): os.remove(sockname) atexit.register(remove_sock, sockname) common.thread(scheduler.start, base, incoming) saxo = Saxo(base, opt) saxo.run()
def start(base): # TODO: Check when two clients are running common.exit_cleanly() # http://stackoverflow.com/questions/11423225 # IGN rather than DFL, otherwise Popen.communicate can quit saxo signal.signal(signal.SIGPIPE, signal.SIG_IGN) opt = configparser.ConfigParser(interpolation=None) config = os.path.join(base, "config") if not os.path.isfile(config): error("missing config file in: `%s`" % config, E_NO_CONFIG) opt.read(config) # TODO: Defaulting? # TODO: Warn if the config file is widely readable? sockname = os.path.join(base, "client.sock") serve(sockname, incoming) os.chmod(sockname, 0o600) # NOTE: If using os._exit, this doesn't work def remove_sock(sockname): if os.path.exists(sockname): os.remove(sockname) atexit.register(remove_sock, sockname) sched = scheduler.Scheduler(incoming) common.thread(sched.start, base) saxo = Saxo(base, opt) saxo.run()
def start(base): # TODO: Check when two clients are running common.exit_cleanly() # http://stackoverflow.com/questions/11423225 signal.signal(signal.SIGPIPE, signal.SIG_DFL) plugins = os.path.join(base, "plugins") if not os.path.isdir(plugins): common.error("no plugins directory: `%s`" % plugins, E_NO_PLUGINS) # TODO: Check for broken symlinks common.populate(saxo_path, base) opt = configparser.ConfigParser(interpolation=None) config = os.path.join(base, "config") opt.read(config) # TODO: Defaulting? # TODO: Warn if the config file is widely readable? sockname = os.path.join(base, "client.sock") serve(sockname, incoming) os.chmod(sockname, 0o600) # NOTE: If using os._exit, this doesn't work def remove_sock(sockname): if os.path.exists(sockname): os.remove(sockname) atexit.register(remove_sock, sockname) common.thread(scheduler.start, base, incoming) saxo = Saxo(base, opt) saxo.run()
def pipe(function): # This gives you: # * concise code # * clean exiting # * input surrogate decoding # * output encoding # * a custom error wrapper # TODO: Would like to run this in caller __name__ == "__main__" # __name__ here is "saxo" import os import sys # Save PEP 3122! if "." in __name__: from . import common else: import common common.exit_cleanly() if len(sys.argv) > 1: arg = sys.argv[1] # We have to do this because python converts arguments to surrogates # http://stackoverflow.com/a/7077803 arg = os.fsencode(arg).decode("utf-8") elif not sys.stdin.isatty(): arg = sys.stdin.buffer.readline() arg = arg.rstrip(b"\r\n") else: arg = "" try: result = function(arg) except Exception as err: import os.path import traceback python = err.__class__.__name__ + ": " + str(err) stack = traceback.extract_tb(err.__traceback__) # limit frame to command module filename = stack[1][0] line_number = "?" for frame in stack: if frame[0] == filename: line_number = frame[1] elif line_number != "?": break where = "(%s:%s)" % (os.path.basename(filename), line_number) result = python + " " + where if result is not None: if not isinstance(result, str): print("Error: expected str, got %s" % type(result)) return result = result.encode("utf-8", "replace") sys.stdout.buffer.write(result + b"\n") sys.stdout.flush()
def pipe(function): # This gives you: # * concise code # * clean exiting # * SAXO_COMMANDS check and possible setup # * input surrogate decoding # * output encoding # * a custom error wrapper # TODO: Would like to run this in caller __name__ == "__main__" # __name__ here is "core" # try import __main__ as main; main.__name__? # Doesn't work. Would probably have to do: # @saxo.command(name=__name__) # Save PEP 3122! if "." in __name__: from . import common else: import common common.exit_cleanly() if not os.environ.get("SAXO_COMMANDS"): os.environ["SAXO_COMMANDS"] = os.path.dirname(sys.argv[0]) if len(sys.argv) > 1: arg = " ".join(sys.argv[1:]) # We have to do this because python converts arguments to surrogates # http://stackoverflow.com/a/7077803 arg = os.fsencode(arg).decode("utf-8") elif not sys.stdin.isatty(): arg = sys.stdin.buffer.readline() arg = arg.rstrip(b"\r\n") arg = arg.decode("utf-8") else: arg = "" try: result = function(arg) except Exception as err: import traceback python = err.__class__.__name__ + ": " + str(err) stack = traceback.extract_tb(err.__traceback__) # limit frame to command module try: filename = stack[1][0] except IndexError: filename = "?" line_number = "?" for frame in stack: if frame[0] == filename: line_number = frame[1] elif line_number != "?": break where = "(%s:%s)" % (os.path.basename(filename), line_number) result = python + " " + where if result is not None: if not isinstance(result, str): print("Error: expected str, got %s" % type(result)) return result = result.encode("utf-8", "replace") sys.stdout.buffer.write(result + b"\n") sys.stdout.flush()