Show More
@@ -0,0 +1,320 b'' | |||
|
1 | # -*- coding: utf-8 -*- | |
|
2 | # | |
|
3 | # File: ipy_profile_zope.py | |
|
4 | # | |
|
5 | # Copyright (c) InQuant GmbH | |
|
6 | # | |
|
7 | # An ipython profile for zope and plone. Some ideas | |
|
8 | # stolen from http://www.tomster.org. | |
|
9 | # | |
|
10 | # German Free Software License (D-FSL) | |
|
11 | # | |
|
12 | # This Program may be used by anyone in accordance with the terms of the | |
|
13 | # German Free Software License | |
|
14 | # The License may be obtained under <http://www.d-fsl.org>. | |
|
15 | ||
|
16 | __author__ = """Stefan Eletzhofer <stefan.eletzhofer@inquant.de>""" | |
|
17 | __docformat__ = 'plaintext' | |
|
18 | __revision__ = "$Revision$" | |
|
19 | ||
|
20 | from IPython import ipapi | |
|
21 | from IPython import Release | |
|
22 | from types import StringType | |
|
23 | import sys | |
|
24 | import os | |
|
25 | import textwrap | |
|
26 | ||
|
27 | # The import below effectively obsoletes your old-style ipythonrc[.ini], | |
|
28 | # so consider yourself warned! | |
|
29 | # import ipy_defaults | |
|
30 | ||
|
31 | _marker = [] | |
|
32 | def shasattr(obj, attr, acquire=False): | |
|
33 | """ See Archetypes/utils.py | |
|
34 | """ | |
|
35 | if not acquire: | |
|
36 | obj = obj.aq_base | |
|
37 | return getattr(obj, attr, _marker) is not _marker | |
|
38 | ||
|
39 | class ZopeDebug(object): | |
|
40 | def __init__(self): | |
|
41 | ||
|
42 | self.instancehome = os.environ.get( "INSTANCE_HOME" ) | |
|
43 | ||
|
44 | configfile = os.environ.get( "CONFIG_FILE" ) | |
|
45 | if configfile is None and self.instancehome is not None: | |
|
46 | configfile = os.path.join( self.instancehome, "etc", "zope.conf" ) | |
|
47 | ||
|
48 | if configfile is None: | |
|
49 | raise RuntimeError( "CONFIG_FILE env not set" ) | |
|
50 | ||
|
51 | print "CONFIG_FILE=", configfile | |
|
52 | print "INSTANCE_HOME=", self.instancehome | |
|
53 | ||
|
54 | self.configfile = configfile | |
|
55 | ||
|
56 | try: | |
|
57 | from Zope2 import configure | |
|
58 | except ImportError: | |
|
59 | from Zope import configure | |
|
60 | ||
|
61 | configure( configfile ) | |
|
62 | ||
|
63 | try: | |
|
64 | import Zope2 | |
|
65 | app = Zope2.app() | |
|
66 | except ImportError: | |
|
67 | import Zope | |
|
68 | app = Zope.app() | |
|
69 | ||
|
70 | from Testing.makerequest import makerequest | |
|
71 | self.app = makerequest( app ) | |
|
72 | ||
|
73 | try: | |
|
74 | self._make_permissive() | |
|
75 | print "Permissive security installed" | |
|
76 | except: | |
|
77 | print "Permissive security NOT installed" | |
|
78 | ||
|
79 | self._pwd = self.portal or self.app | |
|
80 | ||
|
81 | try: | |
|
82 | from zope.component import getSiteManager | |
|
83 | from zope.component import getGlobalSiteManager | |
|
84 | from zope.app.component.hooks import setSite | |
|
85 | ||
|
86 | if self.portal is not None: | |
|
87 | setSite( self.portal ) | |
|
88 | ||
|
89 | gsm = getGlobalSiteManager() | |
|
90 | sm = getSiteManager() | |
|
91 | ||
|
92 | if sm is gsm: | |
|
93 | print "ERROR SETTING SITE!" | |
|
94 | except: | |
|
95 | pass | |
|
96 | ||
|
97 | ||
|
98 | @property | |
|
99 | def utils(self): | |
|
100 | class Utils(object): | |
|
101 | commit = self.commit | |
|
102 | sync = self.sync | |
|
103 | objectInfo = self.objectInfo | |
|
104 | ls = self.ls | |
|
105 | pwd = self.pwd | |
|
106 | cd = self.cd | |
|
107 | su = self.su | |
|
108 | getCatalogInfo = self.getCatalogInfo | |
|
109 | ||
|
110 | @property | |
|
111 | def cwd(self): | |
|
112 | return self.pwd() | |
|
113 | ||
|
114 | return Utils() | |
|
115 | ||
|
116 | @property | |
|
117 | def namespace(self): | |
|
118 | return dict( utils=self.utils, app=self.app, portal=self.portal ) | |
|
119 | ||
|
120 | @property | |
|
121 | def portal(self): | |
|
122 | portals = self.app.objectValues( "Plone Site" ) | |
|
123 | if len(portals): | |
|
124 | return portals[0] | |
|
125 | else: | |
|
126 | raise KeyError( "No Plone Site found.") | |
|
127 | ||
|
128 | def pwd(self): | |
|
129 | return self._pwd | |
|
130 | ||
|
131 | def _make_permissive(self): | |
|
132 | """ | |
|
133 | Make a permissive security manager with all rights. Hell, | |
|
134 | we're developers, aren't we? Security is for whimps. :) | |
|
135 | """ | |
|
136 | from Products.CMFCore.tests.base.security import PermissiveSecurityPolicy | |
|
137 | import AccessControl | |
|
138 | from AccessControl.SecurityManagement import newSecurityManager | |
|
139 | from AccessControl.SecurityManager import setSecurityPolicy | |
|
140 | ||
|
141 | _policy = PermissiveSecurityPolicy() | |
|
142 | self.oldpolicy = setSecurityPolicy(_policy) | |
|
143 | newSecurityManager(None, AccessControl.User.system) | |
|
144 | ||
|
145 | def su(self, username): | |
|
146 | """ Change to named user. | |
|
147 | """ | |
|
148 | # TODO Make it easy to change back to permissive security. | |
|
149 | user = self.portal.acl_users.getUser(username) | |
|
150 | if not user: | |
|
151 | print "Can't find %s in %s" % (username, self.portal.acl_users) | |
|
152 | return | |
|
153 | ||
|
154 | from AccessControl import ZopeSecurityPolicy | |
|
155 | import AccessControl | |
|
156 | from AccessControl.SecurityManagement import newSecurityManager, getSecurityManager | |
|
157 | from AccessControl.SecurityManager import setSecurityPolicy | |
|
158 | ||
|
159 | _policy = ZopeSecurityPolicy | |
|
160 | self.oldpolicy = setSecurityPolicy(_policy) | |
|
161 | wrapped_user = user.__of__(self.portal.acl_users) | |
|
162 | newSecurityManager(None, user) | |
|
163 | print 'User changed.' | |
|
164 | return getSecurityManager().getUser() | |
|
165 | ||
|
166 | def getCatalogInfo(self, obj=None, catalog='portal_catalog', query=None, sort_on='created', sort_order='reverse' ): | |
|
167 | """ Inspect portal_catalog. Pass an object or object id for a | |
|
168 | default query on that object, or pass an explicit query. | |
|
169 | """ | |
|
170 | if obj and query: | |
|
171 | print "Ignoring %s, using query." % obj | |
|
172 | ||
|
173 | catalog = self.portal.get(catalog) | |
|
174 | if not catalog: | |
|
175 | return 'No catalog' | |
|
176 | ||
|
177 | indexes = catalog._catalog.indexes | |
|
178 | if not query: | |
|
179 | if type(obj) is StringType: | |
|
180 | cwd = self.pwd() | |
|
181 | obj = cwd.unrestrictedTraverse( obj ) | |
|
182 | # If the default in the signature is mutable, its value will | |
|
183 | # persist across invocations. | |
|
184 | query = {} | |
|
185 | if indexes.get('path'): | |
|
186 | from string import join | |
|
187 | path = join(obj.getPhysicalPath(), '/') | |
|
188 | query.update({'path': path}) | |
|
189 | if indexes.get('getID'): | |
|
190 | query.update({'getID': obj.id, }) | |
|
191 | if indexes.get('UID') and shasattr(obj, 'UID'): | |
|
192 | query.update({'UID': obj.UID(), }) | |
|
193 | if indexes.get(sort_on): | |
|
194 | query.update({'sort_on': sort_on, 'sort_order': sort_order}) | |
|
195 | if not query: | |
|
196 | return 'Empty query' | |
|
197 | results = catalog(**query) | |
|
198 | ||
|
199 | result_info = [] | |
|
200 | for r in results: | |
|
201 | rid = r.getRID() | |
|
202 | if rid: | |
|
203 | result_info.append( | |
|
204 | {'path': catalog.getpath(rid), | |
|
205 | 'metadata': catalog.getMetadataForRID(rid), | |
|
206 | 'indexes': catalog.getIndexDataForRID(rid), } | |
|
207 | ) | |
|
208 | else: | |
|
209 | result_info.append({'missing': rid}) | |
|
210 | ||
|
211 | if len(result_info) == 1: | |
|
212 | return result_info[0] | |
|
213 | return result_info | |
|
214 | ||
|
215 | def commit(self): | |
|
216 | """ | |
|
217 | Commit the transaction. | |
|
218 | """ | |
|
219 | try: | |
|
220 | import transaction | |
|
221 | transaction.get().commit() | |
|
222 | except ImportError: | |
|
223 | get_transaction().commit() | |
|
224 | ||
|
225 | def sync(self): | |
|
226 | """ | |
|
227 | Sync the app's view of the zodb. | |
|
228 | """ | |
|
229 | self.app._p_jar.sync() | |
|
230 | ||
|
231 | def objectInfo( self, o ): | |
|
232 | """ | |
|
233 | Return a descriptive string of an object | |
|
234 | """ | |
|
235 | Title = "" | |
|
236 | t = getattr( o, 'Title', None ) | |
|
237 | if t: | |
|
238 | Title = t() | |
|
239 | return {'id': o.getId(), | |
|
240 | 'Title': Title, | |
|
241 | 'portal_type': getattr( o, 'portal_type', o.meta_type), | |
|
242 | 'folderish': o.isPrincipiaFolderish | |
|
243 | } | |
|
244 | ||
|
245 | def cd( self, path ): | |
|
246 | """ | |
|
247 | Change current dir to a specific folder. | |
|
248 | ||
|
249 | cd( ".." ) | |
|
250 | cd( "/plone/Members/admin" ) | |
|
251 | cd( portal.Members.admin ) | |
|
252 | etc. | |
|
253 | """ | |
|
254 | if type(path) is not StringType: | |
|
255 | path = '/'.join(path.getPhysicalPath()) | |
|
256 | cwd = self.pwd() | |
|
257 | x = cwd.unrestrictedTraverse( path ) | |
|
258 | if x is None: | |
|
259 | raise KeyError( "Can't cd to %s" % path ) | |
|
260 | ||
|
261 | print "%s -> %s" % ( self.pwd().getId(), x.getId() ) | |
|
262 | self._pwd = x | |
|
263 | ||
|
264 | def ls( self, x=None ): | |
|
265 | """ | |
|
266 | List object(s) | |
|
267 | """ | |
|
268 | if type(x) is StringType: | |
|
269 | cwd = self.pwd() | |
|
270 | x = cwd.unrestrictedTraverse( x ) | |
|
271 | if x is None: | |
|
272 | x = self.pwd() | |
|
273 | if x.isPrincipiaFolderish: | |
|
274 | return [self.objectInfo(o) for id, o in x.objectItems()] | |
|
275 | else: | |
|
276 | return self.objectInfo( x ) | |
|
277 | ||
|
278 | zope_debug = None | |
|
279 | ||
|
280 | def ipy_set_trace(): | |
|
281 | import IPython; IPython.Debugger.Pdb().set_trace() | |
|
282 | ||
|
283 | def main(): | |
|
284 | global zope_debug | |
|
285 | ip = ipapi.get() | |
|
286 | o = ip.options | |
|
287 | # autocall to "full" mode (smart mode is default, I like full mode) | |
|
288 | ||
|
289 | SOFTWARE_HOME = os.environ.get( "SOFTWARE_HOME" ) | |
|
290 | sys.path.append( SOFTWARE_HOME ) | |
|
291 | print "SOFTWARE_HOME=%s\n" % SOFTWARE_HOME | |
|
292 | ||
|
293 | zope_debug = ZopeDebug() | |
|
294 | ||
|
295 | # <HACK ALERT> | |
|
296 | import pdb; | |
|
297 | pdb.set_trace = ipy_set_trace | |
|
298 | # </HACK ALERT> | |
|
299 | ||
|
300 | # I like my banner minimal. | |
|
301 | o.banner = "ZOPE Py %s IPy %s\n" % (sys.version.split('\n')[0],Release.version) | |
|
302 | ||
|
303 | print textwrap.dedent("""\ | |
|
304 | ZOPE mode iPython shell. | |
|
305 | ||
|
306 | Bound names: | |
|
307 | app | |
|
308 | portal | |
|
309 | utils.{ %s } | |
|
310 | ||
|
311 | Uses the $SOFTWARE_HOME and $CONFIG_FILE environment | |
|
312 | variables. | |
|
313 | """ % ( ",".join([ x for x in dir(zope_debug.utils) if not x.startswith("_") ] ) ) ) | |
|
314 | ||
|
315 | ||
|
316 | ip.user_ns.update( zope_debug.namespace ) | |
|
317 | ||
|
318 | ||
|
319 | main() | |
|
320 | # vim: set ft=python ts=4 sw=4 expandtab : |
General Comments 0
You need to be logged in to leave comments.
Login now