From: Fredrik Tolf Date: Wed, 23 Apr 2014 01:44:44 +0000 (+0200) Subject: Create explicit python2 branch as a child of python3. X-Git-Url: http://dolda2000.com/gitweb/?p=pdm.git;a=commitdiff_plain;h=9e3c83b113c519d0073457ae0cfe80291bcda7e9;hp=86b3a0acd5fd18f7816ae91e2839ead5dfab0e33 Create explicit python2 branch as a child of python3. --- diff --git a/pdm-repl b/pdm-repl index cc5c289..992df4f 100755 --- a/pdm-repl +++ b/pdm-repl @@ -1,4 +1,4 @@ -#!/usr/bin/python3 +#!/usr/bin/python import sys, getopt, readline import pdm.cli @@ -24,9 +24,9 @@ buf = "" while True: try: if buf != "": - line = input(" ") + line = raw_input(" ") else: - line = input("% ") + line = raw_input("% ") except EOFError: break if line == "": diff --git a/pdm/cli.py b/pdm/cli.py index 667e2ed..1d83920 100644 --- a/pdm/cli.py +++ b/pdm/cli.py @@ -23,7 +23,7 @@ def resolve(spec): p = spec.rindex(":") first, second = spec[:p], spec[p + 1:] if "/" in second: - from . import sshsock + import sshsock sk = sshsock.sshsocket(first, second) else: sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM) @@ -64,9 +64,9 @@ class client(object): the select() method). """ self.sk = resolve(sk) - self.buf = b"" + self.buf = "" line = self.readline() - if line != b"+PDM1": + if line != "+PDM1": raise protoerr("Illegal protocol signature") if proto is not None: self.select(proto) @@ -82,25 +82,23 @@ class client(object): def readline(self): """Read a single NL-terminated line and return it.""" while True: - p = self.buf.find(b"\n") + p = self.buf.find("\n") if p >= 0: ret = self.buf[:p] self.buf = self.buf[p + 1:] return ret ret = self.sk.recv(1024) - if ret == b"": + if ret == "": return None self.buf += ret def select(self, proto): """Negotiate the given subprotocol with the server""" - if isinstance(proto, str): - proto = proto.encode("ascii") - if b"\n" in proto: + if "\n" in proto: raise Exception("Illegal protocol specified: %r" % proto) - self.sk.send(proto + b"\n") + self.sk.send(proto + "\n") rep = self.readline() - if len(rep) < 1 or rep[0] != b"+"[0]: + if len(rep) < 1 or rep[0] != "+": raise protoerr("Error reply when selecting protocol %s: %s" % (proto, rep[1:])) def __enter__(self): @@ -118,7 +116,7 @@ class replclient(client): """ def __init__(self, sk): """Create a connected client as documented in the `client' class.""" - super().__init__(sk, "repl") + super(replclient, self).__init__(sk, "repl") def run(self, code): """Run a single block of Python code on the server. Returns @@ -131,16 +129,16 @@ class replclient(client): code = ncode while len(code) > 0 and code[-1] == "\n": code = code[:-1] - self.sk.send((code + "\n\n").encode("utf-8")) - buf = b"" + self.sk.send(code + "\n\n") + buf = "" while True: ln = self.readline() - 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")) + if ln[0] == " ": + buf += ln[1:] + "\n" + elif ln[0] == "+": + return buf + elif ln[0] == "-": + raise protoerr("Error reply: %s" % ln[1:]) else: raise protoerr("Illegal reply: %s" % ln) @@ -230,7 +228,7 @@ class perfclient(client): """ def __init__(self, sk): """Create a connected client as documented in the `client' class.""" - super().__init__(sk, "perf") + super(perfclient, self).__init__(sk, "perf") self.nextid = 0 self.lock = threading.Lock() self.proxies = {} @@ -242,10 +240,10 @@ class perfclient(client): self.sk.send(buf) def recvb(self, num): - buf = b"" + buf = "" while len(buf) < num: data = self.sk.recv(num - len(buf)) - if data == b"": + if data == "": raise EOFError() buf += data return buf diff --git a/pdm/perf.py b/pdm/perf.py index fd4daa6..f0e6c89 100644 --- a/pdm/perf.py +++ b/pdm/perf.py @@ -63,7 +63,7 @@ class attrinfo(object): class perfobj(object): def __init__(self, *args, **kwargs): - super().__init__() + super(perfobj, self).__init__() def pdm_protocols(self): return [] @@ -74,7 +74,7 @@ class simpleattr(perfobj): read. """ def __init__(self, func, info = None, *args, **kwargs): - super().__init__(*args, **kwargs) + super(simpleattr, self).__init__(*args, **kwargs) self.func = func if info is None: info = attrinfo() @@ -87,7 +87,7 @@ class simpleattr(perfobj): return self.info def pdm_protocols(self): - return super().pdm_protocols() + ["attr"] + return super(simpleattr, self).pdm_protocols() + ["attr"] class valueattr(perfobj): """An implementation of the `attr' interface, which is initialized @@ -95,7 +95,7 @@ class valueattr(perfobj): updates to the value are reflected in subsequent reads. """ def __init__(self, init, info = None, *args, **kwargs): - super().__init__(*args, **kwargs) + super(valueattr, self).__init__(*args, **kwargs) self.value = init if info is None: info = attrinfo() @@ -108,7 +108,7 @@ class valueattr(perfobj): return self.info def pdm_protocols(self): - return super().pdm_protocols() + ["attr"] + return super(valueattr, self).pdm_protocols() + ["attr"] class eventobj(perfobj): """An implementation of the `event' interface. It keeps track of @@ -116,7 +116,7 @@ class eventobj(perfobj): subscribers when submitted with the `notify' method. """ def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) + super(eventobj, self).__init__(*args, **kwargs) self.subscribers = set() def subscribe(self, cb): @@ -135,7 +135,7 @@ class eventobj(perfobj): except: pass def pdm_protocols(self): - return super().pdm_protocols() + ["event"] + return super(eventobj, self).pdm_protocols() + ["event"] class staticdir(perfobj): """An implementation of the `dir' interface. Put other PERF @@ -143,7 +143,7 @@ class staticdir(perfobj): return them to requesting clients. """ def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) + super(staticdir, self).__init__(*args, **kwargs) self.map = {} def __setitem__(self, name, ob): @@ -159,13 +159,13 @@ class staticdir(perfobj): return self.map.get(name, default) def listdir(self): - return list(self.map.keys()) + return self.map.keys() def lookup(self, name): return self.map[name] def pdm_protocols(self): - return super().pdm_protocols() + ["dir"] + return super(staticdir, self).pdm_protocols() + ["dir"] class simplefunc(perfobj): """An implementation of the `invoke' interface. Put callables in @@ -173,7 +173,7 @@ class simplefunc(perfobj): when invoked with the corresponding method name. Additionally, it updates itself with any keyword-arguments it is initialized with.""" def __init__(self, *args, **kwargs): - super().__init__(*args) + super(simplefunc, self).__init__(*args) self.map = {} self.map.update(kwargs) @@ -189,7 +189,7 @@ class simplefunc(perfobj): self.map[method](*args, **kwargs) def pdm_protocols(self): - return super().pdm_protocols() + ["invoke"] + return super(simplefunc, self).pdm_protocols() + ["invoke"] class event(object): """This class should be subclassed by all event objects sent via @@ -233,7 +233,7 @@ class procevent(event): `finishevent' emitted when the connection is closed. """ def __init__(self, id): - super().__init__() + super(procevent, self).__init__() if isinstance(id, procevent): self.id = id.id else: @@ -242,7 +242,7 @@ class procevent(event): class startevent(procevent): """A subclass of `procevent'. See its documentation for details.""" def __init__(self): - super().__init__(getprocid()) + super(startevent, self).__init__(getprocid()) class finishevent(procevent): """A subclass of `procevent'. Intended to be emitted when a @@ -251,7 +251,7 @@ class finishevent(procevent): distinction is meaningful. The `start' parameter should be the `startevent' instance used when the process was initiated.""" def __init__(self, start, aborted = False): - super().__init__(start) + super(finishevent, self).__init__(start) self.aborted = aborted sysres = staticdir() diff --git a/pdm/srv.py b/pdm/srv.py index 80f9a26..b2c131a 100644 --- a/pdm/srv.py +++ b/pdm/srv.py @@ -40,36 +40,35 @@ class repl(object): self.mod = types.ModuleType("repl") self.mod.echo = self.echo self.printer = pprint.PrettyPrinter(indent = 4, depth = 6) - cl.send(b"+REPL\n") + cl.send("+REPL\n") def sendlines(self, text): for line in text.split("\n"): - self.cl.send(b" " + line.encode("utf-8") + b"\n") + self.cl.send(" " + line + "\n") def echo(self, ob): self.sendlines(self.printer.pformat(ob)) def command(self, cmd): - cmd = cmd.decode("utf-8") try: try: ccode = compile(cmd, "PDM Input", "eval") except SyntaxError: ccode = compile(cmd, "PDM Input", "exec") - exec(ccode, self.mod.__dict__) - self.cl.send(b"+OK\n") + exec ccode in self.mod.__dict__ + self.cl.send("+OK\n") else: self.echo(eval(ccode, self.mod.__dict__)) - self.cl.send(b"+OK\n") + self.cl.send("+OK\n") except: lines = ("".join(traceback.format_exception(*sys.exc_info()))).split("\n") while len(lines) > 0 and lines[-1] == "": lines = lines[:-1] for line in lines: - self.cl.send(b" " + line.encode("utf-8") + b"\n") - self.cl.send(b"+EXC\n") + self.cl.send(" " + line + "\n") + self.cl.send("+EXC\n") def handle(self, buf): - p = buf.find(b"\n\n") + p = buf.find("\n\n") if p < 0: return buf cmd = buf[:p + 1] @@ -172,13 +171,13 @@ class perf(object): def __init__(self, cl): self.cl = cl self.odtab = {} - cl.send(b"+PERF1\n") + cl.send("+PERF1\n") self.buf = "" self.lock = threading.Lock() self.subscribed = {} def closed(self): - for id, recv in self.subscribed.items(): + for id, recv in self.subscribed.iteritems(): ob = self.odtab[id] if ob is None: continue ob, protos = ob @@ -200,7 +199,7 @@ class perf(object): raise ValueError("Object does not support PDM introspection") try: proto = ob.pdm_protocols() - except Exception as exc: + except Exception, exc: raise ValueError("PDM introspection failed", exc) self.odtab[id] = ob, proto return proto @@ -217,7 +216,7 @@ class perf(object): return try: proto = self.bindob(id, ob) - except Exception as exc: + except Exception, exc: self.send("-", exc) return self.send("+", proto) @@ -239,12 +238,12 @@ class perf(object): return try: ob = src.lookup(obnm) - except KeyError as exc: + except KeyError, exc: self.send("-", exc) return try: proto = self.bindob(tgtid, ob) - except Exception as exc: + except Exception, exc: self.send("-", exc) return self.send("+", proto) @@ -274,7 +273,7 @@ class perf(object): return try: ret = ob.readattr() - except Exception as exc: + except Exception, exc: self.send("-", Exception("Could not read attribute")) return self.send("+", ret) @@ -291,7 +290,7 @@ class perf(object): return try: self.send("+", ob.invoke(method, *args, **kwargs)) - except Exception as exc: + except Exception, exc: self.send("-", exc) def event(self, id, ob, ev): @@ -357,7 +356,7 @@ protocols["perf"] = perf class client(threading.Thread): def __init__(self, sk): - super().__init__(name = "Management client") + super(client, self).__init__(name = "Management client") self.setDaemon(True) self.sk = sk self.handler = self @@ -366,10 +365,6 @@ class client(threading.Thread): return self.sk.send(data) def choose(self, proto): - try: - proto = proto.decode("ascii") - except UnicodeError: - proto = None if proto in protocols: self.handler = protocols[proto](self) else: @@ -377,7 +372,7 @@ class client(threading.Thread): raise Exception() def handle(self, buf): - p = buf.find(b"\n") + p = buf.find("\n") if p >= 0: proto = buf[:p] buf = buf[p + 1:] @@ -386,24 +381,24 @@ class client(threading.Thread): def run(self): try: - buf = b"" - self.send(b"+PDM1\n") + buf = "" + self.send("+PDM1\n") while True: ret = self.sk.recv(1024) - if ret == b"": + if ret == "": return buf += ret while True: try: nbuf = self.handler.handle(buf) except: - #for line in traceback.format_exception(*sys.exc_info()): - # print(line) return if nbuf == buf: break buf = nbuf finally: + #for line in traceback.format_exception(*sys.exc_info()): + # print line try: self.sk.close() finally: @@ -420,7 +415,7 @@ class listener(threading.Thread): tcplistener. """ def __init__(self): - super().__init__(name = "Management listener") + super(listener, self).__init__(name = "Management listener") self.setDaemon(True) def listen(self, sk): @@ -448,14 +443,14 @@ class listener(threading.Thread): class unixlistener(listener): """Unix socket listener""" - def __init__(self, name, mode = 0o600, group = None): + def __init__(self, name, mode = 0600, group = None): """Create a listener that will bind to the Unix socket named by `name'. The socket will not actually be bound until the listener is started. The socket will be chmodded to `mode', and if `group' is given, the named group will be set as the owner of the socket. """ - super().__init__() + super(unixlistener, self).__init__() self.name = name self.mode = mode self.group = group @@ -485,7 +480,7 @@ class tcplistener(listener): the given local interface. The socket will not actually be bound until the listener is started. """ - super().__init__() + super(tcplistener, self).__init__() self.port = port self.bindaddr = bindaddr @@ -523,7 +518,7 @@ def listen(spec): last = spec if "/" in first: parts = spec.split(":") - mode = 0o600 + mode = 0600 group = None if len(parts) > 1: mode = int(parts[1], 8) diff --git a/pdm/sshsock.py b/pdm/sshsock.py index 7ee26e8..dd53e6a 100644 --- a/pdm/sshsock.py +++ b/pdm/sshsock.py @@ -9,31 +9,31 @@ class sshsocket(object): if port is not None: args += ["-p", str(int(port))] args += [host] - args += ["python3", "-m", "pdm.sshsock", path] + args += ["python", "-m", "pdm.sshsock", path] self.proc = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=True) fcntl.fcntl(self.proc.stdout, fcntl.F_SETFL, fcntl.fcntl(self.proc.stdout, fcntl.F_GETFL) | os.O_NONBLOCK) head = self.recv(5) - if head != b"SSOCK": + if head != "SSOCK": raise socket.error("unexpected reply from %s: %r" % (host, head)) head = self.recv(1) - if head == b"+": - buf = b"" + if head == "+": + buf = "" while True: r = self.recv(1) - if r == b"": + if r == "": raise socket.error("unexpected EOF in SSH socket stream") - elif r == b"\n": + elif r == "\n": break buf += r return - elif head == b"-": - buf = b"" + elif head == "-": + buf = "" while True: r = self.recv(1) - if r in {b"\n", b""}: + if r in ("\n", ""): break buf += r - raise socket.error(buf.decode("utf-8")) + raise socket.error(buf) else: raise socket.error("unexpected reply from %s: %r" % (host, head)) @@ -60,7 +60,7 @@ class sshsocket(object): self.close() def cli(): - fcntl.fcntl(sys.stdin.buffer, fcntl.F_SETFL, fcntl.fcntl(sys.stdin.buffer, fcntl.F_GETFL) | os.O_NONBLOCK) + fcntl.fcntl(sys.stdin, fcntl.F_SETFL, fcntl.fcntl(sys.stdin, fcntl.F_GETFL) | os.O_NONBLOCK) sk = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: try: @@ -71,32 +71,32 @@ def cli(): return sys.stdout.write("SSOCK+\n") sys.stdout.flush() - buf1 = b"" - buf2 = b"" + buf1 = "" + buf2 = "" while True: wfd = [] if buf1: wfd.append(sk) - if buf2: wfd.append(sys.stdout.buffer) - rfd, wfd, efd = select.select([sk, sys.stdin.buffer], wfd, []) + if buf2: wfd.append(sys.stdout) + rfd, wfd, efd = select.select([sk, sys.stdin], wfd, []) if sk in rfd: ret = sk.recv(65536) - if ret == b"": + if ret == "": break else: buf2 += ret - if sys.stdin.buffer in rfd: - ret = sys.stdin.buffer.read() - if ret == b"": + if sys.stdin in rfd: + ret = sys.stdin.read() + if ret == "": break else: buf1 = ret if sk in wfd: ret = sk.send(buf1) buf1 = buf1[ret:] - if sys.stdout.buffer in wfd: - sys.stdout.buffer.write(buf2) - sys.stdout.buffer.flush() - buf2 = b"" + if sys.stdout in wfd: + sys.stdout.write(buf2) + sys.stdout.flush() + buf2 = "" finally: sk.close() diff --git a/pdm/util.py b/pdm/util.py index 38b0ba0..53cc82a 100644 --- a/pdm/util.py +++ b/pdm/util.py @@ -8,9 +8,9 @@ import sys, traceback, threading def threads(): "Returns a dict of currently known threads, mapped to their respective frames." - tt = {th.ident: th for th in threading.enumerate()} - return {tt.get(key, key): val for key, val in sys._current_frames().items()} + tt = dict((th.ident, th) for th in threading.enumerate()) + return dict((tt.get(key, key), val) for key, val in sys._current_frames().iteritems()) def traces(): "Returns the value of threads() with each frame expanded to a backtrace." - return {th: traceback.extract_stack(frame) for th, frame in threads().items()} + return dict((th, traceback.extract_stack(frame)) for th, frame in threads().iteritems()) diff --git a/setup.py b/setup.py index 96a5d22..adc974e 100755 --- a/setup.py +++ b/setup.py @@ -1,8 +1,8 @@ -#!/usr/bin/python3 +#!/usr/bin/python from distutils.core import setup -setup(name = "pdm3", +setup(name = "pdm", version = "0.3", description = "Python daemon management library", author = "Fredrik Tolf",