Rev 2905: * New method ``bzrlib.repository.Repository.is_write_locked`` useful for in http://people.ubuntu.com/~robertc/baz2.0/locks

Robert Collins robertc at robertcollins.net
Fri Oct 12 07:39:00 BST 2007


At http://people.ubuntu.com/~robertc/baz2.0/locks

------------------------------------------------------------
revno: 2905
revision-id: robertc at robertcollins.net-20071012063833-ll6okyit92y0vm3p
parent: pqm at pqm.ubuntu.com-20071010085229-7x5al1tirr29mq0l
committer: Robert Collins <robertc at robertcollins.net>
branch nick: locks
timestamp: Fri 2007-10-12 16:38:33 +1000
message:
  * New method ``bzrlib.repository.Repository.is_write_locked`` useful for
    determining if a repository is write locked. (Robert Collins)
added:
  bzrlib/tests/repository_implementations/test_is_write_locked.py test_is_write_locked-20071012063748-vk062lmu683qgbc3-1
modified:
  NEWS                           NEWS-20050323055033-4e00b5db738777ff
  bzrlib/remote.py               remote.py-20060720103555-yeeg2x51vn0rbtdp-1
  bzrlib/repository.py           rev_storage.py-20051111201905-119e9401e46257e3
  bzrlib/tests/repository_implementations/__init__.py __init__.py-20060131092037-9564957a7d4a841b
  bzrlib/tests/repository_implementations/test_break_lock.py test_break_lock.py-20060504111704-ee09a107f9f42e43
=== added file 'bzrlib/tests/repository_implementations/test_is_write_locked.py'
--- a/bzrlib/tests/repository_implementations/test_is_write_locked.py	1970-01-01 00:00:00 +0000
+++ b/bzrlib/tests/repository_implementations/test_is_write_locked.py	2007-10-12 06:38:33 +0000
@@ -0,0 +1,62 @@
+# Copyright (C) 2006 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+"""Tests for repository break-lock."""
+
+from cStringIO import StringIO
+
+import bzrlib
+import bzrlib.errors as errors
+from bzrlib.tests.repository_implementations.test_repository import TestCaseWithRepository
+from bzrlib.transport import get_transport
+from bzrlib.workingtree import WorkingTree
+
+
+class TestBreakLock(TestCaseWithRepository):
+
+    def setUp(self):
+        super(TestBreakLock, self).setUp()
+        self.unused_repo = self.make_repository('.')
+        self.repo = self.unused_repo.bzrdir.open_repository()
+        # we want a UI factory that accepts canned input for the tests:
+        # while SilentUIFactory still accepts stdin, we need to customise
+        # ours
+        self.old_factory = bzrlib.ui.ui_factory
+        self.addCleanup(self.restoreFactory)
+        bzrlib.ui.ui_factory = bzrlib.ui.SilentUIFactory()
+        bzrlib.ui.ui_factory.stdin = StringIO("y\n")
+
+    def restoreFactory(self):
+        bzrlib.ui.ui_factory = self.old_factory
+
+    def test_unlocked(self):
+        # break lock when nothing is locked should just return
+        try:
+            self.repo.break_lock()
+        except NotImplementedError:
+            pass
+
+    def test_locked(self):
+        # break_lock when locked should
+        self.repo.lock_write()
+        self.assertEqual(self.repo.get_physical_lock_status(),
+            self.unused_repo.get_physical_lock_status())
+        if not self.unused_repo.get_physical_lock_status():
+            # 'lock_write' has not taken a physical mutex out.
+            self.repo.unlock()
+            return
+        self.unused_repo.break_lock()
+        self.assertRaises(errors.LockBroken, self.repo.unlock)

=== modified file 'NEWS'
--- a/NEWS	2007-10-10 00:21:57 +0000
+++ b/NEWS	2007-10-12 06:38:33 +0000
@@ -204,6 +204,9 @@
      duplication from user input, when a user mentions both a path and an item
      contained within that path. (Robert Collins)
 
+   * New method ``bzrlib.repository.Repository.is_write_locked`` useful for
+     determining if a repository is write locked. (Robert Collins)
+
    * New method on ``bzrlib.tree.Tree`` ``path_content_summary`` provides a
      tuple containing the key information about a path for commit processing
      to complete. (Robert Collins)

=== modified file 'bzrlib/remote.py'
--- a/bzrlib/remote.py	2007-10-10 00:21:57 +0000
+++ b/bzrlib/remote.py	2007-10-12 06:38:33 +0000
@@ -398,6 +398,9 @@
         assert response[0] in ('yes', 'no'), 'unexpected response code %s' % (response,)
         return response[0] == 'yes'
 
+    def is_write_locked(self):
+        return self._lock_mode == 'w'
+
     def lock_read(self):
         # wrong eventually - want a local lock cache context
         if not self._lock_mode:

=== modified file 'bzrlib/repository.py'
--- a/bzrlib/repository.py	2007-10-08 23:27:01 +0000
+++ b/bzrlib/repository.py	2007-10-12 06:38:33 +0000
@@ -656,6 +656,10 @@
     def is_locked(self):
         return self.control_files.is_locked()
 
+    def is_write_locked(self):
+        """Return True if this object is write locked."""
+        return self.is_locked() and self.control_files._lock_mode == 'w'
+
     def lock_write(self, token=None):
         """Lock this repository for writing.
 
@@ -883,7 +887,7 @@
 
         :return: None.
         """
-        if not self.is_locked() or self.control_files._lock_mode != 'w':
+        if not self.is_write_locked():
             raise errors.NotWriteLocked(self)
         if self._write_group:
             raise errors.BzrError('already in a write group')

=== modified file 'bzrlib/tests/repository_implementations/__init__.py'
--- a/bzrlib/tests/repository_implementations/__init__.py	2007-08-17 05:16:14 +0000
+++ b/bzrlib/tests/repository_implementations/__init__.py	2007-10-12 06:38:33 +0000
@@ -103,6 +103,7 @@
         'bzrlib.tests.repository_implementations.test_fetch',
         'bzrlib.tests.repository_implementations.test_fileid_involved',
         'bzrlib.tests.repository_implementations.test_has_same_location',
+        'bzrlib.tests.repository_implementations.test_is_write_locked',
         'bzrlib.tests.repository_implementations.test_iter_reverse_revision_history',
         'bzrlib.tests.repository_implementations.test_pack',
         'bzrlib.tests.repository_implementations.test_reconcile',

=== modified file 'bzrlib/tests/repository_implementations/test_break_lock.py'
--- a/bzrlib/tests/repository_implementations/test_break_lock.py	2006-10-11 23:08:27 +0000
+++ b/bzrlib/tests/repository_implementations/test_break_lock.py	2007-10-12 06:38:33 +0000
@@ -1,4 +1,4 @@
-# Copyright (C) 2006 Canonical Ltd
+# Copyright (C) 2007 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
@@ -14,48 +14,25 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-"""Tests for repository break-lock."""
-
-from cStringIO import StringIO
-
-import bzrlib
-import bzrlib.errors as errors
+"""Tests for Repository.is_write_locked()."""
+
 from bzrlib.tests.repository_implementations.test_repository import TestCaseWithRepository
-from bzrlib.transport import get_transport
-from bzrlib.workingtree import WorkingTree
-
-
-class TestBreakLock(TestCaseWithRepository):
-
-    def setUp(self):
-        super(TestBreakLock, self).setUp()
-        self.unused_repo = self.make_repository('.')
-        self.repo = self.unused_repo.bzrdir.open_repository()
-        # we want a UI factory that accepts canned input for the tests:
-        # while SilentUIFactory still accepts stdin, we need to customise
-        # ours
-        self.old_factory = bzrlib.ui.ui_factory
-        self.addCleanup(self.restoreFactory)
-        bzrlib.ui.ui_factory = bzrlib.ui.SilentUIFactory()
-        bzrlib.ui.ui_factory.stdin = StringIO("y\n")
-
-    def restoreFactory(self):
-        bzrlib.ui.ui_factory = self.old_factory
-
-    def test_unlocked(self):
-        # break lock when nothing is locked should just return
-        try:
-            self.repo.break_lock()
-        except NotImplementedError:
-            pass
-
-    def test_locked(self):
-        # break_lock when locked should
-        self.repo.lock_write()
-        try:
-            self.unused_repo.break_lock()
-        except NotImplementedError:
-            # repository does not support break_lock
-            self.repo.unlock()
-            return
-        self.assertRaises(errors.LockBroken, self.repo.unlock)
+
+
+class TestIsWriteLocked(TestCaseWithRepository):
+
+    def test_not_locked(self):
+        repo = self.make_repository('.')
+        self.assertFalse(repo.is_write_locked())
+
+    def test_read_locked(self):
+        repo = self.make_repository('.')
+        repo.lock_read()
+        self.addCleanup(repo.unlock)
+        self.assertFalse(repo.is_write_locked())
+
+    def test_write_locked(self):
+        repo = self.make_repository('.')
+        repo.lock_write()
+        self.addCleanup(repo.unlock)
+        self.assertTrue(repo.is_write_locked())



More information about the bazaar-commits mailing list