This instance will be upgraded to Heptapod 0.26.0rc1 on 2021-10-25 at 14:00 UTC+2 (a few minutes of down time)

Commit 6bdafe92 authored by Holger Krekel's avatar Holger Krekel
Browse files

py becomes and external ...

parent 05ce33bd409c
from initpkg import initpkg
initpkg(__name__, exportdefs = {
'path.local': './path/local/local.LocalPath',
'path.checker': './path/common.checker',
'path.svnurl': './path/svn/urlcommand.SvnCommandPath',
'path.svnwc': './path/svn/wccommand.SvnWCCommandPath',
'path.extpy': './path/extpy/extpy.Extpy',
'path.NotFound': './path/error.FileNotFound',
'path.Denied': './path/error.PermissionDenied',
'path.NoDirectory': './path/error.NoDirectory',
'path.Invalid': './path/error.Invalid',
'test.collect.Collector': './test/collect.Collector',
'test.collect.Directory': './test/collect.Directory',
'test.collect.Module': './test/collect.Module',
'test.collect.PyCollector':'./test/collect.PyCollector',
'test.collect.Error': './test/collect.Error',
'test.run': './test/run',
'test.main': './test/cmdline.main',
'test.raises': './test/raises.raises',
'test.config': './test/config.config',
'test.compat.TestCase': './test/compat.TestCase',
'test.Item': './test/run.Item',
'test.Option': './test/tool/optparse.Option',
'test.TextReporter': './test/report/text/reporter.TextReporter',
'test.MemoReporter': './test/report/memo.MemoReporter',
'process.cmdexec': './process/cmdexec.cmdexec',
'execnet.PopenGateway': './execnet/register.PopenGateway',
'execnet.SocketGateway': './execnet/register.SocketGateway',
#'execnet.SSHGateway' : './execnet/register.SSHGateway',
'magic.View': './magic/viewtype.View',
'magic.autopath': './magic/autopath.autopath',
'magic.invoke': './magic/invoke.invoke',
'magic.revoke': './magic/invoke.revoke',
'magic.AssertionError': './magic/assertion.AssertionError',
'magic.patch': './magic/patch.patch',
'magic.revert': './magic/patch.revert',
'magic.dyncode.compile': './magic/dyncode.compile',
'magic.dyncode.compile2': './magic/dyncode.compile2',
'magic.dyncode.getsource': './magic/dyncode.getsource',
'magic.dyncode.tbinfo': './magic/dyncode.tbinfo',
'magic.dyncode.listtb': './magic/dyncode.listtb',
'magic.dyncode.findsource': './magic/dyncode.findsource',
'magic.dyncode.getline': './magic/dyncode.getline',
'magic.dyncode.getlines': './magic/dyncode.getlines',
})
#
# find and import a version of 'py'
#
import sys
import os
from os.path import dirname as opd, exists, join, basename, abspath
def searchpy(current):
while 1:
last = current
initpy = join(current, '__init__.py')
if not exists(initpy):
pydir = join(current, 'py')
# recognize py-package and ensure it is importable
if exists(pydir) and exists(join(pydir, '__init__.py')):
for p in sys.path:
if p == current:
return True
print "inserting into sys.path:", current
sys.path.insert(0, current)
return True
current = opd(current)
if last == current:
return False
if not searchpy(abspath(os.curdir)):
if not searchpy(opd(abspath(sys.argv[0]))):
if not searchpy(opd(__file__)):
raise SystemExit, "Could not find 'py' package!"
import py
#!/usr/bin/python
from __future__ import generators
import sys, os
from py.path import local
def search_path(item):
dirs = [os.curdir]
PYTHONPATH = os.getenv('PYTHONPATH')
dirs.extend(PYTHONPATH.split(':'))
dirs.extend(sys.path)
results = {}
for fn in dirs:
p = local(fn)
# XXX zip files
pyfile = p.join(item).new(ext='.py')
if pyfile.check(file=1) and pyfile not in results:
results[pyfile] = 1
yield pyfile
pydir = p.join(item)
if pydir.check(dir=1) and pydir.join('__init__.py').check() and pydir not in results:
results[pydir] = 1
yield pydir
if __name__ == '__main__':
item = sys.argv[1]
for x in search_path(item):
print x
#!/usr/bin/env python
from _findpy import py
import sys
py.test.main(sys.argv)
#!/usr/bin/python
import py, sys
import bike
import rlcompleter2
rlcompleter2.setup()
if __name__ == '__main__':
source = py.path.local(sys.argv[1])
assert source.check(dir=1)
#dest = std.path.local(sys.argv[2])
#dest.ensure(dir=1)
import bike
ctx = bike.init().brmctx
for x in source.visit(py.path.checker(file=1, fnmatch='*.py'),
py.path.checker(dotfile=0)):
ctx.paths.append(str(x))
#!/usr/bin/python
import sys, os
progpath = sys.argv[0]
packagedir = os.path.abspath(os.path.dirname(progpath))
packagename = os.path.basename(packagedir)
bindir = os.path.join(packagedir, 'bin')
rootdir = os.path.dirname(packagedir)
tooldir = os.path.join(rootdir, 'tool')
def prepend_unixpath(VAR, strpath):
value = "%r:$%s" % (strpath, VAR)
shell = os.getenv('SHELL')
if shell and shell.endswith('csh'):
return "setenv %s %s" % (VAR, value)
else:
return "%s=%s; export %s" % (VAR, value, VAR)
if sys.platform != 'win32':
print prepend_unixpath('PATH', bindir)
print prepend_unixpath('PATH', tooldir)
print prepend_unixpath('PYTHONPATH', rootdir)
"""
send a "quit" signal to a remote server
"""
import sys
import socket
hostport = sys.argv[1]
host, port = hostport.split(':')
hostport = (host, int(port))
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(hostport)
sock.sendall('"raise KeyboardInterrupt"\n')
"""
a remote python shell
for injection into startserver.py
"""
import sys, os, socket, select
try:
clientsock
except NameError:
print "client side starting"
import sys
host, port = sys.argv[1].split(':')
port = int(port)
myself = open(os.path.abspath(sys.argv[0])).read()
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
sock.sendall(repr(myself)+'\n')
print "send boot string"
inputlist = [ sock, sys.stdin ]
try:
while 1:
r,w,e = select.select(inputlist, [], [])
if sys.stdin in r:
line = raw_input()
sock.sendall(line + '\n')
if sock in r:
line = sock.recv(4096)
sys.stdout.write(line)
sys.stdout.flush()
except:
import traceback
print traceback.print_exc()
sys.exit(1)
print "server side starting"
# server side
#
from traceback import print_exc
from threading import Thread
class promptagent(Thread):
def __init__(self, clientsock):
Thread.__init__(self)
self.clientsock = clientsock
def run(self):
print "Entering thread prompt loop"
clientfile = self.clientsock.makefile('w')
filein = self.clientsock.makefile('r')
loc = self.clientsock.getsockname()
while 1:
try:
clientfile.write('%s %s >>> ' % loc)
clientfile.flush()
line = filein.readline()
if len(line)==0: raise EOFError,"nothing"
#print >>sys.stderr,"got line: " + line
if line.strip():
oldout, olderr = sys.stdout, sys.stderr
sys.stdout, sys.stderr = clientfile, clientfile
try:
try:
exec compile(line + '\n','<remote pyin>', 'single')
except:
print_exc()
finally:
sys.stdout=oldout
sys.stderr=olderr
clientfile.flush()
except EOFError,e:
print >>sys.stderr, "connection close, prompt thread returns"
break
#print >>sys.stdout, "".join(apply(format_exception,sys.exc_info()))
self.clientsock.close()
prompter = promptagent(clientsock)
prompter.start()
print "promptagent - thread started"
#! /usr/bin/env python
"""
start socket based minimal readline exec server
"""
# this part of the program only executes on the server side
#
progname = 'socket_readline_exec_server-1.2'
debug = 0
import sys, socket, os
if debug: # and not os.isatty(sys.stdin.fileno()):
f = open('/tmp/execnet-socket-pyout.log', 'a', 0)
old = sys.stdout, sys.stderr
sys.stdout = sys.stderr = f
def execloop(serversock):
while 1:
try:
print progname, 'Entering Accept loop', serversock.getsockname()
clientsock,address = serversock.accept()
print progname, 'got new connection from %s %s' % address
clientfile = clientsock.makefile('r+b',0)
print "reading line"
source = clientfile.readline()
clientfile.close()
g = {'clientsock' : clientsock, 'address' : address}
source = eval(source)
if not source:
break
co = compile(source+'\n', source, 'exec')
print progname, 'compiled source, executing'
try:
exec co in g
finally:
print progname, 'finished executing code'
except (SystemExit, KeyboardInterrupt):
break
#except:
# import traceback
# traceback.print_exc()
def bind_and_listen(hostport):
if isinstance(hostport, str):
host, port = hostport.split(':')
hostport = (host, int(port))
serversock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversock.bind(hostport)
serversock.listen(5)
return serversock
def startserver(serversock):
try:
execloop(serversock)
finally:
print "leaving socketserver execloop"
serversock.shutdown(2)
if __name__ == '__main__':
import sys
if len(sys.argv)>1:
hostport = sys.argv[1]
else:
hostport = ':8888'
serversock = bind_and_listen(hostport)
startserver(serversock)
import rlcompleter2
rlcompleter2.setup()
import register, sys
try:
hostport = sys.argv[1]
except:
hostport = ':8888'
gw = register.ServerGateway(hostport)
import sys, os, threading, struct, Queue, traceback
import atexit
# XXX the following line should not be here
from py.__impl__.execnet.source import Source
debug = 0
sysex = (KeyboardInterrupt, SystemExit)
class RemoteError(Exception):
""" Contains an Exceptions from the other side. """
def __init__(self, formatted):
Exception.__init__(self)
self.formatted = formatted
def __str__(self):
return self.formatted
def __repr__(self):
return "%s: %s" %(self.__class__.__name__, self.formatted)
class Gateway(object):
num_worker_threads = 2
RemoteError = RemoteError
def __init__(self, io, startcount=2):
self.io = io
self._execqueue = Queue.Queue()
self._outgoing = Queue.Queue()
self.channelfactory = ChannelFactory(self, startcount)
self.iothreads = [
threading.Thread(target=self.thread_receiver, name='receiver'),
threading.Thread(target=self.thread_sender, name='sender'),
]
self.workerthreads = w = []
for x in range(self.num_worker_threads):
w.append(threading.Thread(target=self.thread_executor,
name='executor %d' % x))
for x in self.iothreads + w:
x.start()
if not _gateways:
atexit.register(cleanup_atexit)
_gateways.append(self)
def _stopexec(self):
if self.workerthreads:
for x in self.workerthreads:
self._execqueue.put(None)
for x in self.workerthreads:
if x.isAlive():
self.trace("joining %r" % x)
x.join()
self.workerthreads[:] = []
def exit(self):
if self.workerthreads:
self._stopexec()
self._outgoing.put(Message.EXIT_GATEWAY())
else:
self.trace("exit() called, but gateway has not threads anymore!")
def join(self):
current = threading.currentThread()
for x in self.iothreads:
if x != current and x.isAlive():
print "joining", x
x.join()
def trace(self, *args):
if debug:
try:
l = "\n".join(args).split(os.linesep)
id = getid(self)
for x in l:
print id, x
except sysex:
raise
except:
import traceback
traceback.print_exc()
def traceex(self, excinfo):
l = traceback.format_exception(*excinfo)
errortext = "".join(l)
self.trace(errortext)
def thread_receiver(self):
""" thread to read and handle Messages half-sync-half-async. """
try:
while 1:
try:
msg = Message.readfrom(self.io)
self.trace("received <- %r" % msg)
msg.received(self)
except sysex:
raise
except:
self.traceex(sys.exc_info())
break
finally:
self.trace('leaving %r' % threading.currentThread())
def thread_sender(self):
""" thread to send Messages over the wire. """
try:
while 1:
msg = self._outgoing.get()
try:
msg.writeto(self.io)
except:
excinfo = sys.exc_info()
self.traceex(excinfo)
msg.post_sent(self, excinfo)
raise
else:
self.trace('sent -> %r' % msg)
msg.post_sent(self)
finally:
self.trace('leaving %r' % threading.currentThread())
def thread_executor(self):
""" worker thread to execute source objects from the execution queue. """
try:
while 1:
task = self._execqueue.get()
if task is None:
break
channel, source = task
try:
loc = { 'channel' : channel }
self.trace("execution starts:", repr(source)[:50])
try:
co = compile(source+'\n', '', 'exec')
exec co in loc
finally:
self.trace("execution finished:", repr(source)[:50])
except (KeyboardInterrupt, SystemExit):
raise
except:
excinfo = sys.exc_info()
l = traceback.format_exception(*excinfo)
errortext = "".join(l)
self._outgoing.put(Message.CHANNEL_CLOSE_ERROR(channel.id, errortext))
self.trace(errortext)
else:
self._outgoing.put(Message.CHANNEL_CLOSE(channel.id))
finally:
self.trace('leaving %r' % threading.currentThread())
# _____________________________________________________________________
#
# High Level Interface
# _____________________________________________________________________
def remote_exec(self, source):
""" return channel object for communicating with the asynchronously
executing 'source' code which will have a corresponding 'channel'
object in its executing namespace.
"""
source = str(Source(source))
channel = self.channelfactory.new()
self._outgoing.put(Message.CHANNEL_OPEN(channel.id, source))
return channel
class Channel(object):
"""Communication channel between two possibly remote threads of code. """
def __init__(self, gateway, id):
assert isinstance(id, int)
self.gateway = gateway
self.id = id
self._items = Queue.Queue()
self._closeevent = threading.Event()
def _close(self, error=None):
if error is not None:
self._error = RemoteError(error)
self._items.put(self._error)
else:
self._error = None
self._closeevent.set()
def __repr__(self):
flag = self._closeevent.isSet() and "closed" or "open"
return "<Channel id=%d %s>" % (self.id, flag)
def waitclose(self, timeout):
""" wait until this channel is closed. Note that a closed
channel may still hold items that will be received or
send. Note that exceptions from the other side will be
reraised as gateway.ExecutionFailed exceptions containing
a textual representation of the remote traceback.
"""
self._closeevent.wait(timeout=timeout)
if not self._closeevent.isSet():
raise IOError, "Timeout"
if self._error:
raise self._error
def send(self, item):
"""sends the given item to the other side of the channel,
possibly blocking if the sender queue is full.
Note that each value V of the items needs to have the
following property (all basic types in python have it):
eval(repr(V)) == V."""
self.gateway._outgoing.put(Message.CHANNEL_DATA(self.id, repr(item)))
def receive(self):
"""receives an item that was sent from the other side,
possibly blocking if there is none.
Note that exceptions from the other side will be
reraised as gateway.ExecutionFailed exceptions containing
a textual representation of the remote traceback.
"""
x = self._items.get()
if isinstance(x, RemoteError):
raise x
return x
#
# helpers
#
class ChannelFactory(object):
def __init__(self, gateway, startcount=1):
self._dict = dict()
self._lock = threading.RLock()
self.gateway = gateway
self.count = startcount
def new(self):
self._lock.acquire()
try:
channel = Channel(self.gateway, self.count)
self._dict[self.count] = channel
return channel
finally:
self.count += 2
self._lock.release()
def __getitem__(self, key):
self._lock.acquire()
try:
return self._dict[key]
finally:
self._lock.release()
def __setitem__(self, key, value):
self._lock.acquire()
try:
self._dict[key] = value
finally:
self._lock.release()
def __delitem__(self, key):
self._lock.acquire()
try: