Rev 2611: (Lukas Lalinsky) don't create a duplicate zipimporter, avoiding loading plugins twice in http://sourcefrog.net/bzr/integration

Martin Pool mbp at sourcefrog.net
Fri Jul 13 04:37:47 BST 2007


At http://sourcefrog.net/bzr/integration

------------------------------------------------------------
revno: 2611
revision-id: mbp at sourcefrog.net-20070713033746-7ujkx3jxbw2gmipl
parent: pqm at pqm.ubuntu.com-20070712133554-r8me4pz4j68p2ytf
committer: Martin Pool <mbp at sourcefrog.net>
branch nick: integration
timestamp: Fri 2007-07-13 13:37:46 +1000
message:
  (Lukas Lalinsky) don't create a duplicate zipimporter, avoiding loading plugins twice
modified:
  bzrlib/plugin.py               plugin.py-20050622060424-829b654519533d69
  bzrlib/tests/test_plugins.py   plugins.py-20050622075746-32002b55e5e943e9
=== modified file 'bzrlib/plugin.py'
--- a/bzrlib/plugin.py	2007-04-20 05:44:37 +0000
+++ b/bzrlib/plugin.py	2007-07-13 03:37:46 +0000
@@ -1,4 +1,4 @@
-# Copyright (C) 2004, 2005 Canonical Ltd
+# Copyright (C) 2004, 2005, 2007 Canonical Ltd
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -37,7 +37,7 @@
 import imp
 import re
 import types
-import zipimport
+import zipfile
 
 from bzrlib import (
     config,
@@ -194,29 +194,34 @@
     """Load all the plugins in a zip."""
     valid_suffixes = ('.py', '.pyc', '.pyo')    # only python modules/packages
                                                 # is allowed
-    if '.zip' not in zip_name:
-        return
+
     try:
-        ziobj = zipimport.zipimporter(zip_name)
-    except zipimport.ZipImportError:
-        # not a valid zip
+        index = zip_name.rindex('.zip')
+    except ValueError:
         return
+    archive = zip_name[:index+4]
+    prefix = zip_name[index+5:]
+
     mutter('Looking for plugins in %r', zip_name)
-    
-    import zipfile
 
     # use zipfile to get list of files/dirs inside zip
-    z = zipfile.ZipFile(ziobj.archive)
-    namelist = z.namelist()
-    z.close()
-    
-    if ziobj.prefix:
-        prefix = ziobj.prefix.replace('\\','/')
+    try:
+        z = zipfile.ZipFile(archive)
+        namelist = z.namelist()
+        z.close()
+    except zipfile.error:
+        # not a valid zip
+        return
+
+    if prefix:
+        prefix = prefix.replace('\\','/')
+        if prefix[-1] != '/':
+            prefix += '/'
         ix = len(prefix)
         namelist = [name[ix:]
                     for name in namelist
                     if name.startswith(prefix)]
-    
+
     mutter('Names in archive: %r', namelist)
     
     for name in namelist:
@@ -253,13 +258,8 @@
             continue
     
         try:
-            plugin = ziobj.load_module(plugin_name)
-            setattr(plugins, plugin_name, plugin)
+            exec "import bzrlib.plugins.%s" % plugin_name in {}
             mutter('Load plugin %s from zip %r', plugin_name, zip_name)
-        except zipimport.ZipImportError, e:
-            mutter('Unable to load plugin %r from %r: %s',
-                   plugin_name, zip_name, str(e))
-            continue
         except KeyboardInterrupt:
             raise
         except Exception, e:

=== modified file 'bzrlib/tests/test_plugins.py'
--- a/bzrlib/tests/test_plugins.py	2007-06-27 19:13:50 +0000
+++ b/bzrlib/tests/test_plugins.py	2007-07-13 03:37:46 +0000
@@ -208,7 +208,10 @@
     def check_plugin_load(self, zip_name, plugin_name):
         self.assertFalse(plugin_name in dir(bzrlib.plugins),
                          'Plugin already loaded')
+        old_path = bzrlib.plugins.__path__
         try:
+            # this is normally done by load_plugins -> set_plugins_path
+            bzrlib.plugins.__path__ = [zip_name]
             bzrlib.plugin.load_from_zip(zip_name)
             self.assertTrue(plugin_name in dir(bzrlib.plugins),
                             'Plugin is not loaded')
@@ -216,6 +219,8 @@
             # unregister plugin
             if getattr(bzrlib.plugins, plugin_name, None):
                 delattr(bzrlib.plugins, plugin_name)
+                del sys.modules['bzrlib.plugins.' + plugin_name]
+            bzrlib.plugins.__path__ = old_path
 
     def test_load_module(self):
         self.make_zipped_plugin('./test.zip', 'ziplug.py')




More information about the bazaar-commits mailing list