1
2
3
4
5
6
7
8
9
10
11
12
13
14 __doc__="""ZenScriptBase
15 """
16
17 from AccessControl.SecurityManagement import newSecurityManager
18 from AccessControl.SecurityManagement import noSecurityManager
19 from threading import Lock
20 from Utils import getObjByPath, zenPath
21 from CmdBase import CmdBase
22
23 from Exceptions import ZentinelException
24
25 import os
26 defaultCacheDir = zenPath('var')
27
29
31
39
40
42 if not self.app:
43 from ZEO import ClientStorage
44 from ZODB import DB
45 addr = (self.options.host, self.options.port)
46 storage=ClientStorage.ClientStorage(addr,
47 client=self.options.pcachename,
48 var=self.options.pcachedir,
49 cache_size=self.options.pcachesize*1024*1024)
50 self.db=DB(storage, cache_size=self.options.cachesize)
51 self.poollock = Lock()
52 self.getDataRoot()
53 self.login()
54
55
56 - def login(self, name='admin', userfolder=None):
57 '''Logs in.'''
58 if userfolder is None:
59 userfolder = self.app.acl_users
60 user = userfolder.getUserById(name)
61 if user is None: return
62 if not hasattr(user, 'aq_base'):
63 user = user.__of__(userfolder)
64 newSecurityManager(None, user)
65
66
68 '''Logs out.'''
69 noSecurityManager()
70
71
73 """Return a wrapped app connection from the connection pool.
74 """
75 if not self.db:
76 raise ZentinelException(
77 "running inside zope can't open connections.")
78 try:
79 self.poollock.acquire()
80 connection=self.db.open()
81 root=connection.root()
82 app=root['Application']
83 self.getContext(app)
84 app._p_jar.sync()
85 return app
86 finally:
87 self.poollock.release()
88
89
91 """Close all connections in both free an inuse pools.
92 """
93 self.db.close()
94
95
97 if self.app: return
98 self.connection=self.db.open()
99 root=self.connection.root()
100 self.app=root['Application']
101 self.getContext(self.app)
102
103
105 self.connection.sync()
106
107
109 self.connection.close()
110
111 self.app = None
112 self.dataroot = None
113 self.dmd = None
114
115
117 if not self.app: self.opendb()
118 if not self.dataroot:
119 self.dataroot = getObjByPath(self.app, self.options.dataroot)
120 self.dmd = self.dataroot
121
122
123 - def getContext(self, app):
124 from ZPublisher.HTTPRequest import HTTPRequest
125 from ZPublisher.HTTPResponse import HTTPResponse
126 from ZPublisher.BaseRequest import RequestContainer
127 resp = HTTPResponse(stdout=None)
128 env = {
129 'SERVER_NAME':'localhost',
130 'SERVER_PORT':'8080',
131 'REQUEST_METHOD':'GET'
132 }
133 req = HTTPRequest(None, env, resp)
134 return app.__of__(RequestContainer(REQUEST = req))
135
136
138 """return an object based on a path starting from the dmd"""
139 return getObjByPath(self.app, self.options.dataroot+path)
140
141
146
147
149 """basic options setup sub classes can add more options here"""
150 CmdBase.buildOptions(self)
151 self.parser.add_option('--host',
152 dest="host",default="localhost",
153 help="hostname of zeo server")
154 self.parser.add_option('--port',
155 dest="port",type="int", default=8100,
156 help="port of zeo server")
157 self.parser.add_option('-R', '--dataroot',
158 dest="dataroot",
159 default="/zport/dmd",
160 help="root object for data load (i.e. /zport/dmd)")
161 self.parser.add_option('--cachesize',
162 dest="cachesize",default=1000, type='int',
163 help="in memory cachesize default: 1000")
164 self.parser.add_option('--pcachename',
165 dest="pcachename",default=None,
166 help="persistent cache file name default:None")
167 self.parser.add_option('--pcachedir',
168 dest="pcachedir",default=defaultCacheDir,
169 help="persistent cache file directory")
170 self.parser.add_option('--pcachesize',
171 dest="pcachesize",default=10, type='int',
172 help="persistent cache file size in MB")
173