Rev 5232: (andrew) Fix RemoteBranchConfig.set_user_option when passed a dict in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Fri May 14 12:05:54 BST 2010


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

------------------------------------------------------------
revno: 5232 [merge]
revision-id: pqm at pqm.ubuntu.com-20100514110532-qngntbe48hh01wkj
parent: pqm at pqm.ubuntu.com-20100514094851-mju4y69uma6kwsqe
parent: andrew.bennetts at canonical.com-20100514090235-32z3rbfplw712csz
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Fri 2010-05-14 12:05:32 +0100
message:
  (andrew) Fix RemoteBranchConfig.set_user_option when passed a dict
  	value. (#430382)
added:
  bzrlib/tests/per_branch/test_config.py test_config.py-20100513163053-tufhixqa9nn7lsp2-1
modified:
  NEWS                           NEWS-20050323055033-4e00b5db738777ff
  bzrlib/remote.py               remote.py-20060720103555-yeeg2x51vn0rbtdp-1
  bzrlib/smart/branch.py         branch.py-20061124031907-mzh3pla28r83r97f-1
  bzrlib/smart/request.py        request.py-20061108095550-gunadhxmzkdjfeek-1
  bzrlib/tests/per_branch/__init__.py __init__.py-20060123013057-b12a52c3f361daf4
  bzrlib/tests/test_remote.py    test_remote.py-20060720103555-yeeg2x51vn0rbtdp-2
  bzrlib/tests/test_smart.py     test_smart.py-20061122024551-ol0l0o0oofsu9b3t-2
=== modified file 'NEWS'
--- a/NEWS	2010-05-14 08:27:57 +0000
+++ b/NEWS	2010-05-14 09:02:35 +0000
@@ -108,6 +108,11 @@
   Error, breaking on of our test hygiene tests.
   (Robert Collins, Vincent Ladeuil).
 
+* ``set_user_option`` with a dict on remote branches no longer fails with
+  an AttributeError.  There is a new ``Branch.set_config_option_dict`` RPC
+  to support this efficiently.
+  (Andrew Bennetts, #430382)
+  
 * Show the filenames when a file rename fails so that the error will be
   more comprehensible.
   (Martin Pool, #491763)

=== modified file 'bzrlib/remote.py'
--- a/bzrlib/remote.py	2010-05-11 08:44:59 +0000
+++ b/bzrlib/remote.py	2010-05-13 16:17:54 +0000
@@ -2788,17 +2788,51 @@
         medium = self._branch._client._medium
         if medium._is_remote_before((1, 14)):
             return self._vfs_set_option(value, name, section)
+        if isinstance(value, dict):
+            if medium._is_remote_before((2, 2)):
+                return self._vfs_set_option(value, name, section)
+            return self._set_config_option_dict(value, name, section)
+        else:
+            return self._set_config_option(value, name, section)
+
+    def _set_config_option(self, value, name, section):
         try:
             path = self._branch._remote_path()
             response = self._branch._client.call('Branch.set_config_option',
                 path, self._branch._lock_token, self._branch._repo_lock_token,
                 value.encode('utf8'), name, section or '')
         except errors.UnknownSmartMethod:
+            medium = self._branch._client._medium
             medium._remember_remote_is_before((1, 14))
             return self._vfs_set_option(value, name, section)
         if response != ():
             raise errors.UnexpectedSmartServerResponse(response)
 
+    def _serialize_option_dict(self, option_dict):
+        utf8_dict = {}
+        for key, value in option_dict.items():
+            if isinstance(key, unicode):
+                key = key.encode('utf8')
+            if isinstance(value, unicode):
+                value = value.encode('utf8')
+            utf8_dict[key] = value
+        return bencode.bencode(utf8_dict)
+
+    def _set_config_option_dict(self, value, name, section):
+        try:
+            path = self._branch._remote_path()
+            serialised_dict = self._serialize_option_dict(value)
+            response = self._branch._client.call(
+                'Branch.set_config_option_dict',
+                path, self._branch._lock_token, self._branch._repo_lock_token,
+                serialised_dict, name, section or '')
+        except errors.UnknownSmartMethod:
+            medium = self._branch._client._medium
+            medium._remember_remote_is_before((2, 2))
+            return self._vfs_set_option(value, name, section)
+        if response != ():
+            raise errors.UnexpectedSmartServerResponse(response)
+
     def _real_object(self):
         self._branch._ensure_real()
         return self._branch._real_branch

=== modified file 'bzrlib/smart/branch.py'
--- a/bzrlib/smart/branch.py	2010-05-11 14:14:55 +0000
+++ b/bzrlib/smart/branch.py	2010-05-13 16:17:54 +0000
@@ -17,7 +17,10 @@
 """Server-side branch related request implmentations."""
 
 
-from bzrlib import errors
+from bzrlib import (
+    bencode,
+    errors,
+    )
 from bzrlib.bzrdir import BzrDir
 from bzrlib.smart.request import (
     FailedSmartServerResponse,
@@ -194,6 +197,23 @@
         return SuccessfulSmartServerResponse(())
 
 
+class SmartServerBranchRequestSetConfigOptionDict(SmartServerLockedBranchRequest):
+    """Set an option in the branch configuration.
+    
+    New in 2.2.
+    """
+
+    def do_with_locked_branch(self, branch, value_dict, name, section):
+        utf8_dict = bencode.bdecode(value_dict)
+        value_dict = {}
+        for key, value in utf8_dict.items():
+            value_dict[key.decode('utf8')] = value.decode('utf8')
+        if not section:
+            section = None
+        branch._get_config().set_option(value_dict, name, section)
+        return SuccessfulSmartServerResponse(())
+
+
 class SmartServerBranchRequestSetLastRevision(SmartServerSetTipRequest):
 
     def do_tip_change_with_locked_branch(self, branch, new_last_revision_id):

=== modified file 'bzrlib/smart/request.py'
--- a/bzrlib/smart/request.py	2010-02-17 17:11:16 +0000
+++ b/bzrlib/smart/request.py	2010-05-13 16:17:54 +0000
@@ -510,6 +510,8 @@
     'bzrlib.smart.branch', 'SmartServerRequestRevisionHistory')
 request_handlers.register_lazy( 'Branch.set_config_option',
     'bzrlib.smart.branch', 'SmartServerBranchRequestSetConfigOption')
+request_handlers.register_lazy( 'Branch.set_config_option_dict',
+    'bzrlib.smart.branch', 'SmartServerBranchRequestSetConfigOptionDict')
 request_handlers.register_lazy( 'Branch.set_last_revision',
     'bzrlib.smart.branch', 'SmartServerBranchRequestSetLastRevision')
 request_handlers.register_lazy(

=== modified file 'bzrlib/tests/per_branch/__init__.py'
--- a/bzrlib/tests/per_branch/__init__.py	2010-04-01 12:53:53 +0000
+++ b/bzrlib/tests/per_branch/__init__.py	2010-05-13 15:14:41 +0000
@@ -165,6 +165,7 @@
         'branch',
         'break_lock',
         'check',
+        'config',
         'create_checkout',
         'create_clone',
         'commit',

=== added file 'bzrlib/tests/per_branch/test_config.py'
--- a/bzrlib/tests/per_branch/test_config.py	1970-01-01 00:00:00 +0000
+++ b/bzrlib/tests/per_branch/test_config.py	2010-05-13 16:31:08 +0000
@@ -0,0 +1,38 @@
+# Copyright (C) 2010 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 branch.get_config behaviour."""
+
+from bzrlib import (
+    branch,
+    errors,
+    remote,
+    tests,
+    )
+from bzrlib.tests import per_branch
+
+
+class TestGetConfig(per_branch.TestCaseWithBranch):
+
+    def test_set_user_option_with_dict(self):
+        b = self.make_branch('b')
+        config = b.get_config()
+        value_dict = {
+            'ascii': 'abcd', u'unicode \N{WATCH}': u'foo \N{INTERROBANG}'}
+        config.set_user_option('name', value_dict.copy())
+        self.assertEqual(value_dict, config.get_user_option('name'))
+
+

=== modified file 'bzrlib/tests/test_remote.py'
--- a/bzrlib/tests/test_remote.py	2010-05-13 18:52:58 +0000
+++ b/bzrlib/tests/test_remote.py	2010-05-14 09:02:35 +0000
@@ -1652,6 +1652,32 @@
         branch.unlock()
         self.assertFinished(client)
 
+    def test_set_option_with_dict(self):
+        client = FakeClient()
+        client.add_expected_call(
+            'Branch.get_stacked_on_url', ('memory:///',),
+            'error', ('NotStacked',),)
+        client.add_expected_call(
+            'Branch.lock_write', ('memory:///', '', ''),
+            'success', ('ok', 'branch token', 'repo token'))
+        encoded_dict_value = 'd5:ascii1:a11:unicode \xe2\x8c\x9a3:\xe2\x80\xbde'
+        client.add_expected_call(
+            'Branch.set_config_option_dict', ('memory:///', 'branch token',
+            'repo token', encoded_dict_value, 'foo', ''),
+            'success', ())
+        client.add_expected_call(
+            'Branch.unlock', ('memory:///', 'branch token', 'repo token'),
+            'success', ('ok',))
+        transport = MemoryTransport()
+        branch = self.make_remote_branch(transport, client)
+        branch.lock_write()
+        config = branch._get_config()
+        config.set_option(
+            {'ascii': 'a', u'unicode \N{WATCH}': u'\N{INTERROBANG}'},
+            'foo')
+        branch.unlock()
+        self.assertFinished(client)
+
     def test_backwards_compat_set_option(self):
         self.setup_smart_server_with_call_log()
         branch = self.make_branch('.')
@@ -1664,6 +1690,20 @@
         self.assertLength(10, self.hpss_calls)
         self.assertEqual('value', branch._get_config().get_option('name'))
 
+    def test_backwards_compat_set_option_with_dict(self):
+        self.setup_smart_server_with_call_log()
+        branch = self.make_branch('.')
+        verb = 'Branch.set_config_option_dict'
+        self.disable_verb(verb)
+        branch.lock_write()
+        self.addCleanup(branch.unlock)
+        self.reset_smart_call_log()
+        config = branch._get_config()
+        value_dict = {'ascii': 'a', u'unicode \N{WATCH}': u'\N{INTERROBANG}'}
+        config.set_option(value_dict, 'name')
+        self.assertLength(10, self.hpss_calls)
+        self.assertEqual(value_dict, branch._get_config().get_option('name'))
+
 
 class TestBranchLockWrite(RemoteBranchTestCase):
 

=== modified file 'bzrlib/tests/test_smart.py'
--- a/bzrlib/tests/test_smart.py	2010-05-06 23:41:35 +0000
+++ b/bzrlib/tests/test_smart.py	2010-05-13 16:17:54 +0000
@@ -802,6 +802,44 @@
         branch.unlock()
 
 
+class TestSmartServerBranchRequestSetConfigOptionDict(TestLockedBranch):
+
+    def setUp(self):
+        TestLockedBranch.setUp(self)
+        # A dict with non-ascii keys and values to exercise unicode
+        # roundtripping.
+        self.encoded_value_dict = (
+            'd5:ascii1:a11:unicode \xe2\x8c\x9a3:\xe2\x80\xbde')
+        self.value_dict = {
+            'ascii': 'a', u'unicode \N{WATCH}': u'\N{INTERROBANG}'}
+
+    def test_value_name(self):
+        branch = self.make_branch('.')
+        request = smart_branch.SmartServerBranchRequestSetConfigOptionDict(
+            branch.bzrdir.root_transport)
+        branch_token, repo_token = self.get_lock_tokens(branch)
+        config = branch._get_config()
+        result = request.execute('', branch_token, repo_token,
+            self.encoded_value_dict, 'foo', '')
+        self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), result)
+        self.assertEqual(self.value_dict, config.get_option('foo'))
+        # Cleanup
+        branch.unlock()
+
+    def test_value_name_section(self):
+        branch = self.make_branch('.')
+        request = smart_branch.SmartServerBranchRequestSetConfigOptionDict(
+            branch.bzrdir.root_transport)
+        branch_token, repo_token = self.get_lock_tokens(branch)
+        config = branch._get_config()
+        result = request.execute('', branch_token, repo_token,
+            self.encoded_value_dict, 'foo', 'gam')
+        self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), result)
+        self.assertEqual(self.value_dict, config.get_option('foo', 'gam'))
+        # Cleanup
+        branch.unlock()
+
+
 class TestSmartServerBranchRequestSetTagsBytes(TestLockedBranch):
     # Only called when the branch format and tags match [yay factory
     # methods] so only need to test straight forward cases.




More information about the bazaar-commits mailing list