Rev 2754: (Blake Winton) BZR_PLUGIN_PATH should ignore trailiing slashes in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Tue Aug 28 06:09:24 BST 2007


At file:///home/pqm/archives/thelove/bzr/%2Btrunk/

------------------------------------------------------------
revno: 2754
revision-id: pqm at pqm.ubuntu.com-20070828050922-e7ovfulum5c7sd5o
parent: pqm at pqm.ubuntu.com-20070828035639-q7qmg7gafaevlvku
parent: ian.clatworthy at internode.on.net-20070828043410-d7vgwl5wq1ahpavt
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Tue 2007-08-28 06:09:22 +0100
message:
  (Blake Winton) BZR_PLUGIN_PATH should ignore trailiing slashes
modified:
  NEWS                           NEWS-20050323055033-4e00b5db738777ff
  bzrlib/plugin.py               plugin.py-20050622060424-829b654519533d69
  bzrlib/tests/test_plugins.py   plugins.py-20050622075746-32002b55e5e943e9
    ------------------------------------------------------------
    revno: 2753.1.1
    merged: ian.clatworthy at internode.on.net-20070828043410-d7vgwl5wq1ahpavt
    parent: pqm at pqm.ubuntu.com-20070828035639-q7qmg7gafaevlvku
    parent: bwinton at latte.ca-20070823174335-9o9dqk4rda5t1wjw
    committer: Ian Clatworthy <ian.clatworthy at internode.on.net>
    branch nick: ianc-integration
    timestamp: Tue 2007-08-28 14:34:10 +1000
    message:
      (Blake Winton) BZR_PLUGIN_PATH should ignore trailiing slashes
    ------------------------------------------------------------
    revno: 2652.2.7
    merged: bwinton at latte.ca-20070823174335-9o9dqk4rda5t1wjw
    parent: bwinton at latte.ca-20070815134320-4gx58ximngjjputp
    committer: Blake Winton <bwinton at latte.ca>
    branch nick: plugin_path
    timestamp: Thu 2007-08-23 13:43:35 -0400
    message:
      fix lines which were wider than 79 chars.  Also handle files a little more safely.
    ------------------------------------------------------------
    revno: 2652.2.6
    merged: bwinton at latte.ca-20070815134320-4gx58ximngjjputp
    parent: bwinton at latte.ca-20070809172002-hj9owx5i7d2tsoch
    committer: Blake Winton <bwinton at latte.ca>
    branch nick: plugin_path
    timestamp: Wed 2007-08-15 09:43:20 -0400
    message:
      Incorporate suggestions from Alexander Belchenko
    ------------------------------------------------------------
    revno: 2652.2.5
    merged: bwinton at latte.ca-20070809172002-hj9owx5i7d2tsoch
    parent: bwinton at latte.ca-20070809171651-r63ke3oe42cm6xtw
    committer: Blake Winton <bwinton at latte.ca>
    branch nick: plugin_path
    timestamp: Thu 2007-08-09 13:20:02 -0400
    message:
      Get rid of CRs.
    ------------------------------------------------------------
    revno: 2652.2.4
    merged: bwinton at latte.ca-20070809171651-r63ke3oe42cm6xtw
    parent: bwinton at latte.ca-20070809152827-n9snsukm6f6x5sqq
    committer: Blake Winton <bwinton at latte.ca>
    branch nick: plugin_path
    timestamp: Thu 2007-08-09 13:16:51 -0400
    message:
      Add a note explaining why I strip the slashes twice.
    ------------------------------------------------------------
    revno: 2652.2.3
    merged: bwinton at latte.ca-20070809152827-n9snsukm6f6x5sqq
    parent: bwinton at latte.ca-20070808135140-d2aesh1pwsmr3n1d
    committer: Blake Winton <bwinton at latte.ca>
    branch nick: plugin_path
    timestamp: Thu 2007-08-09 11:28:27 -0400
    message:
      Understand the code and comments of the test, instead of just cargo-culting them.
      Specifically, I'm only loading one plugin, so references to "first" and "plugins" aren't correct.
    ------------------------------------------------------------
    revno: 2652.2.2
    merged: bwinton at latte.ca-20070808135140-d2aesh1pwsmr3n1d
    parent: bwinton at latte.ca-20070808134211-yr45a8db0ob4rw3p
    committer: Blake Winton <bwinton at latte.ca>
    branch nick: plugin_path
    timestamp: Wed 2007-08-08 09:51:40 -0400
    message:
      Forgot to update NEWS
    ------------------------------------------------------------
    revno: 2652.2.1
    merged: bwinton at latte.ca-20070808134211-yr45a8db0ob4rw3p
    parent: pqm at pqm.ubuntu.com-20070725140043-22lenkarm0oc3tvx
    committer: Blake Winton <bwinton at latte.ca>
    branch nick: plugin_path
    timestamp: Wed 2007-08-08 09:42:11 -0400
    message:
      Add a test for BZR_PLUGIN_PATH, and code and another test to allow BZR_PLUGIN_PATH to contain trailing slashes.
=== modified file 'NEWS'
--- a/NEWS	2007-08-28 03:56:39 +0000
+++ b/NEWS	2007-08-28 04:34:10 +0000
@@ -39,6 +39,9 @@
     * Accept ..\ as a path in revision specifiers. This fixes for example
       "-r branch:..\other-branch" on Windows.  (Lukáš Lalinský) 
 
+    * ``BZR_PLUGIN_PATH`` may now contain trailing slashes.
+      (Blake Winton, #129299)
+
   IMPROVEMENTS:
 
     * ``pull`` and ``merge`` are much faster at installing bundle format 4.

=== modified file 'bzrlib/plugin.py'
--- a/bzrlib/plugin.py	2007-07-13 03:37:46 +0000
+++ b/bzrlib/plugin.py	2007-08-28 04:34:10 +0000
@@ -79,11 +79,16 @@
     global _loaded
     _loaded = True
 
+def _strip_trailing_sep(path):
+    return path.rstrip("\\/")
 
 def set_plugins_path():
     """Set the path for plugins to be loaded from."""
     path = os.environ.get('BZR_PLUGIN_PATH',
                           get_default_plugin_path()).split(os.pathsep)
+    # Get rid of trailing slashes, since Python can't handle them when
+    # it tries to import modules.
+    path = map(_strip_trailing_sep, path)
     # search the plugin path before the bzrlib installed dir
     path.append(os.path.dirname(plugins.__file__))
     plugins.__path__ = path
@@ -123,7 +128,12 @@
 
     The python module path for bzrlib.plugins will be modified to be 'dirs'.
     """
-    plugins.__path__ = dirs
+    # We need to strip the trailing separators here as well as in the
+    # set_plugins_path function because calling code can pass anything in to
+    # this function, and since it sets plugins.__path__, it should set it to
+    # something that will be valid for Python to use (in case people try to
+    # run "import bzrlib.plugins.PLUGINNAME" after calling this function).
+    plugins.__path__ = map(_strip_trailing_sep, dirs)
     for d in dirs:
         if not d:
             continue

=== modified file 'bzrlib/tests/test_plugins.py'
--- a/bzrlib/tests/test_plugins.py	2007-08-06 08:25:46 +0000
+++ b/bzrlib/tests/test_plugins.py	2007-08-28 04:34:10 +0000
@@ -69,8 +69,19 @@
         # tempattribute list.
         template = ("from bzrlib.tests.test_plugins import TestLoadingPlugins\n"
                     "TestLoadingPlugins.activeattributes[%r].append('%s')\n")
-        print >> file(os.path.join('first', 'plugin.py'), 'w'), template % (tempattribute, 'first')
-        print >> file(os.path.join('second', 'plugin.py'), 'w'), template % (tempattribute, 'second')
+
+        outfile = open(os.path.join('first', 'plugin.py'), 'w')
+        try:
+            print >> outfile, template % (tempattribute, 'first')
+        finally:
+            outfile.close()
+
+        outfile = open(os.path.join('second', 'plugin.py'), 'w')
+        try:
+            print >> outfile, template % (tempattribute, 'second')
+        finally:
+            outfile.close()
+
         try:
             bzrlib.plugin.load_from_path(['first', 'second'])
             self.assertEqual(['first'], self.activeattributes[tempattribute])
@@ -102,8 +113,19 @@
         # tempattribute list.
         template = ("from bzrlib.tests.test_plugins import TestLoadingPlugins\n"
                     "TestLoadingPlugins.activeattributes[%r].append('%s')\n")
-        print >> file(os.path.join('first', 'pluginone.py'), 'w'), template % (tempattribute, 'first')
-        print >> file(os.path.join('second', 'plugintwo.py'), 'w'), template % (tempattribute, 'second')
+
+        outfile = open(os.path.join('first', 'pluginone.py'), 'w')
+        try:
+            print >> outfile, template % (tempattribute, 'first')
+        finally:
+            outfile.close()
+
+        outfile = open(os.path.join('second', 'plugintwo.py'), 'w')
+        try:
+            print >> outfile, template % (tempattribute, 'second')
+        finally:
+            outfile.close()
+
         oldpath = bzrlib.plugins.__path__
         try:
             bzrlib.plugins.__path__ = ['first', 'second']
@@ -119,6 +141,42 @@
                 del bzrlib.plugins.plugin
         self.failIf(getattr(bzrlib.plugins, 'plugin', None))
 
+    def test_plugins_can_load_from_directory_with_trailing_slash(self):
+        # This test tests that a plugin can load from a directory when the
+        # directory in the path has a trailing slash.
+        # check the plugin is not loaded already
+        self.failIf(getattr(bzrlib.plugins, 'ts_plugin', None))
+        tempattribute = "trailing-slash"
+        self.failIf(tempattribute in self.activeattributes)
+        # set a place for the plugin to record its loading, and at the same
+        # time validate that the location the plugin should record to is
+        # valid and correct.
+        bzrlib.tests.test_plugins.TestLoadingPlugins.activeattributes \
+            [tempattribute] = []
+        self.failUnless(tempattribute in self.activeattributes)
+        # create a directory for the plugin
+        os.mkdir('plugin_test')
+        # write a plugin that will record when its loaded in the 
+        # tempattribute list.
+        template = ("from bzrlib.tests.test_plugins import TestLoadingPlugins\n"
+                    "TestLoadingPlugins.activeattributes[%r].append('%s')\n")
+
+        outfile = open(os.path.join('plugin_test', 'ts_plugin.py'), 'w')
+        try:
+            print >> outfile, template % (tempattribute, 'plugin')
+        finally:
+            outfile.close()
+
+        try:
+            bzrlib.plugin.load_from_path(['plugin_test'+os.sep])
+            self.assertEqual(['plugin'], self.activeattributes[tempattribute])
+        finally:
+            # remove the plugin 'plugin'
+            del self.activeattributes[tempattribute]
+            if getattr(bzrlib.plugins, 'ts_plugin', None):
+                del bzrlib.plugins.ts_plugin
+        self.failIf(getattr(bzrlib.plugins, 'ts_plugin', None))
+
 
 class TestAllPlugins(TestCaseInTempDir):
 
@@ -243,6 +301,25 @@
         finally:
             bzrlib.plugins.__path__ = old_path
 
+    def test_set_plugins_path_with_trailing_slashes(self):
+        """set_plugins_path should set the module __path__ based on
+        BZR_PLUGIN_PATH."""
+        old_path = bzrlib.plugins.__path__
+        old_env = os.environ.get('BZR_PLUGIN_PATH')
+        try:
+            bzrlib.plugins.__path__ = []
+            os.environ['BZR_PLUGIN_PATH'] = "first\\//\\" + os.pathsep + \
+                "second/\\/\\/"
+            bzrlib.plugin.set_plugins_path()
+            expected_path = ['first', 'second',
+                os.path.dirname(bzrlib.plugins.__file__)]
+            self.assertEqual(expected_path, bzrlib.plugins.__path__)
+        finally:
+            bzrlib.plugins.__path__ = old_path
+            if old_env != None:
+                os.environ['BZR_PLUGIN_PATH'] = old_env
+            else:
+                del os.environ['BZR_PLUGIN_PATH']
 
 class TestHelpIndex(tests.TestCase):
     """Tests for the PluginsHelpIndex class."""




More information about the bazaar-commits mailing list