def interactive_client(file, address, cache_size, readonly, repair, startup): if file: storage = FileStorage(file, readonly=readonly, repair=repair) description = file else: socket_address = SocketAddress.new(address) wait_for_server(address=socket_address) storage = ClientStorage(address=socket_address) description = socket_address connection = Connection(storage, cache_size=cache_size) console_module = ModuleType('__console__') sys.modules['__console__'] = console_module namespace = {'connection': connection, 'root': connection.get_root(), 'get': connection.get, 'sys': sys, 'os': os, 'int8_to_str': int8_to_str, 'str_to_int8': str_to_int8, 'pp': pprint} vars(console_module).update(namespace) configure_readline( vars(console_module), os.path.expanduser("~/.durushistory")) console = InteractiveConsole(vars(console_module)) if startup: src = '''with open('{fn}', 'rb') as _: _ = compile(_.read(), '{fn}', 'exec') exec(globals().pop('_')) '''.format(fn = os.path.expanduser(startup)).rstrip() console.runsource(src, '-stub-', 'exec') help = (' connection -> the Connection\n' ' root -> the root instance') console.interact('Durus %s\n%s' % (description, help))
def interactive_client(file, address, cache_size, readonly, repair, startup, storage_class=None): if file: storage = get_storage(file, storage_class=storage_class, readonly=readonly, repair=repair) description = file else: socket_address = SocketAddress.new(address) wait_for_server(address=socket_address) storage = ClientStorage(address=socket_address) description = socket_address connection = Connection(storage, cache_size=cache_size) console_module = ModuleType('__console__') sys.modules['__console__'] = console_module namespace = {'connection': connection, 'root': connection.get_root(), 'get': connection.get, 'sys': sys, 'os': os, 'int8_to_str': int8_to_str, 'str_to_int8': str_to_int8, 'pp': pprint} vars(console_module).update(namespace) configure_readline( vars(console_module), os.path.expanduser("~/.durushistory")) console = InteractiveConsole(vars(console_module)) if startup: console.runsource('execfile("%s")' % os.path.expanduser(startup)) help = (' connection -> the Connection\n' ' root -> the root instance') console.interact('Durus %s\n%s' % (description, help))
def start_durus(logfile, logginglevel, address, storage, gcbytes): if logfile is None: logfile = sys.stderr else: logfile = open(logfile, 'a+') direct_output(logfile) logger.setLevel(logginglevel) socket_address = SocketAddress.new(address) if hasattr(storage, 'get_filename'): log(20, 'Storage file=%s address=%s', storage.get_filename(), socket_address) StorageServer(storage, address=socket_address, gcbytes=gcbytes).serve()
def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT, address=None): self.address = SocketAddress.new(address or (host, port)) self.s = self.address.get_connected_socket() assert self.s, "Could not connect to %s" % self.address self.oid_pool = [] self.oid_pool_size = 32 self.begin() protocol = StorageServer.protocol assert len(protocol) == 4 write_all(self.s, 'V', protocol) server_protocol = read(self.s, 4) if server_protocol != protocol: raise ProtocolError("Protocol version mismatch.")
def stop_durus(address): socket_address = SocketAddress.new(address) sock = socket_address.get_connected_socket() if sock is None: log(20, "Durus server %s doesn't seem to be running." % str(address)) return False write(sock, 'Q') # graceful exit message sock.close() # Try to wait until the address is free. for attempt in range(20): sleep(0.5) sock = socket_address.get_connected_socket() if sock is None: break sock.close() return True
def run_durus_main(): parser = OptionParser() parser.set_description('Run a Durus Server') parser.add_option( '--port', dest='port', default=DEFAULT_PORT, type='int', help='Port to listen on. (default=%s)' % DEFAULT_PORT) parser.add_option( '--file', dest='file', default=None, help=('If not given, the storage is in a new temporary file.')) parser.add_option( '--host', dest='host', default=DEFAULT_HOST, help='Host to listen on. (default=%s)' % DEFAULT_HOST) parser.add_option( '--storage-class', dest='storage', default=None, help='Storage class (e.g. durus.file_storage.FileStorage).') parser.add_option( '--gcbytes', dest='gcbytes', default=DEFAULT_GCBYTES, type='int', help=('Trigger garbage collection after this many commits. (default=%s)' % DEFAULT_GCBYTES)) if hasattr(socket, 'AF_UNIX'): parser.add_option( '--address', dest="address", default=None, help=( "Address of the server.\n" "If given, this is the path to a Unix domain socket for " "the server.")) parser.add_option( '--owner', dest="owner", default=None, help="Owner of the Unix domain socket (the --address value).") parser.add_option( '--group', dest="group", default=None, help="group of the Unix domain socket (the --address value).") parser.add_option( '--umask', dest="umask", default=None, type="int", help="umask for the Unix domain socket (the --address value).") logginglevel = logger.getEffectiveLevel() parser.add_option( '--logginglevel', dest='logginglevel', default=logginglevel, type='int', help=('Logging level. Lower positive numbers log more. (default=%s)' % logginglevel)) parser.add_option( '--logfile', dest='logfile', default=None, help=('Log file. (default=stderr)')) parser.add_option( '--repair', dest='repair', action='store_true', help=('Repair the filestorage by truncating to remove anything ' 'that is malformed. Without this option, errors ' 'will cause the program to report and terminate without ' 'attempting any repair.')) parser.add_option( '--readonly', dest='readonly', action='store_true', help='Open the file in read-only mode.') parser.add_option( '--stop', dest='stop', action='store_true', help='Instead of starting the server, try to stop a running one.') (options, args) = parser.parse_args() if getattr(options, 'address', None) is None: address = SocketAddress.new((options.host, options.port)) else: address = SocketAddress.new(address=options.address, owner=options.owner, group=options.group, umask=options.umask) if not options.stop: storage = get_storage(options.file, storage_class=options.storage, repair=options.repair, readonly=options.readonly) start_durus(options.logfile, options.logginglevel, address, storage, options.gcbytes) else: stop_durus(address)
def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT): self.address = SocketAddress.new((host, port)) self.socket = self.address.get_connected_socket() assert self.socket, 'Could not connect to %s' % (self.address) if self.server_protocol() != PROTOCOL: raise ProtocolError("Protocol version mismatch.")