Rev 4301: Start building up a BzrDir.initialize_ex verb for the smart server. in http://people.ubuntu.com/~robertc/baz2.0/pending/push.roundtrips

Robert Collins robertc at robertcollins.net
Fri Apr 24 01:45:14 BST 2009


At http://people.ubuntu.com/~robertc/baz2.0/pending/push.roundtrips

------------------------------------------------------------
revno: 4301
revision-id: robertc at robertcollins.net-20090424004511-8oszlwmvehlqwrla
parent: robertc at robertcollins.net-20090423233544-3dfus3gca15ita16
committer: Robert Collins <robertc at robertcollins.net>
branch nick: push.roundtrips
timestamp: Fri 2009-04-24 10:45:11 +1000
message:
  Start building up a BzrDir.initialize_ex verb for the smart server.
=== modified file 'bzrlib/bzrdir.py'
--- a/bzrlib/bzrdir.py	2009-04-23 23:35:44 +0000
+++ b/bzrlib/bzrdir.py	2009-04-24 00:45:11 +0000
@@ -1859,6 +1859,8 @@
         shared_repo=False):
         """Create this format on transport.
 
+        The directory to initialize will be created.
+
         :param force_new_repo: Do not use a shared repository for the target,
                                even if one is available.
         :param create_prefix: Create any missing directories leading up to
@@ -2956,8 +2958,8 @@
         return to_convert
 
 
-# This is not in remote.py because it's small, and needs to be registered.
-# Putting it in remote.py creates a circular import problem.
+# This is not in remote.py because it's relatively small, and needs to be
+# registered. Putting it in remote.py creates a circular import problem.
 # we can make it a lazy object if the control formats is turned into something
 # like a registry.
 class RemoteBzrDirFormat(BzrDirMetaFormat1):
@@ -3020,6 +3022,65 @@
         self._supply_sub_formats_to(format)
         return remote.RemoteBzrDir(transport, format)
 
+    def _serialize_NoneTrueFalse(self, arg):
+        if arg is False:
+            return 'False'
+        if arg:
+            return 'True'
+        return ''
+
+    def initialize_on_transport_ex(self, transport, use_existing_dir=False,
+        create_prefix=False, force_new_repo=False, stacked_on=None,
+        stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
+        shared_repo=False):
+        try:
+            # hand off the request to the smart server
+            client_medium = transport.get_smart_medium()
+        except errors.NoSmartMedium:
+            # TODO: lookup the local format from a server hint.
+            local_dir_format = BzrDirMetaFormat1()
+            self._supply_sub_formats_to(local_dir_format)
+            return local_dir_format.initialize_on_transport_ex(transport,
+                use_existing_dir=use_existing_dir, create_prefix=create_prefix,
+                force_new_repo=force_new_repo, stacked_on=stacked_on,
+                stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
+                make_working_trees=make_working_trees, shared_repo=shared_repo)
+        client = _SmartClient(client_medium)
+        path = client.remote_path_from_transport(transport)
+        if client_medium._is_remote_before((1, 15)):
+            local_dir_format = BzrDirMetaFormat1()
+            self._supply_sub_formats_to(local_dir_format)
+            return local_dir_format.initialize_on_transport_ex(transport,
+                use_existing_dir=use_existing_dir, create_prefix=create_prefix,
+                force_new_repo=force_new_repo, stacked_on=stacked_on,
+                stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
+                make_working_trees=make_working_trees, shared_repo=shared_repo)
+        if not (create_prefix is False and force_new_repo is False and
+            stacked_on is None and stack_on_pwd is None and repo_format_name is
+            None and make_working_trees is None and shared_repo is False):
+            local_dir_format = BzrDirMetaFormat1()
+            self._supply_sub_formats_to(local_dir_format)
+            return local_dir_format.initialize_on_transport_ex(transport,
+                use_existing_dir=use_existing_dir, create_prefix=create_prefix,
+                force_new_repo=force_new_repo, stacked_on=stacked_on,
+                stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
+                make_working_trees=make_working_trees, shared_repo=shared_repo)
+        args = []
+        args.append(self._serialize_NoneTrueFalse(use_existing_dir))
+        try:
+            response = client.call('BzrDirFormat.initialize_ex', path, *args)
+        except errors.UnknownSmartMethod:
+            local_dir_format = BzrDirMetaFormat1()
+            self._supply_sub_formats_to(local_dir_format)
+            return local_dir_format.initialize_on_transport_ex(transport,
+                use_existing_dir=use_existing_dir, create_prefix=create_prefix,
+                force_new_repo=force_new_repo, stacked_on=stacked_on,
+                stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
+                make_working_trees=make_working_trees, shared_repo=shared_repo)
+        format = RemoteBzrDirFormat()
+        self._supply_sub_formats_to(format)
+        return None, remote.RemoteBzrDir(transport, format), None, None
+
     def _open(self, transport):
         return remote.RemoteBzrDir(transport, self)
 

=== modified file 'bzrlib/smart/bzrdir.py'
--- a/bzrlib/smart/bzrdir.py	2009-04-14 04:33:41 +0000
+++ b/bzrlib/smart/bzrdir.py	2009-04-24 00:45:11 +0000
@@ -325,6 +325,29 @@
         return SuccessfulSmartServerResponse(('ok', ))
 
 
+class SmartServerRequestBzrDirInitializeEx(SmartServerRequest):
+
+    def parse_NoneTrueFalse(self, arg):
+        if not arg:
+            return None
+        if arg == 'False':
+            return False
+        if arg == 'True':
+            return True
+        raise AssertionError("invalid arg %r" % arg)
+
+    def do(self, path, use_existing_dir):
+        """Initialize a bzrdir at path as per BzrDirFormat.initialize_ex
+
+        :return: SmartServerResponse()
+        """
+        use_existing_dir = self.parse_NoneTrueFalse(use_existing_dir)
+        target_transport = self.transport_from_client_path(path)
+        BzrDirFormat.get_default_format().initialize_on_transport_ex(target_transport,
+            use_existing_dir=use_existing_dir)
+        return SuccessfulSmartServerResponse(())
+
+
 class SmartServerRequestOpenBranch(SmartServerRequestBzrDir):
 
     def do_bzrdir_request(self):

=== modified file 'bzrlib/smart/message.py'
--- a/bzrlib/smart/message.py	2009-03-23 14:59:43 +0000
+++ b/bzrlib/smart/message.py	2009-04-24 00:45:11 +0000
@@ -351,5 +351,9 @@
         raise errors.LockContention('(remote lock)')
     elif error_name == 'LockFailed':
         raise errors.LockFailed(*error_args[:2])
+    elif error_name == 'FileExists':
+        raise errors.FileExists(error_args[0])
+    elif error_name == 'NoSuchFile':
+        raise errors.NoSuchFile(error_args[0])
     else:
         raise errors.ErrorFromSmartServer(error_tuple)

=== modified file 'bzrlib/smart/request.py'
--- a/bzrlib/smart/request.py	2009-04-20 04:19:45 +0000
+++ b/bzrlib/smart/request.py	2009-04-24 00:45:11 +0000
@@ -492,6 +492,9 @@
     'BzrDirFormat.initialize', 'bzrlib.smart.bzrdir',
     'SmartServerRequestInitializeBzrDir')
 request_handlers.register_lazy(
+    'BzrDirFormat.initialize_ex', 'bzrlib.smart.bzrdir',
+    'SmartServerRequestBzrDirInitializeEx')
+request_handlers.register_lazy(
     'BzrDir.open', 'bzrlib.smart.bzrdir', 'SmartServerRequestOpenBzrDir')
 request_handlers.register_lazy(
     'BzrDir.open_branch', 'bzrlib.smart.bzrdir',

=== modified file 'bzrlib/tests/test_smart.py'
--- a/bzrlib/tests/test_smart.py	2009-04-15 03:45:24 +0000
+++ b/bzrlib/tests/test_smart.py	2009-04-24 00:45:11 +0000
@@ -350,6 +350,38 @@
             request.execute, 'subdir')
 
 
+class TestSmartServerRequestBzrDirInitializeEx(tests.TestCaseWithMemoryTransport):
+    """Basic tests for BzrDir.initialize_ex in the smart server.
+
+    The main unit tests in test_bzrdir exercise the API coprehensively.
+    """
+
+    def test_empty_dir(self):
+        """Initializing an empty dir should succeed and do it."""
+        backing = self.get_transport()
+        request = smart.bzrdir.SmartServerRequestBzrDirInitializeEx(backing)
+        self.assertEqual(SmartServerResponse(()), request.execute('', 'True'))
+        made_dir = bzrdir.BzrDir.open_from_transport(backing)
+        # no branch, tree or repository is expected with the current
+        # default formart.
+        self.assertRaises(errors.NoWorkingTree, made_dir.open_workingtree)
+        self.assertRaises(errors.NotBranchError, made_dir.open_branch)
+        self.assertRaises(errors.NoRepositoryPresent, made_dir.open_repository)
+
+    def test_missing_dir(self):
+        """Initializing a missing directory should fail like the bzrdir api."""
+        backing = self.get_transport()
+        request = smart.bzrdir.SmartServerRequestBzrDirInitializeEx(backing)
+        self.assertRaises(errors.NoSuchFile, request.execute, 'subdir/dir', 'False')
+
+    def test_initialized_dir(self):
+        """Initializing an extant dirctory should fail like the bzrdir api."""
+        backing = self.get_transport()
+        request = smart.bzrdir.SmartServerRequestBzrDirInitializeEx(backing)
+        self.make_bzrdir('subdir')
+        self.assertRaises(errors.FileExists, request.execute, 'subdir', 'False')
+
+
 class TestSmartServerRequestOpenBranch(TestCaseWithChrootedTransport):
 
     def test_no_branch(self):
@@ -1479,6 +1511,8 @@
             smart.bzrdir.SmartServerRequestFindRepositoryV2)
         self.assertHandlerEqual('BzrDirFormat.initialize',
             smart.bzrdir.SmartServerRequestInitializeBzrDir)
+        self.assertHandlerEqual('BzrDirFormat.initialize_ex',
+            smart.bzrdir.SmartServerRequestBzrDirInitializeEx)
         self.assertHandlerEqual('BzrDir.cloning_metadir',
             smart.bzrdir.SmartServerBzrDirRequestCloningMetaDir)
         self.assertHandlerEqual('BzrDir.get_config_file',




More information about the bazaar-commits mailing list