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 defaultCacheDir = zenPath('var')
26
28
30
31 - def __init__(self, noopts=0, app=None, connect=False):
38
39
41 if not self.app:
42 from ZEO import ClientStorage
43 from ZODB import DB
44 addr = (self.options.host, self.options.port)
45 storage=ClientStorage.ClientStorage(addr,
46 client=self.options.pcachename,
47 var=self.options.pcachedir,
48 cache_size=self.options.pcachesize*1024*1024)
49 self.db=DB(storage, cache_size=self.options.cachesize)
50 self.poollock = Lock()
51 self.getDataRoot()
52 self.login()
53
54
55 - def login(self, name='admin', userfolder=None):
56 '''Logs in.'''
57 if userfolder is None:
58 userfolder = self.app.acl_users
59 user = userfolder.getUserById(name)
60 if user is None: return
61 if not hasattr(user, 'aq_base'):
62 user = user.__of__(userfolder)
63 newSecurityManager(None, user)
64
65
67 '''Logs out.'''
68 noSecurityManager()
69
70
72 """Return a wrapped app connection from the connection pool.
73 """
74 if not self.db:
75 raise ZentinelException(
76 "running inside zope can't open connections.")
77 try:
78 self.poollock.acquire()
79 connection=self.db.open()
80 root=connection.root()
81 app=root['Application']
82 self.getContext(app)
83 app._p_jar.sync()
84 return app
85 finally:
86 self.poollock.release()
87
88
90 """Close all connections in both free an inuse pools.
91 """
92 self.db.close()
93
94
96 if self.app: return
97 self.connection=self.db.open()
98 root=self.connection.root()
99 self.app=root['Application']
100 self.getContext(self.app)
101
102
104 self.connection.sync()
105
106
108 self.connection.close()
109
110 self.app = None
111 self.dataroot = None
112 self.dmd = None
113
114
116 if not self.app: self.opendb()
117 if not self.dataroot:
118 self.dataroot = getObjByPath(self.app, self.options.dataroot)
119 self.dmd = self.dataroot
120
121
122 - def getContext(self, app):
123 from ZPublisher.HTTPRequest import HTTPRequest
124 from ZPublisher.HTTPResponse import HTTPResponse
125 from ZPublisher.BaseRequest import RequestContainer
126 resp = HTTPResponse(stdout=None)
127 env = {
128 'SERVER_NAME':'localhost',
129 'SERVER_PORT':'8080',
130 'REQUEST_METHOD':'GET'
131 }
132 req = HTTPRequest(None, env, resp)
133 return app.__of__(RequestContainer(REQUEST = req))
134
135
137 """return an object based on a path starting from the dmd"""
138 return getObjByPath(self.app, self.options.dataroot+path)
139
140
145
146
148 """basic options setup sub classes can add more options here"""
149 CmdBase.buildOptions(self)
150 self.parser.add_option('--host',
151 dest="host",default="localhost",
152 help="hostname of zeo server")
153 self.parser.add_option('--port',
154 dest="port",type="int", default=8100,
155 help="port of zeo server")
156 self.parser.add_option('-R', '--dataroot',
157 dest="dataroot",
158 default="/zport/dmd",
159 help="root object for data load (i.e. /zport/dmd)")
160 self.parser.add_option('--cachesize',
161 dest="cachesize",default=1000, type='int',
162 help="in memory cachesize default: 1000")
163 self.parser.add_option('--pcachename',
164 dest="pcachename",default=None,
165 help="persistent cache file name default:None")
166 self.parser.add_option('--pcachedir',
167 dest="pcachedir",default=defaultCacheDir,
168 help="persistent cache file directory")
169 self.parser.add_option('--pcachesize',
170 dest="pcachesize",default=10, type='int',
171 help="persistent cache file size in MB")
172