Rev 4629: Start introducing accessors for plugin paths. in file:///home/vila/src/bzr/bugs/412930-plugin-path/

Vincent Ladeuil v.ladeuil+lp at free.fr
Wed Aug 19 18:01:07 BST 2009


At file:///home/vila/src/bzr/bugs/412930-plugin-path/

------------------------------------------------------------
revno: 4629
revision-id: v.ladeuil+lp at free.fr-20090819170106-v652tu3p0mqnqs09
parent: pqm at pqm.ubuntu.com-20090819030231-xjwlii48wpfy8qui
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: 412930-plugin-path
timestamp: Wed 2009-08-19 19:01:06 +0200
message:
  Start introducing accessors for plugin paths.
  
  * bzrlib/tests/test_plugins.py:
  (clear_plugins): Deleted (or rather turned into a proper setUp).
  (TestLoadFromPath): Use a proper setUp.
  (TestLoadPlugins): Merged into TestLoadFromPath.
  (TestEnvPluginPath): Start simple tests for path composition.
  
  * bzrlib/plugin.py:
  (_append_new_path): New helper.
  (get_core_plugin_path, get_site_plugin_path,
  get_user_plugin_path): New accessors.
-------------- next part --------------
=== modified file 'bzrlib/plugin.py'
--- a/bzrlib/plugin.py	2009-03-24 01:53:42 +0000
+++ b/bzrlib/plugin.py	2009-08-19 17:01:06 +0000
@@ -91,6 +91,52 @@
     return path
 
 
+def _append_new_path(paths, new_path):
+    """Append a new path if it set and not already known."""
+    if new_path is not None and new_path not in paths:
+        paths.append(new_path)
+    return paths
+
+
+def get_core_plugin_path():
+    core_path = None
+    bzr_exe = bool(getattr(sys, 'frozen', None))
+    if bzr_exe:    # expand path for bzr.exe
+        # We need to use relative path to system-wide plugin
+        # directory because bzrlib from standalone bzr.exe
+        # could be imported by another standalone program
+        # (e.g. bzr-config; or TortoiseBzr/Olive if/when they
+        # will become standalone exe). [bialix 20071123]
+        # __file__ typically is
+        # C:\Program Files\Bazaar\lib\library.zip\bzrlib\plugin.pyc
+        # then plugins directory is
+        # C:\Program Files\Bazaar\plugins
+        # so relative path is ../../../plugins
+        core_path = osutils.abspath(osutils.pathjoin(
+                osutils.dirname(__file__), '../../../plugins'))
+    else:     # don't look inside library.zip
+        # search the plugin path before the bzrlib installed dir
+        core_path = os.path.dirname(_mod_plugins.__file__)
+    return core_path
+
+
+def get_site_plugin_path():
+    """Returns the path for the site installed plugins."""
+    site_path = None
+    try:
+        from distutils.sysconfig import get_python_lib
+    except ImportError:
+        # If distutuils is not available, we just don't know where they are
+        pass
+    else:
+        site_path = osutils.pathjoin(get_python_lib(), 'bzrlib', 'plugins')
+    return site_path
+
+
+def get_user_plugin_path():
+    return osutils.pathjoin(config.config_dir(), 'plugins')
+
+
 def get_standard_plugins_path():
     """Determine a plugin path suitable for general use."""
     path = os.environ.get('BZR_PLUGIN_PATH',

=== modified file 'bzrlib/tests/test_plugins.py'
--- a/bzrlib/tests/test_plugins.py	2009-06-10 03:56:49 +0000
+++ b/bzrlib/tests/test_plugins.py	2009-08-19 17:01:06 +0000
@@ -597,34 +597,36 @@
         self.assertEqual('foo_bar', topic.get_help_topic())
 
 
-def clear_plugins(test_case):
-    # Save the attributes that we're about to monkey-patch.
-    old_plugins_path = bzrlib.plugins.__path__
-    old_loaded = plugin._loaded
-    old_load_from_path = plugin.load_from_path
-    # Change bzrlib.plugin to think no plugins have been loaded yet.
-    bzrlib.plugins.__path__ = []
-    plugin._loaded = False
-    # Monkey-patch load_from_path to stop it from actually loading anything.
-    def load_from_path(dirs):
-        pass
-    plugin.load_from_path = load_from_path
-    def restore_plugins():
-        bzrlib.plugins.__path__ = old_plugins_path
-        plugin._loaded = old_loaded
-        plugin.load_from_path = old_load_from_path
-    test_case.addCleanup(restore_plugins)
-
-
-class TestPluginPaths(tests.TestCase):
+class TestLoadFromPath(tests.TestCaseInTempDir):
+
+    def setUp(self):
+        super(TestLoadFromPath, self).setUp()
+        # Save the attributes that we're about to monkey-patch.
+        old_plugins_path = bzrlib.plugins.__path__
+        old_loaded = plugin._loaded
+        old_load_from_path = plugin.load_from_path
+
+        def restore():
+            bzrlib.plugins.__path__ = old_plugins_path
+            plugin._loaded = old_loaded
+            plugin.load_from_path = old_load_from_path
+
+        self.addCleanup(restore)
+
+        # Change bzrlib.plugin to think no plugins have been loaded yet.
+        bzrlib.plugins.__path__ = []
+        plugin._loaded = False
+
+        # Monkey-patch load_from_path to stop it from actually loading anything.
+        def load_from_path(dirs):
+            pass
+        plugin.load_from_path = load_from_path
 
     def test_set_plugins_path_with_args(self):
-        clear_plugins(self)
         plugin.set_plugins_path(['a', 'b'])
         self.assertEqual(['a', 'b'], bzrlib.plugins.__path__)
 
     def test_set_plugins_path_defaults(self):
-        clear_plugins(self)
         plugin.set_plugins_path()
         self.assertEqual(plugin.get_standard_plugins_path(),
                          bzrlib.plugins.__path__)
@@ -651,11 +653,7 @@
         os.environ['BZR_PLUGIN_PATH'] = 'foo/'
         self.assertEqual('foo', plugin.get_standard_plugins_path()[0])
 
-
-class TestLoadPlugins(tests.TestCaseInTempDir):
-
     def test_load_plugins(self):
-        clear_plugins(self)
         plugin.load_plugins(['.'])
         self.assertEqual(bzrlib.plugins.__path__, ['.'])
         # subsequent loads are no-ops
@@ -663,7 +661,35 @@
         self.assertEqual(bzrlib.plugins.__path__, ['.'])
 
     def test_load_plugins_default(self):
-        clear_plugins(self)
         plugin.load_plugins()
         path = plugin.get_standard_plugins_path()
         self.assertEqual(path, bzrlib.plugins.__path__)
+
+
+class TestEnvPluginPath(tests.TestCaseInTempDir):
+
+    def setUp(self):
+        super(TestEnvPluginPath, self).setUp()
+        old_default = plugin.DEFAULT_PLUGIN_PATH
+
+        def restore():
+            plugin.DEFAULT_PLUGIN_PATH = old_default
+
+        self.addCleanup(restore)
+
+        plugin.DEFAULT_PLUGIN_PATH = None
+
+        self.user = plugin.get_user_plugin_path()
+        self.site = plugin.get_site_plugin_path()
+        self.core = plugin.get_core_plugin_path()
+
+    def _list2path(self, *args):
+        paths = []
+        for p in args:
+            plugin._append_new_path(paths, p)
+        return paths
+
+    def test_default(self):
+        self.assertEquals(self._list2path(self.user, self.core, self.site),
+                          plugin.get_standard_plugins_path())
+



More information about the bazaar-commits mailing list