X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=pdm%2Fcli.py;h=7cfa2635fd5d60a369f44bd9bf764fa14a8f8b5c;hb=3e02e6b54c766318d60405e16c556d8fcaff4143;hp=81246b7fd439fed65bbb9ca8f2adf0b42f3a0786;hpb=73c1ef8bb613a660f37fbe0cfe50b74fc5925229;p=pdm.git diff --git a/pdm/cli.py b/pdm/cli.py index 81246b7..7cfa263 100644 --- a/pdm/cli.py +++ b/pdm/cli.py @@ -1,7 +1,9 @@ -"""Management for daemon processes +"""Python Daemon Management -- Client functions -This module provides some client support for the daemon management -provided in the pdm.srv module. +This module implements the client part of the PDM protocols. The +primary objects of interest are the replclient and perfclient classes, +which implement support for their respective protocols. See their +documentation for details. """ import socket, pickle, struct, select, threading @@ -17,16 +19,21 @@ def resolve(spec): return spec sk = None try: - if "/" in spec: + if ":" in spec: + p = spec.rindex(":") + first, second = spec[:p], spec[p + 1:] + if "/" in second: + from . import sshsock + sk = sshsock.sshsocket(first, second) + else: + sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sk.connect((first, second)) + elif "/" in spec: sk = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sk.connect(spec) elif spec.isdigit(): sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sk.connect(("localhost", int(spec))) - elif ":" in spec: - sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - p = spec.rindex(":") - sk.connect((spec[:p], int(spec[p + 1:]))) else: raise Exception("Unknown target specification %r" % spec) rv = sk @@ -49,17 +56,17 @@ class client(object): """Create a client object connected to the specified server. `sk' can either be a socket object, which is used as it is, or a string specification very similar to the - specification for pdm.srv.listen, so see its documentation for - details. The differences are only that this function does not - take arguments specific to socket creation, like the mode and - group arguments for Unix sockets. If `proto' is given, that - subprotocol will negotiated with the server (by calling the - select() method). + specification for L{pdm.srv.listen}, so see its documentation + for details. The differences are only that this function does + not take arguments specific to socket creation, like the mode + and group arguments for Unix sockets. If `proto' is given, + that subprotocol will negotiated with the server (by calling + the select() method). """ self.sk = resolve(sk) - self.buf = "" + self.buf = b"" line = self.readline() - if line != "+PDM1": + if line != b"+PDM1": raise protoerr("Illegal protocol signature") if proto is not None: self.select(proto) @@ -75,23 +82,25 @@ class client(object): def readline(self): """Read a single NL-terminated line and return it.""" while True: - p = self.buf.find("\n") + p = self.buf.find(b"\n") if p >= 0: ret = self.buf[:p] self.buf = self.buf[p + 1:] return ret ret = self.sk.recv(1024) - if ret == "": + if ret == b"": return None self.buf += ret def select(self, proto): """Negotiate the given subprotocol with the server""" - if "\n" in proto: + if isinstance(proto, str): + proto = proto.encode("ascii") + if b"\n" in proto: raise Exception("Illegal protocol specified: %r" % proto) - self.sk.send(proto + "\n") + self.sk.send(proto + b"\n") rep = self.readline() - if len(rep) < 1 or rep[0] != "+": + if len(rep) < 1 or rep[0] != b"+"[0]: raise protoerr("Error reply when selecting protocol %s: %s" % (proto, rep[1:])) def __enter__(self): @@ -104,17 +113,17 @@ class client(object): class replclient(client): """REPL protocol client - Implements the client side of the REPL protocol; see pdm.srv.repl - for details on the protocol and its functionality. + Implements the client side of the REPL protocol; see + L{pdm.srv.repl} for details on the protocol and its functionality. """ def __init__(self, sk): """Create a connected client as documented in the `client' class.""" - super(replclient, self).__init__(sk, "repl") + super().__init__(sk, "repl") def run(self, code): """Run a single block of Python code on the server. Returns - the output of the command (as documented in pdm.srv.repl) as a - string. + the output of the command (as documented in L{pdm.srv.repl}) + as a string. """ while True: ncode = code.replace("\n\n", "\n") @@ -122,16 +131,16 @@ class replclient(client): code = ncode while len(code) > 0 and code[-1] == "\n": code = code[:-1] - self.sk.send(code + "\n\n") - buf = "" + self.sk.send((code + "\n\n").encode("utf-8")) + buf = b"" while True: ln = self.readline() - if ln[0] == " ": - buf += ln[1:] + "\n" - elif ln[0] == "+": - return buf - elif ln[0] == "-": - raise protoerr("Error reply: %s" % ln[1:]) + if ln[0] == b" "[0]: + buf += ln[1:] + b"\n" + elif ln[0] == b"+"[0]: + return buf.decode("utf-8") + elif ln[0] == b"-"[0]: + raise protoerr("Error reply: %s" % ln[1:].decode("utf-8")) else: raise protoerr("Illegal reply: %s" % ln) @@ -205,8 +214,8 @@ class perfproxy(object): class perfclient(client): """PERF protocol client - Implements the client side of the PERF protocol; see pdm.srv.perf - for details on the protocol and its functionality. + Implements the client side of the PERF protocol; see + L{pdm.srv.perf} for details on the protocol and its functionality. This client class implements functions for finding PERF objects on the server, and returns, for each server-side object looked up, a @@ -216,12 +225,12 @@ class perfclient(client): they implement a close() method for that purpose, and can also be used in `with' statements. - See pdm.srv.perf for details on the various PERF interfaces that - the proxy objects might implement. + See L{pdm.srv.perf} for details on the various PERF interfaces + that the proxy objects might implement. """ def __init__(self, sk): """Create a connected client as documented in the `client' class.""" - super(perfclient, self).__init__(sk, "perf") + super().__init__(sk, "perf") self.nextid = 0 self.lock = threading.Lock() self.proxies = {} @@ -233,10 +242,10 @@ class perfclient(client): self.sk.send(buf) def recvb(self, num): - buf = "" + buf = b"" while len(buf) < num: data = self.sk.recv(num - len(buf)) - if data == "": + if data == b"": raise EOFError() buf += data return buf @@ -313,6 +322,11 @@ class perfclient(client): of the slash. For instance, find("pdm.perf.sysres/cputime") will return the built-in attribute for reading the CPU time used by the server process. + + The proxy objects returned by this function are cached and the + same object is returned the next time the same name is + requested, which means that they are kept live until the + client connection is closed. """ ret = self.names.get(name) if ret is None: