1
2
3
4
5
6
7
8
9
10
11
12
13
14 __doc__="""ZenDaemon
15
16 $Id: ZC.py,v 1.9 2004/02/16 17:19:31 edahl Exp $"""
17
18 __version__ = "$Revision: 1.9 $"[11:-2]
19
20 from threading import Lock
21
22 from AccessControl.SecurityManagement import newSecurityManager
23 from AccessControl.SecurityManagement import noSecurityManager
24 from Utils import getObjByPath, zenPath
25
26 from Exceptions import ZentinelException
27 from ZenDaemon import ZenDaemon
28
29 import os
30 defaultCacheDir = zenPath('var')
31
33
35
36
57
59 from ZEO.ClientStorage import ClientStorage
60 storage=ClientStorage((self.options.host, self.options.port),
61 client=self.options.pcachename,
62 var=self.options.pcachedir,
63 cache_size=self.options.pcachesize*1024*1024)
64 from ZODB import DB
65 self.db = DB(storage, cache_size=self.options.cachesize)
66
67
68 - def login(self, name='admin', userfolder=None):
69 '''Logs in.'''
70 if userfolder is None:
71 userfolder = self.app.acl_users
72 user = userfolder.getUserById(name)
73 if user is None: return
74 if not hasattr(user, 'aq_base'):
75 user = user.__of__(userfolder)
76 newSecurityManager(None, user)
77
78
80 '''Logs out.'''
81 noSecurityManager()
82
83
85 """Return a wrapped app connection from the connection pool.
86 """
87 if not self.db:
88 raise ZentinelException(
89 "running inside zope can't open connections.")
90 try:
91 self.poollock.acquire()
92 connection=self.db.open()
93 root=connection.root()
94 app=root['Application']
95 self.getContext(app)
96 app._p_jar.sync()
97 return app
98 finally:
99 self.poollock.release()
100
101
103 """Close all connections in both free an inuse pools.
104 """
105 self.db.close()
106
107
109 if self.app: return
110 self.connection=self.db.open()
111 root=self.connection.root()
112 self.app=root['Application']
113 self.getContext(self.app)
114
115
117 self.connection.sync()
118
119
121 self.connection.close()
122
123 self.app = None
124 self.dataroot = None
125 self.dmd = None
126
127
129 if not self.app: self.opendb()
130 if not self.dataroot:
131 self.dataroot = getObjByPath(self.app, self.options.dataroot)
132 self.dmd = self.dataroot
133
134
135 - def getContext(self, app):
136 from ZPublisher.HTTPRequest import HTTPRequest
137 from ZPublisher.HTTPResponse import HTTPResponse
138 from ZPublisher.BaseRequest import RequestContainer
139 resp = HTTPResponse(stdout=None)
140 env = {
141 'SERVER_NAME':'localhost',
142 'SERVER_PORT':'8080',
143 'REQUEST_METHOD':'GET'
144 }
145 req = HTTPRequest(None, env, resp)
146 return app.__of__(RequestContainer(REQUEST = req))
147
148
150 """return an object based on a path starting from the dmd"""
151 return getObjByPath(self.app, self.options.dataroot+path)
152
153
158
159
161 """basic options setup sub classes can add more options here"""
162 ZenDaemon.buildOptions(self)
163 self.parser.add_option('--host',
164 dest="host",default="localhost",
165 help="hostname of zeo server")
166 self.parser.add_option('--port',
167 dest="port",type="int", default=8100,
168 help="port of zeo server")
169 self.parser.add_option('-R', '--dataroot',
170 dest="dataroot",
171 default="/zport/dmd",
172 help="root object for data load (i.e. /zport/dmd)")
173 self.parser.add_option('--cachesize',
174 dest="cachesize",default=1000, type='int',
175 help="in memory cachesize default: 1000")
176 self.parser.add_option('--pcachename',
177 dest="pcachename",default=None,
178 help="persistent cache file name default:None")
179 self.parser.add_option('--pcachedir',
180 dest="pcachedir",default=defaultCacheDir,
181 help="persistent cache file directory")
182 self.parser.add_option('--pcachesize',
183 dest="pcachesize",default=10, type='int',
184 help="persistent cache file size in MB")
185