Rev 5737: (jelmer) Add ControlDir.check_support_status() which can print upgrade in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Wed Mar 23 15:44:03 UTC 2011


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

------------------------------------------------------------
revno: 5737 [merge]
revision-id: pqm at pqm.ubuntu.com-20110323154359-2air2wty7svt2p92
parent: pqm at pqm.ubuntu.com-20110323150313-b058qpny5xns1ztt
parent: jelmer at samba.org-20110323143138-vdx9ije9zhl2oujp
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Wed 2011-03-23 15:43:59 +0000
message:
  (jelmer) Add ControlDir.check_support_status() which can print upgrade
   recommendations and raise incompatible format errors. (Jelmer Vernooij)
added:
  bzrlib/tests/per_controldir/test_format.py test_format.py-20110312004133-k0plu1qapsfxadfd-1
modified:
  bzrlib/bzrdir.py               bzrdir.py-20060131065624-156dfea39c4387cb
  bzrlib/controldir.py           controldir.py-20100802102926-hvtvh0uae5epuibp-1
  bzrlib/plugins/weave_fmt/bzrdir.py bzrdir_weave.py-20110310114200-ndz63gzqll03nf4z-1
  bzrlib/tests/per_controldir/__init__.py __init__.py-20060131065642-34c39b54f42dd048
  bzrlib/tests/test_controldir.py test_controldir.py-20110224120830-peu3bobygfcfsilp-1
  bzrlib/ui/__init__.py          ui.py-20050824083933-8cf663c763ba53a9
  doc/en/release-notes/bzr-2.4.txt bzr2.4.txt-20110114053217-k7ym9jfz243fddjm-1
=== modified file 'bzrlib/bzrdir.py'
--- a/bzrlib/bzrdir.py	2011-03-12 21:22:14 +0000
+++ b/bzrlib/bzrdir.py	2011-03-22 12:11:20 +0000
@@ -122,34 +122,6 @@
             # No repo, no problem.
             pass
 
-    @staticmethod
-    def _check_supported(format, allow_unsupported,
-        recommend_upgrade=True,
-        basedir=None):
-        """Give an error or warning on old formats.
-
-        :param format: may be any kind of format - workingtree, branch,
-        or repository.
-
-        :param allow_unsupported: If true, allow opening
-        formats that are strongly deprecated, and which may
-        have limited functionality.
-
-        :param recommend_upgrade: If true (default), warn
-        the user through the ui object that they may wish
-        to upgrade the object.
-        """
-        # TODO: perhaps move this into a base Format class; it's not BzrDir
-        # specific. mbp 20070323
-        if not allow_unsupported and not format.is_supported():
-            # see open_downlevel to open legacy branches.
-            raise errors.UnsupportedFormatError(format=format)
-        if recommend_upgrade \
-            and getattr(format, 'upgrade_recommended', False):
-            ui.ui_factory.recommend_upgrade(
-                format.get_format_description(),
-                basedir)
-
     def clone_on_transport(self, transport, revision_id=None,
         force_new_repo=False, preserve_stacking=False, stacked_on=None,
         create_prefix=False, use_existing_dir=True, no_tree=False):
@@ -729,7 +701,7 @@
         except errors.TooManyRedirections:
             raise errors.NotBranchError(base)
 
-        BzrDir._check_supported(format, _unsupported)
+        format.check_support_status(_unsupported)
         return format.open(transport, _found=True)
 
     @staticmethod
@@ -1169,7 +1141,7 @@
                     ignore_fallbacks=False):
         """See BzrDir.open_branch."""
         format = self.find_branch_format(name=name)
-        self._check_supported(format, unsupported)
+        format.check_support_status(unsupported)
         return format.open(self, name=name,
             _found=True, ignore_fallbacks=ignore_fallbacks)
 
@@ -1177,7 +1149,7 @@
         """See BzrDir.open_repository."""
         from bzrlib.repository import RepositoryFormat
         format = RepositoryFormat.find_format(self)
-        self._check_supported(format, unsupported)
+        format.check_support_status(unsupported)
         return format.open(self, _found=True)
 
     def open_workingtree(self, unsupported=False,
@@ -1185,8 +1157,7 @@
         """See BzrDir.open_workingtree."""
         from bzrlib.workingtree import WorkingTreeFormat
         format = WorkingTreeFormat.find_format(self)
-        self._check_supported(format, unsupported,
-            recommend_upgrade,
+        format.check_support_status(unsupported, recommend_upgrade,
             basedir=self.root_transport.base)
         return format.open(self, _found=True)
 

=== modified file 'bzrlib/controldir.py'
--- a/bzrlib/controldir.py	2011-03-23 05:15:48 +0000
+++ b/bzrlib/controldir.py	2011-03-23 14:31:38 +0000
@@ -32,6 +32,7 @@
     fetch,
     revision as _mod_revision,
     transport as _mod_transport,
+    ui,
     urlutils,
     )
 from bzrlib.push import (
@@ -82,6 +83,7 @@
         return self.user_transport.base
 
 
+
 class ControlDir(ControlComponent):
     """A control directory.
 
@@ -597,6 +599,8 @@
 class ControlComponentFormat(object):
     """A component that can live inside of a .bzr meta directory."""
 
+    upgrade_recommended = False
+
     def get_format_string(self):
         """Return the format of this format, if usable in meta directories."""
         raise NotImplementedError(self.get_format_string)
@@ -605,6 +609,34 @@
         """Return the short description for this format."""
         raise NotImplementedError(self.get_format_description)
 
+    def is_supported(self):
+        """Is this format supported?
+
+        Supported formats must be initializable and openable.
+        Unsupported formats may not support initialization or committing or
+        some other features depending on the reason for not being supported.
+        """
+        return True
+
+    def check_support_status(self, allow_unsupported, recommend_upgrade=True,
+        basedir=None):
+        """Give an error or warning on old formats.
+
+        :param allow_unsupported: If true, allow opening
+            formats that are strongly deprecated, and which may
+            have limited functionality.
+
+        :param recommend_upgrade: If true (default), warn
+            the user through the ui object that they may wish
+            to upgrade the object.
+        """
+        if not allow_unsupported and not self.is_supported():
+            # see open_downlevel to open legacy branches.
+            raise errors.UnsupportedFormatError(format=self)
+        if recommend_upgrade and self.upgrade_recommended:
+            ui.ui_factory.recommend_upgrade(
+                self.get_format_description(), basedir)
+
 
 class ControlComponentFormatRegistry(registry.FormatRegistry):
     """A registry for control components (branch, workingtree, repository)."""
@@ -737,6 +769,9 @@
     """Whether components can not change format independent of the control dir.
     """
 
+    upgrade_recommended = False
+    """Whether an upgrade from this format is recommended."""
+
     def get_format_description(self):
         """Return the short description for this format."""
         raise NotImplementedError(self.get_format_description)
@@ -764,6 +799,25 @@
         """
         return True
 
+    def check_support_status(self, allow_unsupported, recommend_upgrade=True,
+        basedir=None):
+        """Give an error or warning on old formats.
+
+        :param allow_unsupported: If true, allow opening
+            formats that are strongly deprecated, and which may
+            have limited functionality.
+
+        :param recommend_upgrade: If true (default), warn
+            the user through the ui object that they may wish
+            to upgrade the object.
+        """
+        if not allow_unsupported and not self.is_supported():
+            # see open_downlevel to open legacy branches.
+            raise errors.UnsupportedFormatError(format=self)
+        if recommend_upgrade and self.upgrade_recommended:
+            ui.ui_factory.recommend_upgrade(
+                self.get_format_description(), basedir)
+
     def same_model(self, target_format):
         return (self.repository_format.rich_root_data ==
             target_format.rich_root_data)

=== modified file 'bzrlib/plugins/weave_fmt/bzrdir.py'
--- a/bzrlib/plugins/weave_fmt/bzrdir.py	2011-03-11 15:36:12 +0000
+++ b/bzrlib/plugins/weave_fmt/bzrdir.py	2011-03-23 13:26:59 +0000
@@ -863,7 +863,7 @@
         """See BzrDir.open_branch."""
         from bzrlib.plugins.weave_fmt.branch import BzrBranchFormat4
         format = BzrBranchFormat4()
-        self._check_supported(format, unsupported)
+        format.check_support_status(unsupported)
         return format.open(self, name, _found=True)
 
     def sprout(self, url, revision_id=None, force_new_repo=False,

=== modified file 'bzrlib/tests/per_controldir/__init__.py'
--- a/bzrlib/tests/per_controldir/__init__.py	2011-01-27 13:58:35 +0000
+++ b/bzrlib/tests/per_controldir/__init__.py	2011-03-12 14:39:47 +0000
@@ -77,6 +77,7 @@
 def load_tests(standard_tests, module, loader):
     test_per_controldir = [
         'bzrlib.tests.per_controldir.test_controldir',
+        'bzrlib.tests.per_controldir.test_format',
         'bzrlib.tests.per_controldir.test_push',
         ]
     submod_tests = loader.loadTestsFromModuleNames(test_per_controldir)

=== added file 'bzrlib/tests/per_controldir/test_format.py'
--- a/bzrlib/tests/per_controldir/test_format.py	1970-01-01 00:00:00 +0000
+++ b/bzrlib/tests/per_controldir/test_format.py	2011-03-22 12:10:34 +0000
@@ -0,0 +1,44 @@
+# Copyright (C) 2011 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
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+"""Tests for control directory formats."""
+
+from bzrlib import (
+    errors,
+    )
+
+from bzrlib.tests.per_controldir import TestCaseWithControlDir
+
+
+class TestControlDir(TestCaseWithControlDir):
+
+    def test_get_format_description(self):
+        self.assertIsInstance(self.bzrdir_format.get_format_description(),
+                str)
+
+    def test_is_supported(self):
+        self.assertIsInstance(self.bzrdir_format.is_supported(), bool)
+
+    def test_upgrade_recommended(self):
+        self.assertIsInstance(self.bzrdir_format.upgrade_recommended, bool)
+
+    def test_check_support_status(self):
+        if not self.bzrdir_format.is_supported():
+            self.assertRaises(errors.UnsupportedFormatError,
+                self.bzrdir_format.check_support_status, False)
+        else:
+            self.bzrdir_format.check_support_status(True)
+            self.bzrdir_format.check_support_status(False)

=== modified file 'bzrlib/tests/test_controldir.py'
--- a/bzrlib/tests/test_controldir.py	2011-03-11 14:14:37 +0000
+++ b/bzrlib/tests/test_controldir.py	2011-03-22 12:10:34 +0000
@@ -23,6 +23,7 @@
     controldir,
     errors,
     tests,
+    ui,
     )
 from bzrlib.tests.scenarios import load_tests_apply_scenarios
 
@@ -180,3 +181,57 @@
                 break
         else:
             self.fail("No NotBzrDirFormat in %s" % formats)
+
+
+class UnsupportedControlComponentFormat(controldir.ControlComponentFormat):
+
+    def is_supported(self):
+        return False
+
+
+class OldControlComponentFormat(controldir.ControlComponentFormat):
+
+    def get_format_description(self):
+        return "An old format that is slow"
+
+    upgrade_recommended = True
+
+
+class DefaultControlComponentFormatTests(tests.TestCase):
+    """Tests for default ControlComponentFormat implementation."""
+
+    def test_check_support_status_unsupported(self):
+        self.assertRaises(errors.UnsupportedFormatError,
+            UnsupportedControlComponentFormat().check_support_status,
+            allow_unsupported=False)
+        UnsupportedControlComponentFormat().check_support_status(
+            allow_unsupported=True)
+
+    def test_check_support_status_supported(self):
+        controldir.ControlComponentFormat().check_support_status(
+            allow_unsupported=False)
+        controldir.ControlComponentFormat().check_support_status(
+            allow_unsupported=True)
+
+    def test_recommend_upgrade_current_format(self):
+        stderr = tests.StringIOWrapper()
+        ui.ui_factory = tests.TestUIFactory(stderr=stderr)
+        format = controldir.ControlComponentFormat()
+        format.check_support_status(allow_unsupported=False,
+            recommend_upgrade=True)
+        self.assertEquals("", stderr.getvalue())
+
+    def test_recommend_upgrade_old_format(self):
+        stderr = tests.StringIOWrapper()
+        ui.ui_factory = tests.TestUIFactory(stderr=stderr)
+        format = OldControlComponentFormat()
+        format.check_support_status(allow_unsupported=False,
+            recommend_upgrade=False)
+        self.assertEquals("", stderr.getvalue())
+        format.check_support_status(allow_unsupported=False,
+            recommend_upgrade=True, basedir='apath')
+        self.assertEquals(
+            'An old format that is slow is deprecated and a better format '
+            'is available.\nIt is recommended that you upgrade by running '
+            'the command\n  bzr upgrade apath\n',
+            stderr.getvalue())

=== modified file 'bzrlib/ui/__init__.py'
--- a/bzrlib/ui/__init__.py	2010-09-14 06:46:18 +0000
+++ b/bzrlib/ui/__init__.py	2011-03-12 17:06:27 +0000
@@ -154,7 +154,12 @@
             "%(from_format)s to %(to_format)s.\n"
             "This may take some time. Upgrade the repositories to the "
             "same format for better performance."
-            )
+            ),
+        recommend_upgrade=("%(current_format_name)s is deprecated "
+            "and a better format is available.\n"
+            "It is recommended that you upgrade by "
+            "running the command\n"
+            "  bzr upgrade %(basedir)s"),
         )
 
     def __init__(self):
@@ -343,21 +348,14 @@
         """
         return NullProgressView()
 
-    def recommend_upgrade(self,
-        current_format_name,
-        basedir):
-        # XXX: this should perhaps be in the TextUIFactory and the default can do
-        # nothing
-        #
-        # XXX: Change to show_user_warning - that will accomplish the previous
-        # xxx. -- mbp 2010-02-25
-        trace.warning("%s is deprecated "
-            "and a better format is available.\n"
-            "It is recommended that you upgrade by "
-            "running the command\n"
-            "  bzr upgrade %s",
-            current_format_name,
-            basedir)
+    def recommend_upgrade(self, current_format_name, basedir):
+        """Recommend the user upgrade a control directory.
+
+        :param current_format_name: Description of the current format
+        :param basedir: Location of the control dir
+        """
+        self.show_user_warning('recommend_upgrade',
+            current_format_name=current_format_name, basedir=basedir)
 
     def report_transport_activity(self, transport, byte_count, direction):
         """Called by transports as they do IO.

=== modified file 'doc/en/release-notes/bzr-2.4.txt'
--- a/doc/en/release-notes/bzr-2.4.txt	2011-03-22 16:39:39 +0000
+++ b/doc/en/release-notes/bzr-2.4.txt	2011-03-23 12:56:01 +0000
@@ -234,6 +234,10 @@
   ``Prober`` implementations should now implement a ``known_formats``
   method. (Jelmer Vernooij)
 
+* ControlDirFormats can now provide a ``check_status`` method and
+  raise a custom exception or warning when an unsupported or deprecated
+  format is being opened.  (Jelmer Vernooij, #731311)
+
 * ``bzrlib.revionspec.dwim_revspecs`` is deprecated.
   Use ``bzrlib.revisionspec.RevisionSpec_dwim.append_possible_revspec`` and
   ``bzrlib.revisionspec.RevisionSpec_dwim.append_possible_lazy_revspec``




More information about the bazaar-commits mailing list