[Checkins] SVN: grokcore.startup/trunk/ allow running ad hoc scripts against a setup application, similar to "./bin/zopectl run [somescript.py]"

Jan-Wijbrand Kolman janwijbrand at gmail.com
Thu May 20 06:33:40 EDT 2010


Log message for revision 112560:
  allow running ad hoc scripts against a setup application, similar to "./bin/zopectl run [somescript.py]"

Changed:
  U   grokcore.startup/trunk/CHANGES.txt
  U   grokcore.startup/trunk/setup.py
  U   grokcore.startup/trunk/src/grokcore/startup/README.txt
  U   grokcore.startup/trunk/src/grokcore/startup/startup.py
  U   grokcore.startup/trunk/src/grokcore/startup/tests/test_grokcorestartup.py

-=-
Modified: grokcore.startup/trunk/CHANGES.txt
===================================================================
--- grokcore.startup/trunk/CHANGES.txt	2010-05-20 08:37:03 UTC (rev 112559)
+++ grokcore.startup/trunk/CHANGES.txt	2010-05-20 10:33:39 UTC (rev 112560)
@@ -4,6 +4,11 @@
 0.5 (unreleased)
 ================
 
+- Amend the interactive_debug_prompt function to behave more or less like the
+  "old" zopectl command. Whenever there's commandline arguments passed to the
+  command, the first one is assumed to be a python script that is 'execfile'd.
+  This allows ad hoc scripts to run against the setup application.
+
 - Make package comply to zope.org repository policy.
 
 - The upgrade notes will be moved to the Grok upgrade notes.

Modified: grokcore.startup/trunk/setup.py
===================================================================
--- grokcore.startup/trunk/setup.py	2010-05-20 08:37:03 UTC (rev 112559)
+++ grokcore.startup/trunk/setup.py	2010-05-20 10:33:39 UTC (rev 112560)
@@ -13,10 +13,14 @@
     )
 
 tests_require = [
+    'zope.app.appsetup',
     'zope.app.testing',
     'zope.component',
+    'zope.container',
     'zope.interface',
+    'zope.site',
     'zope.testing',
+    'zope.traversing',
     ]
 
 setup(

Modified: grokcore.startup/trunk/src/grokcore/startup/README.txt
===================================================================
--- grokcore.startup/trunk/src/grokcore/startup/README.txt	2010-05-20 08:37:03 UTC (rev 112559)
+++ grokcore.startup/trunk/src/grokcore/startup/README.txt	2010-05-20 10:33:39 UTC (rev 112560)
@@ -255,23 +255,84 @@
     >>> reraise()
     False
 
+  Clean up the temp_dir
 
+    >>> import shutil
+    >>> shutil.rmtree(temp_dir)
+
 ``interactive_debug_prompt(zope_conf_path)``
 --------------------------------------------
 
   Get an interactive console with a debugging shell started.
 
-  Normally used as entry point in projects ``setup.py``.
+    >>> import zope.app.appsetup.appsetup
+    >>> # Ugh - allow a reconfiguration of an app.
+    >>> zope.app.appsetup.appsetup._configured = False
 
-  The debugger will be started with the configuration given in
-  `zope.conf_path`.
+    >>> temp_dir = tempfile.mkdtemp()
+    >>> sitezcml = os.path.join(temp_dir, 'site.zcml')
+    >>> open(sitezcml, 'w').write(
+    ...    """<configure xmlns="http://namespaces.zope.org/zope">
+    ...   <include package="zope.component" file="meta.zcml"/>
+    ...   <include package="zope.component"/>
+    ...   <include package="zope.traversing"/>
+    ...   <include package="zope.security" file="meta.zcml"/>
+    ...   <include package="zope.security"/>
+    ...   <include package="zope.container"/>
+    ...   <include package="zope.site"/>
+    ...   <include package="zope.app.appsetup"/>
+    ... </configure>""")
+    >>>
+    >>> zopeconf = os.path.join(temp_dir, 'zope.conf')
+    >>> open(zopeconf, 'w').write("""
+    ...     site-definition %s
+    ...     <zodb>
+    ...       <filestorage>
+    ...         path %s
+    ...       </filestorage>
+    ...     </zodb>
+    ...     <eventlog>
+    ...       <logfile>
+    ...         path STDOUT
+    ...         formatter zope.exceptions.log.Formatter
+    ...       </logfile>
+    ...     </eventlog>
+    ...     """ % (sitezcml, os.path.join(temp_dir, 'Data.fs')))
+    >>>
+    >>> import sys
+    >>> old_argv = sys.argv[:]
+    >>>
+    >>> script = os.path.join(temp_dir, 'script.py')
+    >>> open(script, 'w').write(
+    ...    """import sys
+    ... from pprint import pprint
+    ... pprint(debugger)
+    ... pprint(app)
+    ... pprint(root)
+    ... pprint(sys.argv)""")
+    >>>
+    >>> sys.argv = ['interactive_debug_prompt', script]
+    >>> from grokcore.startup import interactive_debug_prompt
+    >>> try:
+    ...     interactive_debug_prompt(zope_conf=zopeconf)
+    ... except SystemExit:
+    ...     pass
+    ------
+    ...WARNING zope.app.appsetup Security policy is not configured.
+    Please make sure that securitypolicy.zcml is included in site.zcml
+    immediately before principals.zcml
+    ...
+    <zope.app.debug.debug.Debugger object at ...>
+    <zope.app.debug.debug.Debugger object at ...>
+    <zope.site.folder.Folder object at ...>
+    ['...script.py']
 
-  We cannot start an interactive console here, but we can at least
-  import the debugger function::
+  Clean up the temp_dir
 
-    >>> from grokcore.startup import interactive_debug_prompt
+    >>> sys.argv = old_argv
+    >>> import shutil
+    >>> shutil.rmtree(temp_dir)
 
-
 .. _grok: http://pypi.python.org/pypi/grok
 .. _grokproject: http://pypi.python.org/pypi/grokproject
 .. _Paste: http://pythonpaste.org/

Modified: grokcore.startup/trunk/src/grokcore/startup/startup.py
===================================================================
--- grokcore.startup/trunk/src/grokcore/startup/startup.py	2010-05-20 08:37:03 UTC (rev 112559)
+++ grokcore.startup/trunk/src/grokcore/startup/startup.py	2010-05-20 10:33:39 UTC (rev 112560)
@@ -32,22 +32,39 @@
         provideAdapter(do_not_reraise_exception, (iface, ), IReRaiseException)
     # Return the created application
     return app
-    
 
-def interactive_debug_prompt(zope_conf=os.path.join('parts', 'etc',
-                                                    'zope.conf')):
+def interactive_debug_prompt(
+    zope_conf=os.path.join('parts', 'etc', 'zope.conf')):
+
     db = zope.app.wsgi.config(zope_conf)
     debugger = zope.app.debug.Debugger.fromDatabase(db)
-    # Invoke an interactive interpreter shell
-    banner = ("Welcome to the interactive debug prompt.\n"
-              "The 'root' variable contains the ZODB root folder.\n"
-              "The 'app' variable contains the Debugger, 'app.publish(path)' "
-              "simulates a request.")
-    code.interact(banner=banner, local={'debugger': debugger,
-                                        'app':      debugger,
-                                        'root':     debugger.root()})
+    globals_ = {
+        'debugger': debugger,
+        'app': debugger,
+        'root': debugger.root()}
 
+    if len(sys.argv) > 1:
+        # There're arguments passed to the command. We replicate the
+        # "old" zopectl run command behaviour that would execfile()
+        # the second argument.
 
+        # The current first argument is the interactive_debugger command
+        # itself. Pop it from the args list and as a result, the script
+        # to run is the first argument.
+        del sys.argv[0]
+
+        globals_['__file__'] = sys.argv[0]
+        execfile(sys.argv[0], globals_)
+        sys.exit()
+
+    # Invoke an interactive interpreter prompt
+    banner = (
+        "Welcome to the interactive debug prompt.\n"
+        "The 'root' variable contains the ZODB root folder.\n"
+        "The 'app' variable contains the Debugger, 'app.publish(path)' "
+        "simulates a request.")
+    code.interact(banner=banner, local=globals_)
+
 # To retain backwards compatibilty with the ctl scripts generated by previous
 # versions of grokproject we keep the zdaemon controller code.
 # The intention is to have this code removed during the Grok 1.2 release.

Modified: grokcore.startup/trunk/src/grokcore/startup/tests/test_grokcorestartup.py
===================================================================
--- grokcore.startup/trunk/src/grokcore/startup/tests/test_grokcorestartup.py	2010-05-20 08:37:03 UTC (rev 112559)
+++ grokcore.startup/trunk/src/grokcore/startup/tests/test_grokcorestartup.py	2010-05-20 10:33:39 UTC (rev 112560)
@@ -14,17 +14,18 @@
 optionflags=(doctest.ELLIPSIS+
             doctest.NORMALIZE_WHITESPACE)
 
-main_doctests = ['README.txt',]
+main_doctests = ['README.txt']
 
 def test_suite():
     suite = unittest.TestSuite()
 
     for testfile in main_doctests:
         suite.addTest(
-            doctest.DocFileSuite(os.path.join('..', testfile),
-                                 optionflags=optionflags,
-                                 globs=globals(),
-                                 checker=checker))
+            doctest.DocFileSuite(
+                os.path.join('..', testfile),
+                optionflags=optionflags,
+                globs=globals(),
+                checker=checker))
     return suite
 
 if __name__ == '__main__':



More information about the checkins mailing list