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
34 -def login(context, name='admin', userfolder=None):
35 '''Logs in.'''
36 if userfolder is None:
37 userfolder = context.getPhysicalRoot().acl_users
38 user = userfolder.getUserById(name)
39 if user is None: return
40 if not hasattr(user, 'aq_base'):
41 user = user.__of__(userfolder)
42 newSecurityManager(None, user)
43 return user
44
46
47
48 - def __init__(self, noopts=0, app=None, keeproot=False):
49 ZenDaemon.__init__(self, noopts, keeproot)
50 self.dataroot = None
51 self.app = app
52 self.db = None
53 if not app:
54 try:
55 self.zeoConnect()
56 except ValueError:
57 cache = os.path.join(self.options.pcachedir,
58 '%s.zec' % self.options.pcachename)
59 if os.path.exists(cache):
60 self.log.warning("Deleting corrupted cache %s" % cache)
61 os.unlink(cache)
62 self.zeoConnect()
63 else:
64 raise
65 self.poollock = Lock()
66 self.getDataRoot()
67 self.login()
68
70 from ZEO.ClientStorage import ClientStorage
71 storage=ClientStorage((self.options.host, self.options.port),
72 client=self.options.pcachename,
73 var=self.options.pcachedir,
74 cache_size=self.options.pcachesize*1024*1024)
75 from ZODB import DB
76 self.db = DB(storage, cache_size=self.options.cachesize)
77
78
79 - def login(self, name='admin', userfolder=None):
80 '''Logs in.'''
81 login(self.dmd, name, userfolder)
82
83
85 '''Logs out.'''
86 noSecurityManager()
87
88
90 """Return a wrapped app connection from the connection pool.
91 """
92 if not self.db:
93 raise ZentinelException(
94 "running inside zope can't open connections.")
95 try:
96 self.poollock.acquire()
97 connection=self.db.open()
98 root=connection.root()
99 app=root['Application']
100 self.getContext(app)
101 app._p_jar.sync()
102 return app
103 finally:
104 self.poollock.release()
105
106
108 """Close all connections in both free an inuse pools.
109 """
110 self.db.close()
111
112
114 if self.app: return
115 self.connection=self.db.open()
116 root=self.connection.root()
117 self.app=root['Application']
118 self.getContext(self.app)
119
120
122 self.connection.sync()
123
124
126 self.connection.close()
127
128 self.app = None
129 self.dataroot = None
130 self.dmd = None
131
132
134 if not self.app: self.opendb()
135 if not self.dataroot:
136 self.dataroot = getObjByPath(self.app, self.options.dataroot)
137 self.dmd = self.dataroot
138
139
140 - def getContext(self, app):
141 from ZPublisher.HTTPRequest import HTTPRequest
142 from ZPublisher.HTTPResponse import HTTPResponse
143 from ZPublisher.BaseRequest import RequestContainer
144 resp = HTTPResponse(stdout=None)
145 env = {
146 'SERVER_NAME':'localhost',
147 'SERVER_PORT':'8080',
148 'REQUEST_METHOD':'GET'
149 }
150 req = HTTPRequest(None, env, resp)
151 return app.__of__(RequestContainer(REQUEST = req))
152
153
155 """return an object based on a path starting from the dmd"""
156 return getObjByPath(self.app, self.options.dataroot+path)
157
158
163
164 - def sigTerm(self, signum=None, frame=None):
166
168 """basic options setup sub classes can add more options here"""
169 ZenDaemon.buildOptions(self)
170 self.parser.add_option('--host',
171 dest="host",default="localhost",
172 help="hostname of zeo server")
173 self.parser.add_option('--port',
174 dest="port",type="int", default=8100,
175 help="port of zeo server")
176 self.parser.add_option('-R', '--dataroot',
177 dest="dataroot",
178 default="/zport/dmd",
179 help="root object for data load (i.e. /zport/dmd)")
180 self.parser.add_option('--cachesize',
181 dest="cachesize",default=1000, type='int',
182 help="in memory cachesize default: 1000")
183 self.parser.add_option('--pcachename',
184 dest="pcachename",default=None,
185 help="persistent cache file name default:None")
186 self.parser.add_option('--pcachedir',
187 dest="pcachedir",default=defaultCacheDir,
188 help="persistent cache file directory")
189 self.parser.add_option('--pcachesize',
190 dest="pcachesize",default=10, type='int',
191 help="persistent cache file size in MB")
192