Rev 18: Add some simple add/remove helpers. in lp:~jameinel/+junk/file_locking

John Arbash Meinel john at arbash-meinel.com
Mon Sep 21 20:39:57 BST 2009


At lp:~jameinel/+junk/file_locking

------------------------------------------------------------
revno: 18
revision-id: john at arbash-meinel.com-20090921193948-q2pge6dqzaev52rk
parent: john at arbash-meinel.com-20090921191820-9bf1bga0r9srf2ju
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: file_locking
timestamp: Mon 2009-09-21 14:39:48 -0500
message:
  Add some simple add/remove helpers.
-------------- next part --------------
=== modified file 'file_lock.py'
--- a/file_lock.py	2009-09-21 19:18:20 +0000
+++ b/file_lock.py	2009-09-21 19:39:48 +0000
@@ -154,6 +154,9 @@
 
     def to_bytes(self, locking_info):
         content = [self._HEADER, '\n']
+        # TODO: We may want to gzip everything but the initial header. We
+        #       should at least consider it, in case the download/upload
+        #       content size ever becomes a factor.
         stanza = rio.Stanza()
         for tracked_id in sorted(locking_info._tracked_ids):
             stanza.add('t', tracked_id)
@@ -183,6 +186,9 @@
     def create_lock(self, file_id):
         # Mostly the same info that is generated for bzr LockDir info files
         # Note that this is slightly wrong if you have a per-directory username
+        if file_id not in self._tracked_ids:
+            # TODO: Custom exception
+            raise KeyError('{%s} is not tracked' % (file_id,))
         global_conf = _mod_config.GlobalConfig()
         nonce = osutils.rand_chars(20)
         s = rio.Stanza(host=osutils.get_host_name(),
@@ -194,6 +200,16 @@
                    )
         self._active_locks[file_id] = s
         return s
+
+    def remove_lock(self, file_id):
+        """Remove the given lock from the 'active' set."""
+        del self._active_locks[file_id]
+
+    def track(self, file_id):
+        self._tracked_ids.add(file_id)
+
+    def stop_tracking(self, file_id):
+        self._tracked_ids.remove(file_id)
         
 
 class FileLockStore(object):

=== modified file 'tests/test_file_lock.py'
--- a/tests/test_file_lock.py	2009-09-21 19:18:20 +0000
+++ b/tests/test_file_lock.py	2009-09-21 19:39:48 +0000
@@ -80,6 +80,15 @@
                              'active_locks: 0\n'
                              '\n' % s._HEADER, s.to_bytes(li))
 
+    def test_track(self):
+        li = file_lock.FileLockingInfo(['file-id-1'], {})
+        self.assertEqual(set(['file-id-1']), li._tracked_ids)
+        li.track('file-id-2')
+        self.assertEqual(set(['file-id-1', 'file-id-2']), li._tracked_ids)
+        li.stop_tracking('file-id-1')
+        self.assertEqual(set(['file-id-2']), li._tracked_ids)
+        self.assertRaises(KeyError, li.stop_tracking, 'file-id-1')
+
     def test_create_lock(self):
         li = file_lock.FileLockingInfo(['file-id-1', 'file-id-2'], {})
         info = li.create_lock('file-id-1')
@@ -139,6 +148,19 @@
                                      info2['user'],
                                     ), s.to_bytes(li))
 
+    def test_create_lock_not_tracked(self):
+        li = file_lock.FileLockingInfo(['file-id-1', 'file-id-2'], {})
+        self.assertRaises(KeyError, li.create_lock, 'not-file-id')
+
+    def test_remove_lock(self):
+        li = file_lock.FileLockingInfo(['file-id-1', 'file-id-2'], {})
+        info = li.create_lock('file-id-1')
+        info2 = li.create_lock('file-id-2')
+        self.assertEqual(['file-id-1', 'file-id-2'], sorted(li._active_locks))
+        li.remove_lock('file-id-1')
+        self.assertEqual(['file-id-2'], sorted(li._active_locks))
+        self.assertRaises(KeyError, li.remove_lock, 'file-id-1')
+
 
 class TestHeldLockSerializer(tests.TestCase):
 



More information about the bazaar-commits mailing list