Rev 5658: (jelmer) Allow registration of extra repository formats. (Jelmer Vernooij) in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Thu Feb 10 16:31:13 UTC 2011


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

------------------------------------------------------------
revno: 5658 [merge]
revision-id: pqm at pqm.ubuntu.com-20110210163054-0fv1ac5k18e8859h
parent: pqm at pqm.ubuntu.com-20110209181710-h55d74ahkew1tkhl
parent: jelmer at samba.org-20110210151332-5jcn9i8pm5etrmmo
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Thu 2011-02-10 16:30:54 +0000
message:
  (jelmer) Allow registration of extra repository formats. (Jelmer Vernooij)
modified:
  bzrlib/bzrdir.py               bzrdir.py-20060131065624-156dfea39c4387cb
  bzrlib/repofmt/weaverepo.py    presplitout.py-20070125045333-wfav3tsh73oxu3zk-1
  bzrlib/repository.py           rev_storage.py-20051111201905-119e9401e46257e3
  bzrlib/tests/per_interrepository/__init__.py __init__.py-20060220054744-baf49a1f88f17b1a
  bzrlib/tests/per_repository/__init__.py __init__.py-20060131092037-9564957a7d4a841b
  bzrlib/tests/test_bzrdir.py    test_bzrdir.py-20060131065654-deba40eef51cf220
  bzrlib/tests/test_remote.py    test_remote.py-20060720103555-yeeg2x51vn0rbtdp-2
  bzrlib/tests/test_repository.py test_repository.py-20060131075918-65c555b881612f4d
  doc/en/release-notes/bzr-2.4.txt bzr2.4.txt-20110114053217-k7ym9jfz243fddjm-1
=== modified file 'bzrlib/bzrdir.py'
--- a/bzrlib/bzrdir.py	2011-01-27 15:58:36 +0000
+++ b/bzrlib/bzrdir.py	2011-02-08 12:48:44 +0000
@@ -2102,8 +2102,8 @@
         """Circular import protection."""
         if self._repository_format:
             return self._repository_format
-        from bzrlib.repository import RepositoryFormat
-        return RepositoryFormat.get_default_format()
+        from bzrlib.repository import format_registry
+        return format_registry.get_default()
 
     def _set_repository_format(self, value):
         """Allow changing the repository format for metadir formats."""

=== modified file 'bzrlib/repofmt/weaverepo.py'
--- a/bzrlib/repofmt/weaverepo.py	2011-02-07 04:14:29 +0000
+++ b/bzrlib/repofmt/weaverepo.py	2011-02-08 13:34:52 +0000
@@ -859,9 +859,4 @@
         return self.source.revision_ids_to_search_result(result_set)
 
 
-_legacy_formats = [RepositoryFormat4(),
-                   RepositoryFormat5(),
-                   RepositoryFormat6()]
-
-
 InterRepository.register_optimiser(InterWeaveRepo)

=== modified file 'bzrlib/repository.py'
--- a/bzrlib/repository.py	2011-02-07 04:30:00 +0000
+++ b/bzrlib/repository.py	2011-02-08 15:41:44 +0000
@@ -3004,6 +3004,62 @@
             control_files)
 
 
+class RepositoryFormatRegistry(registry.FormatRegistry):
+    """Repository format registry."""
+
+    def __init__(self, other_registry=None):
+        super(RepositoryFormatRegistry, self).__init__(other_registry)
+        self._extra_formats = []
+
+    def register(self, format):
+        """Register a new repository format."""
+        super(RepositoryFormatRegistry, self).register(
+            format.get_format_string(), format)
+
+    def remove(self, format):
+        """Remove a registered repository format."""
+        super(RepositoryFormatRegistry, self).remove(
+            format.get_format_string())
+
+    def register_extra(self, format):
+        """Register a repository format that can not be used in a metadir.
+
+        This is mainly useful to allow custom repository formats, such as older
+        Bazaar formats and foreign formats, to be tested.
+        """
+        self._extra_formats.append(registry._ObjectGetter(format))
+
+    def remove_extra(self, format):
+        """Remove an extra repository format.
+        """
+        self._extra_formats.remove(registry._ObjectGetter(format))
+
+    def register_extra_lazy(self, module_name, member_name):
+        """Register a repository format lazily.
+        """
+        self._extra_formats.append(
+            registry._LazyObjectGetter(module_name, member_name))
+
+    def get_default(self):
+        """Return the current default format."""
+        from bzrlib import bzrdir
+        return bzrdir.format_registry.make_bzrdir('default').repository_format
+
+    def _get_extra(self):
+        result = []
+        for getter in self._extra_formats:
+            f = getter.get_obj()
+            if callable(f):
+                f = f()
+            result.append(f)
+        return result
+
+    def _get_all(self):
+        """Return all repository formats, even those not usable in metadirs.
+        """
+        return [self.get(k) for k in self.keys()] + self._get_extra()
+
+
 network_format_registry = registry.FormatRegistry()
 """Registry of formats indexed by their network name.
 
@@ -3013,7 +3069,7 @@
 """
 
 
-format_registry = registry.FormatRegistry(network_format_registry)
+format_registry = RepositoryFormatRegistry(network_format_registry)
 """Registry of formats, indexed by their BzrDirMetaFormat format string.
 
 This can contain either format instances themselves, or classes/factories that
@@ -3124,18 +3180,20 @@
                                             kind='repository')
 
     @classmethod
+    @symbol_versioning.deprecated_method(symbol_versioning.deprecated_in((2, 4, 0)))
     def register_format(klass, format):
-        format_registry.register(format.get_format_string(), format)
+        format_registry.register(format)
 
     @classmethod
+    @symbol_versioning.deprecated_method(symbol_versioning.deprecated_in((2, 4, 0)))
     def unregister_format(klass, format):
-        format_registry.remove(format.get_format_string())
+        format_registry.remove(format)
 
     @classmethod
+    @symbol_versioning.deprecated_method(symbol_versioning.deprecated_in((2, 4, 0)))
     def get_default_format(klass):
         """Return the current default format."""
-        from bzrlib import bzrdir
-        return bzrdir.format_registry.make_bzrdir('default').repository_format
+        return format_registry.get_default()
 
     def get_format_string(self):
         """Return the ASCII format string that identifies this format.
@@ -3294,6 +3352,16 @@
     'RepositoryFormat6',
 )
 
+format_registry.register_extra_lazy(
+    'bzrlib.repofmt.weaverepo',
+    'RepositoryFormat4')
+format_registry.register_extra_lazy(
+    'bzrlib.repofmt.weaverepo',
+    'RepositoryFormat5')
+format_registry.register_extra_lazy(
+    'bzrlib.repofmt.weaverepo',
+    'RepositoryFormat6')
+
 # formats which have no format string are not discoverable or independently
 # creatable on disk, so are not registered in format_registry.  They're
 # all in bzrlib.repofmt.weaverepo now.  When an instance of one of these is

=== modified file 'bzrlib/tests/per_interrepository/__init__.py'
--- a/bzrlib/tests/per_interrepository/__init__.py	2010-11-22 03:35:24 +0000
+++ b/bzrlib/tests/per_interrepository/__init__.py	2011-02-08 12:15:41 +0000
@@ -36,9 +36,10 @@
     InterRepository,
     )
 from bzrlib.tests import (
-                          default_transport,
-                          multiply_tests,
-                          )
+    TestSkipped,
+    default_transport,
+    multiply_tests,
+    )
 from bzrlib.tests.per_controldir.test_controldir import TestCaseWithControlDir
 
 

=== modified file 'bzrlib/tests/per_repository/__init__.py'
--- a/bzrlib/tests/per_repository/__init__.py	2010-08-27 17:53:08 +0000
+++ b/bzrlib/tests/per_repository/__init__.py	2011-02-08 15:25:49 +0000
@@ -28,10 +28,7 @@
     repository,
     )
 from bzrlib.revision import NULL_REVISION
-from bzrlib.repofmt import (
-    weaverepo,
-    )
-from bzrlib.remote import RemoteBzrDirFormat, RemoteRepositoryFormat
+from bzrlib.remote import RemoteRepositoryFormat
 from bzrlib.tests import (
     default_transport,
     multiply_scenarios,
@@ -73,9 +70,7 @@
 def all_repository_format_scenarios():
     """Return a list of test scenarios for parameterising repository tests.
     """
-    registry = repository.format_registry
-    all_formats = [registry.get(k) for k in registry.keys()]
-    all_formats.extend(weaverepo._legacy_formats)
+    all_formats = repository.format_registry._get_all()
     # format_scenarios is all the implementations of Repository; i.e. all disk
     # formats plus RemoteRepository.
     format_scenarios = formats_to_scenarios(

=== modified file 'bzrlib/tests/test_bzrdir.py'
--- a/bzrlib/tests/test_bzrdir.py	2011-02-07 03:24:37 +0000
+++ b/bzrlib/tests/test_bzrdir.py	2011-02-10 15:13:32 +0000
@@ -172,7 +172,7 @@
             self.assertIs(bzrdir.format_registry.get('dirstate-with-subtree'),
                           bzrdir.format_registry.get('default'))
             self.assertIs(
-                repository.RepositoryFormat.get_default_format().__class__,
+                repository.format_registry.get_default().__class__,
                 knitrepo.RepositoryFormatKnit3)
         finally:
             bzrdir.format_registry.set_default_repository(old_default)

=== modified file 'bzrlib/tests/test_remote.py'
--- a/bzrlib/tests/test_remote.py	2011-02-07 02:39:15 +0000
+++ b/bzrlib/tests/test_remote.py	2011-02-10 15:13:32 +0000
@@ -1879,7 +1879,7 @@
 
     def test_get_format_description(self):
         remote_repo_format = RemoteRepositoryFormat()
-        real_format = repository.RepositoryFormat.get_default_format()
+        real_format = repository.format_registry.get_default()
         remote_repo_format._network_name = real_format.network_name()
         self.assertEqual(remoted_description(real_format),
             remote_repo_format.get_format_description())
@@ -2443,7 +2443,7 @@
         the client is finished.
         """
         sink = repo._get_sink()
-        fmt = repository.RepositoryFormat.get_default_format()
+        fmt = repository.format_registry.get_default()
         resume_tokens, missing_keys = sink.insert_stream([], fmt, [])
         self.assertEqual([], resume_tokens)
         self.assertEqual(set(), missing_keys)
@@ -2549,7 +2549,7 @@
                 return True
         repo._real_repository = FakeRealRepository()
         sink = repo._get_sink()
-        fmt = repository.RepositoryFormat.get_default_format()
+        fmt = repository.format_registry.get_default()
         stream = self.make_stream_with_inv_deltas(fmt)
         resume_tokens, missing_keys = sink.insert_stream(stream, fmt, [])
         # Every record from the first inventory delta should have been sent to

=== modified file 'bzrlib/tests/test_repository.py'
--- a/bzrlib/tests/test_repository.py	2011-01-27 15:58:36 +0000
+++ b/bzrlib/tests/test_repository.py	2011-02-08 16:08:23 +0000
@@ -26,13 +26,15 @@
 import sys
 
 import bzrlib
-from bzrlib.errors import (NoSuchFile,
-                           UnknownFormatError,
-                           UnsupportedFormatError,
-                           )
+from bzrlib.errors import (
+    NoSuchFile,
+    UnknownFormatError,
+    UnsupportedFormatError,
+    )
 from bzrlib import (
     btree_index,
     graph,
+    symbol_versioning,
     tests,
     transport,
     )
@@ -67,7 +69,7 @@
     def test_get_set_default_format(self):
         old_default = bzrdir.format_registry.get('default')
         private_default = old_default().repository_format.__class__
-        old_format = repository.RepositoryFormat.get_default_format()
+        old_format = repository.format_registry.get_default()
         self.assertTrue(isinstance(old_format, private_default))
         def make_sample_bzrdir():
             my_bzrdir = bzrdir.BzrDirMetaFormat1()
@@ -87,7 +89,7 @@
             bzrdir.format_registry.remove('default')
             bzrdir.format_registry.remove('sample')
             bzrdir.format_registry.register('default', old_default, '')
-        self.assertIsInstance(repository.RepositoryFormat.get_default_format(),
+        self.assertIsInstance(repository.format_registry.get_default(),
                               old_format.__class__)
 
 
@@ -115,6 +117,15 @@
         return "opened repository."
 
 
+class SampleExtraRepositoryFormat(repository.RepositoryFormat):
+    """A sample format that can not be used in a metadir
+
+    """
+
+    def get_format_string(self):
+        raise NotImplementedError
+
+
 class TestRepositoryFormat(TestCaseWithTransport):
     """Tests for the Repository format detection used by the bzr meta dir facility.BzrBranchFormat facility."""
 
@@ -145,19 +156,57 @@
                           dir)
 
     def test_register_unregister_format(self):
+        # Test deprecated format registration functions
         format = SampleRepositoryFormat()
         # make a control dir
         dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
         # make a repo
         format.initialize(dir)
         # register a format for it.
-        repository.RepositoryFormat.register_format(format)
+        self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
+            repository.RepositoryFormat.register_format, format)
         # which repository.Open will refuse (not supported)
-        self.assertRaises(UnsupportedFormatError, repository.Repository.open, self.get_url())
+        self.assertRaises(UnsupportedFormatError, repository.Repository.open,
+            self.get_url())
         # but open(unsupported) will work
         self.assertEqual(format.open(dir), "opened repository.")
         # unregister the format
-        repository.RepositoryFormat.unregister_format(format)
+        self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
+            repository.RepositoryFormat.unregister_format, format)
+
+
+class TestRepositoryFormatRegistry(TestCase):
+
+    def setUp(self):
+        super(TestRepositoryFormatRegistry, self).setUp()
+        self.registry = repository.RepositoryFormatRegistry()
+
+    def test_register_unregister_format(self):
+        format = SampleRepositoryFormat()
+        self.registry.register(format)
+        self.assertEquals(format, self.registry.get("Sample .bzr repository format."))
+        self.registry.remove(format)
+        self.assertRaises(KeyError, self.registry.get, "Sample .bzr repository format.")
+
+    def test_get_all(self):
+        format = SampleRepositoryFormat()
+        self.assertEquals([], self.registry._get_all())
+        self.registry.register(format)
+        self.assertEquals([format], self.registry._get_all())
+
+    def test_register_extra(self):
+        format = SampleExtraRepositoryFormat()
+        self.assertEquals([], self.registry._get_all())
+        self.registry.register_extra(format)
+        self.assertEquals([format], self.registry._get_all())
+
+    def test_register_extra_lazy(self):
+        self.assertEquals([], self.registry._get_all())
+        self.registry.register_extra_lazy("bzrlib.tests.test_repository",
+            "SampleExtraRepositoryFormat")
+        formats = self.registry._get_all()
+        self.assertEquals(1, len(formats))
+        self.assertIsInstance(formats[0], SampleExtraRepositoryFormat)
 
 
 class TestFormat6(TestCaseWithTransport):

=== modified file 'doc/en/release-notes/bzr-2.4.txt'
--- a/doc/en/release-notes/bzr-2.4.txt	2011-02-09 17:10:05 +0000
+++ b/doc/en/release-notes/bzr-2.4.txt	2011-02-10 16:30:54 +0000
@@ -110,6 +110,10 @@
   ``import_last_revision_info_and_tags`` method instead.
   (Andrew Bennetts)
 
+* Repository formats should now be registered on the format registry
+  (``bzrlib.repository.format_registry``) rather than using the class
+  methods on ``RepositoryFormat``. (Jelmer Vernooij)
+
 * The ``revision_id`` parameter of
   ``Repository.search_missing_revision_ids`` and
   ``InterRepository.search_missing_revision_ids`` is deprecated.  It is




More information about the bazaar-commits mailing list