Created twisted package with manhole stuff

git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@1084 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
helmutm 2006-02-18 10:56:52 +00:00
parent dc5c09afdd
commit c0b49805f3
4 changed files with 135 additions and 0 deletions

47
twisted/__init__.py Normal file
View file

@ -0,0 +1,47 @@
"""
$Id$
"""
from twisted.internet import reactor, protocol
from twisted.protocols import basic
from twisted.cred import portal, checkers
from twisted.conch import manhole, manhole_ssh
try:
from zope.app.publication.zopepublication import ZopePublication
from zope.app.component.hooks import setSite
hasZope = True
except:
hasZope = False
import time
import sys
from cStringIO import StringIO
def getManholeFactory(namespace, **passwords):
realm = manhole_ssh.TerminalRealm()
def getManhole(_):
return manhole.Manhole(namespace)
realm.chainedProtocolFactory.protocolFactory = getManhole
p = portal.Portal(realm)
p.registerChecker(
checkers.InMemoryUsernamePasswordDatabaseDontUse(**passwords))
return manhole_ssh.ConchFactory(p)
def printTime():
print 'twisted.manhole running:', time.strftime('%H:%M:%S')
reactor.callLater(60, printTime)
#reactor.callLater(10, stopReactor)
#reactor.listenTCP(5222, EchoServerFactory())
def startup(event=None):
printTime()
d = globals()
#d['event'] = event
if hasZope and event is not None:
conn =event.database.open()
root = conn.root()[ZopePublication.root_name]
d.update(locals())
reactor.listenTCP(5001, getManholeFactory(d, admin='aaa'))
if __name__ == '__main__':
startup()

11
twisted/configure.zcml Normal file
View file

@ -0,0 +1,11 @@
<!-- $Id$ -->
<configure
xmlns="http://namespaces.zope.org/zope"
i18n_domain="zope"
>
<subscriber handler="cybertools.twisted.manhole.startup"
for="zope.app.appsetup.interfaces.IDatabaseOpenedEvent" />
</configure>

View file

@ -0,0 +1 @@
<include package="cybertools.twisted" />

76
twisted/manhole.py Normal file
View file

@ -0,0 +1,76 @@
"""
$Id$
"""
from twisted.internet import reactor, protocol
from twisted.protocols import basic
from twisted.cred import portal, checkers
from twisted.conch import manhole, manhole_ssh
try:
from zope.app.publication.zopepublication import ZopePublication
from zope.app.component.hooks import setSite
from zope.app import zapi
hasZope = True
except:
hasZope = False
import time
import sys
from cStringIO import StringIO
def getManholeFactory(namespace, **passwords):
realm = manhole_ssh.TerminalRealm()
def getManhole(_):
return manhole.Manhole(namespace)
realm.chainedProtocolFactory.protocolFactory = getManhole
p = portal.Portal(realm)
p.registerChecker(
checkers.InMemoryUsernamePasswordDatabaseDontUse(**passwords))
return manhole_ssh.ConchFactory(p)
def printTime():
print 'twisted.manhole running:', time.strftime('%H:%M:%S')
reactor.callLater(60, printTime)
#reactor.callLater(10, stopReactor)
#reactor.listenTCP(5222, EchoServerFactory())
def help():
info = """
Use dir() to see what variables and functions are available."""
zopeInfo = """
In order to get access to local utilities and adapters you should
issue a setSite(root). Don't forget to call setSite() before
finishing your session.
You may use x = zapi.traverse(root, 'path/to/object') to get an
object in your folder hierarchy. Then you may call any method or
access any attribute of this object."""
print info
if hasZope:
print zopeInfo
print
def startup(event=None, port=5001):
global hasZope
printTime()
d = globals()
if hasZope and event is not None:
conn =event.database.open()
root = conn.root()[ZopePublication.root_name]
else:
hasZope = False
d.update(locals())
namespace = {}
for key in ('__builtins__', 'event', 'setSite', 'hasZope', 'zapi', 'root',
'__doc__', 'help'):
if key in d:
namespace[key] = d[key]
reactor.listenTCP(port, getManholeFactory(namespace, admin='aaa'))
if __name__ == '__main__':
port = 5001
if len(sys.argv) > 1:
port = int(sys.argv[-1])
startup(port=port)
reactor.run()