[Zope3-checkins] SVN: Zope3/trunk/src/zope/dependencytool/ - move some general code into helper functions

Fred L. Drake, Jr. fred at zope.com
Fri May 21 17:22:20 EDT 2004


Log message for revision 24865:
- move some general code into helper functions
- make the ZCML handler properly support the -p/--packages option



-=-
Modified: Zope3/trunk/src/zope/dependencytool/finddeps.py
===================================================================
--- Zope3/trunk/src/zope/dependencytool/finddeps.py	2004-05-21 20:46:47 UTC (rev 24864)
+++ Zope3/trunk/src/zope/dependencytool/finddeps.py	2004-05-21 21:22:20 UTC (rev 24865)
@@ -52,7 +52,7 @@
 import zope
 
 from zope.dependencytool.dependency import Dependency
-from zope.dependencytool.importfinder import ImportFinder
+from zope.dependencytool import importfinder
 
 
 # Get the Zope base path
@@ -102,7 +102,7 @@
 
 
 def getDependenciesOfPythonFile(path, packages):
-    finder = ImportFinder(packages)
+    finder = importfinder.ImportFinder(packages)
     module_name = makeDottedName(path)
     if '.' in module_name:
         package = module_name[:module_name.rfind('.')]
@@ -128,10 +128,9 @@
             for name in match:
                 if name.startswith('.'):
                     name = localPackage + name
-                try:
-                    __import__(name)
-                except:
-                    continue
+                name = importfinder.module_for_importable(name)
+                if packages:
+                    name = importfinder.package_for_module(name)
                 deps.append(Dependency(name, path, lineno))
     return deps
 

Modified: Zope3/trunk/src/zope/dependencytool/importfinder.py
===================================================================
--- Zope3/trunk/src/zope/dependencytool/importfinder.py	2004-05-21 20:46:47 UTC (rev 24864)
+++ Zope3/trunk/src/zope/dependencytool/importfinder.py	2004-05-21 21:22:20 UTC (rev 24865)
@@ -104,15 +104,11 @@
         # command-line scripts), so we need to filter that out.
         if self.module_checks[name] and name != "__main__":
             if self.packages:
-                __import__(name)
-                module = sys.modules[name]
-                if not hasattr(module, "__path__"):
-                    if "." in name:
-                        name = name[:name.rfind(".")]
-                    else:
-                        # just drop it on the floor, since we're not
-                        # interested in bare modules
-                        return
+                name = package_for_module(name)
+                if name is None:
+                    # just drop it on the floor, since we're not
+                    # interested in bare modules
+                    return
             self.deps.append(Dependency(name, self.path, lineno))
 
     def check_module_name(self, name):
@@ -214,3 +210,33 @@
         self.state = self.post_name_state
         self.post_name_state = None
         self.transition(type, string, lineno)
+
+
+def package_for_module(name):
+    """Return the package name for the module named `name`."""
+    __import__(name)
+    module = sys.modules[name]
+    if not hasattr(module, "__path__"):
+        if "." in name:
+            name = name[:name.rfind(".")]
+        else:
+            name = None
+    return name
+
+
+def module_for_importable(name):
+    """Return the module name for the importable object `name`."""
+    try:
+        __import__(name)
+    except ImportError:
+        while "." in name:
+            name = name[:name.rfind(".")]
+            try:
+                __import__(name)
+            except ImportError:
+                pass
+            else:
+                break
+        else:
+            return None
+    return name

Modified: Zope3/trunk/src/zope/dependencytool/tests/test_importfinder.py
===================================================================
--- Zope3/trunk/src/zope/dependencytool/tests/test_importfinder.py	2004-05-21 20:46:47 UTC (rev 24864)
+++ Zope3/trunk/src/zope/dependencytool/tests/test_importfinder.py	2004-05-21 21:22:20 UTC (rev 24865)
@@ -18,7 +18,7 @@
 import os
 import unittest
 
-from zope.dependencytool.importfinder import ImportFinder
+from zope.dependencytool import importfinder
 
 
 here = os.path.dirname(__file__)
@@ -29,7 +29,7 @@
 class ImportFinderTestCase(unittest.TestCase):
 
     def test_relative_imports(self):
-        finder = ImportFinder()
+        finder = importfinder.ImportFinder()
         path = os.path.join(here, "sample.py")
         f = open(path, "rU")
         try:
@@ -38,9 +38,45 @@
             f.close()
         imports = finder.get_imports()
         self.assertEqual(len(imports), 1)
-        self.assertEqual(imports[0].name,
-                         "%s.pkg.module" % THIS_PACKAGE)
+        self.assertEqual(imports[0].name, "%s.pkg.module" % THIS_PACKAGE)
 
+    def test_relative_imports_for_packages(self):
+        finder = importfinder.ImportFinder(packages=True)
+        path = os.path.join(here, "sample.py")
+        f = open(path, "rU")
+        try:
+            finder.find_imports(f, path, THIS_PACKAGE)
+        finally:
+            f.close()
+        imports = finder.get_imports()
+        self.assertEqual(len(imports), 1)
+        self.assertEqual(imports[0].name, "%s.pkg" % THIS_PACKAGE)
 
+    def test_package_for_module(self):
+        self.assertEqual(
+            importfinder.package_for_module(__name__),
+            THIS_PACKAGE)
+        self.assertEqual(
+            importfinder.package_for_module("os"),
+            None)
+        self.assertEqual(
+            importfinder.package_for_module("distutils.sysconfig"),
+            "distutils")
+
+    def test_module_for_importable(self):
+        clsname = __name__ + ".ImportFinderTestCase"
+        self.assertEqual(
+            importfinder.module_for_importable(clsname), __name__)
+        self.assertEqual(
+            importfinder.module_for_importable("os.path.isdir"), "os.path")
+        # check when we ask about a module:
+        self.assertEqual(
+            importfinder.module_for_importable(__name__), __name__)
+        # and a method of a class:
+        methodname = clsname + ".test_module_for_importable"
+        self.assertEqual(
+            importfinder.module_for_importable(methodname), __name__)
+
+
 def test_suite():
     return unittest.makeSuite(ImportFinderTestCase)




More information about the Zope3-Checkins mailing list