[Zope3-checkins] SVN: Zope3/trunk/ add script that shows how to use
the import reporter to extract information;
Fred L. Drake, Jr.
fred at zope.com
Fri May 28 12:45:45 EDT 2004
Log message for revision 25090:
add script that shows how to use the import reporter to extract information;
this reports who the first importer is for all modules imported when running
some script
-=-
Added: Zope3/trunk/src/zope/importtool/app.py
===================================================================
--- Zope3/trunk/src/zope/importtool/app.py 2004-05-28 16:04:07 UTC (rev 25089)
+++ Zope3/trunk/src/zope/importtool/app.py 2004-05-28 16:45:45 UTC (rev 25090)
@@ -0,0 +1,86 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Command-line tool to perform import analysis.
+"""
+import optparse
+import os
+import sys
+
+from zope.importtool import hook
+from zope.importtool import reporter
+
+
+def main(argv=None):
+ if argv is None:
+ argv = sys.argv
+ try:
+ options = Options(argv)
+ except SystemExit:
+ print >>sys.stderr, "usage: %s script [args]"
+ raise
+ run(options)
+
+
+def run(options):
+ reporter = FirstImportReporter()
+ hook.install_reporter(reporter)
+ globals = {"__name__": "__main__",
+ "__file__": options.argv[0]}
+ old_argv = sys.argv[:]
+ sys.argv[:] = options.argv
+ try:
+ execfile(options.script, globals)
+ finally:
+ hook.uninstall_reporter()
+ sys.argv[:] = old_argv
+ reporter.display_report()
+
+
+class Options:
+
+ def __init__(self, argv):
+ self.program = os.path.basename(argv[0])
+ if len(argv) < 2:
+ raise SystemExit(2)
+ self.argv = argv[1:]
+ self.script = self.argv[0]
+
+
+class FirstImportReporter(reporter.Reporter):
+
+ def __init__(self):
+ self.already_found = {}
+ self.index = 0
+
+ def found(self, importer, imported, fromlist):
+ if imported not in self.already_found:
+ self.already_found[imported] = importer, self.index
+ self.index += 1
+
+ def display_report(self):
+ L = [(i, imported, importer)
+ for imported, (importer, i) in self.already_found.iteritems()]
+ L.sort()
+ if not L:
+ print "---------------------"
+ print "No imports to report."
+ else:
+ left_width = right_width = 0
+ for i, imported, importer in L:
+ left_width = max(left_width, len(imported))
+ right_width = max(right_width, len(importer))
+ width = left_width + 1 + right_width
+ print width * "-"
+ for i, imported, importer in L:
+ print imported.ljust(left_width), importer
Property changes on: Zope3/trunk/src/zope/importtool/app.py
___________________________________________________________________
Name: svn:mime-type
+ text/x-python
Name: svn:eol-style
+ native
Added: Zope3/trunk/src/zope/importtool/tests/script.py
===================================================================
--- Zope3/trunk/src/zope/importtool/tests/script.py 2004-05-28 16:04:07 UTC (rev 25089)
+++ Zope3/trunk/src/zope/importtool/tests/script.py 2004-05-28 16:45:45 UTC (rev 25090)
@@ -0,0 +1,23 @@
+#!/usr/bin/env python2.3
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Sample script that gets executed by the tests.
+
+$Id$
+"""
+import sys
+print "script ran"
+print "args:", sys.argv[1:]
+print "__name__ =", __name__
+print
Property changes on: Zope3/trunk/src/zope/importtool/tests/script.py
___________________________________________________________________
Name: svn:mime-type
+ text/x-python
Name: svn:eol-style
+ native
Added: Zope3/trunk/src/zope/importtool/tests/test_app.py
===================================================================
--- Zope3/trunk/src/zope/importtool/tests/test_app.py 2004-05-28 16:04:07 UTC (rev 25089)
+++ Zope3/trunk/src/zope/importtool/tests/test_app.py 2004-05-28 16:45:45 UTC (rev 25090)
@@ -0,0 +1,96 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Tests of zope.importtool.app.
+
+$Id$
+"""
+import os
+import sys
+import unittest
+
+from StringIO import StringIO
+
+from zope.importtool import app
+
+
+class OptionsTestCase(unittest.TestCase):
+
+ def test_basic_command_line(self):
+ argv = ["foo/bar.py", "somescript", "arg1", "-opt", "arg2"]
+ opts = app.Options(argv)
+ self.assertEqual(opts.program, "bar.py")
+ self.assertEqual(opts.script, "somescript")
+ self.assertEqual(opts.argv, argv[1:])
+
+ def test_just_script_name(self):
+ argv = ["foo/bar.py", "somescript"]
+ opts = app.Options(argv)
+ self.assertEqual(opts.program, "bar.py")
+ self.assertEqual(opts.script, "somescript")
+ self.assertEqual(opts.argv, argv[1:])
+
+ def test_missing_script_name(self):
+ # This tests calls app.main() instead of app.Options() since
+ # the main() function is responsible for generating the usage
+ # message, and we want to check that there was a message on
+ # stderr.
+ old_stderr = sys.stderr
+ sys.stderr = StringIO()
+ try:
+ try:
+ app.main(["foo/bar.py"])
+ finally:
+ error_output = sys.stderr.getvalue()
+ sys.stderr = old_stderr
+ except SystemExit, e:
+ self.failUnless(error_output)
+ self.assertEqual(e.code, 2)
+ else:
+ self.fail("expected SystemExit")
+
+
+class FirstImportReporterTestCase(unittest.TestCase):
+
+ def setUp(self):
+ self.old_stdout = sys.stdout
+ self.old_stderr = sys.stderr
+ sys.stdout = StringIO()
+ sys.stderr = StringIO()
+
+ def tearDown(self):
+ sys.stdout = self.old_stdout
+ sys.stderr = self.old_stderr
+
+ def test_running_script(self):
+ here = os.path.dirname(__file__)
+ script = os.path.join(here, "script.py")
+ app.main(["foo/bar.py", script, "splat", "-opt", "surge"])
+ self.failIf(sys.stderr.getvalue())
+ self.assertEqual(sys.stdout.getvalue(),
+ EXPECTED_OUTPUT)
+
+EXPECTED_OUTPUT = """\
+script ran
+args: ['splat', '-opt', 'surge']
+__name__ = __main__
+
+------------
+sys __main__
+"""
+
+
+def test_suite():
+ suite = unittest.makeSuite(OptionsTestCase)
+ suite.addTest(unittest.makeSuite(FirstImportReporterTestCase))
+ return suite
Property changes on: Zope3/trunk/src/zope/importtool/tests/test_app.py
___________________________________________________________________
Name: svn:mime-type
+ text/x-python
Name: svn:eol-style
+ native
Added: Zope3/trunk/utilities/importtool
===================================================================
--- Zope3/trunk/utilities/importtool 2004-05-28 16:04:07 UTC (rev 25089)
+++ Zope3/trunk/utilities/importtool 2004-05-28 16:45:45 UTC (rev 25090)
@@ -0,0 +1,40 @@
+#!/usr/bin/env python2.3
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Script to search for package dependencies.
+
+$Id$
+"""
+
+import os
+import sys
+
+here = os.path.dirname(os.path.realpath(__file__))
+swhome = os.path.dirname(here)
+
+for parts in [("src",), ("lib", "python"), ("Lib", "site-packages")]:
+ d = os.path.join(swhome, *(parts + ("zope", "importtool")))
+ if os.path.isdir(d):
+ d = os.path.join(swhome, *parts)
+ sys.path.insert(0, d)
+ break
+else:
+ print >>sys.stderr, "Could not locate Zope software installation!"
+ sys.exit(1)
+
+
+from zope.importtool.app import main
+
+
+main()
Property changes on: Zope3/trunk/utilities/importtool
___________________________________________________________________
Name: svn:executable
+ *
More information about the Zope3-Checkins
mailing list