Rev 31: Change track and untrack into multi functions. in lp:~jameinel/+junk/file_locking

John Arbash Meinel john at arbash-meinel.com
Tue Sep 22 22:08:34 BST 2009


At lp:~jameinel/+junk/file_locking

------------------------------------------------------------
revno: 31
revision-id: john at arbash-meinel.com-20090922210824-qaha0fffgcjsu4z0
parent: john at arbash-meinel.com-20090922210047-315k7gvcdy1fqndw
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: file_locking
timestamp: Tue 2009-09-22 16:08:24 -0500
message:
  Change track and untrack into multi functions.
  
  This way we can add/remove in bulk, and have it tracked as a single log entry.
-------------- next part --------------
=== modified file 'file_lock.py'
--- a/file_lock.py	2009-09-22 21:00:47 +0000
+++ b/file_lock.py	2009-09-22 21:08:24 +0000
@@ -212,18 +212,19 @@
         info = self._active_locks.pop(file_id)
         self._logger.lock_removed(info)
 
-    def track(self, file_id):
-        """Include this file-id in the set of objects that can be locked."""
-        self._logger.track_id(file_id)
-        self._tracked_ids.add(file_id)
+    def track(self, file_ids):
+        """Include these file-ids in the set of objects that can be locked."""
+        self._logger.track_ids(file_ids)
+        self._tracked_ids.update(file_ids)
 
-    def stop_tracking(self, file_id):
+    def stop_tracking(self, file_ids):
         """Remove this file-id from being tracked."""
         # TODO: Custom exception
-        if file_id in self._active_locks:
-            raise ValueError('{%s} has an active lock' % (file_id,))
-        self._tracked_ids.remove(file_id)
-        self._logger.stop_tracking(file_id)
+        for file_id in file_ids:
+            if file_id in self._active_locks:
+                raise ValueError('{%s} has an active lock' % (file_id,))
+            self._tracked_ids.remove(file_id)
+        self._logger.stop_tracking(file_ids)
 
 
 class FileLockLogger(object):
@@ -283,16 +284,18 @@
         stanza.add('locked-time', info['time'])
         self._log_stanza(stanza)
 
-    def track_id(self, file_id):
+    def track_ids(self, file_ids):
         """We started tracking a new file id."""
-        stanza = self._create_action('track file-id')
-        stanza.add('file_id', file_id)
+        stanza = self._create_action('track file-ids')
+        for file_id in file_ids:
+            stanza.add('file_id', file_id)
         self._log_stanza(stanza)
 
-    def stop_tracking(self, file_id):
+    def stop_tracking(self, file_ids):
         """We stopped tracking this file id."""
-        stanza = self._create_action('untrack file-id')
-        stanza.add('file_id', file_id)
+        stanza = self._create_action('untrack file-ids')
+        for file_id in file_ids:
+            stanza.add('file_id', file_id)
         self._log_stanza(stanza)
 
 
@@ -432,6 +435,8 @@
 
     def _get_tracked_paths(self):
         """Find the set of paths that are tracked."""
+        # TODO: this may be a good function to memoize as long as we are
+        #       holding the same LockStore object
         ls = self._get_lock_store()
         paths = []
         for file_id in ls._locking_info._tracked_ids:

=== modified file 'tests/test_file_lock.py'
--- a/tests/test_file_lock.py	2009-09-22 21:00:47 +0000
+++ b/tests/test_file_lock.py	2009-09-22 21:08:24 +0000
@@ -84,11 +84,11 @@
     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')
+        li.track(['file-id-2'])
         self.assertEqual(set(['file-id-1', 'file-id-2']), li._tracked_ids)
-        li.stop_tracking('file-id-1')
+        li.stop_tracking(['file-id-1'])
         self.assertEqual(set(['file-id-2']), li._tracked_ids)
-        self.assertRaises(KeyError, li.stop_tracking, 'file-id-1')
+        self.assertRaises(KeyError, li.stop_tracking, ['file-id-1'])
 
     def test_create_lock(self):
         li = file_lock.FileLockingInfo(['file-id-1', 'file-id-2'], {})
@@ -165,7 +165,7 @@
     def test_stop_tracking_locked(self):
         li = file_lock.FileLockingInfo(['file-id-1', 'file-id-2'], {})
         li.create_lock('file-id-1')
-        self.assertRaises(ValueError, li.stop_tracking, 'file-id-1')
+        self.assertRaises(ValueError, li.stop_tracking, ['file-id-1'])
 
 
 class TestHeldLockSerializer(tests.TestCase):
@@ -278,7 +278,7 @@
         tree.lock_write()
         manager.initialize_lock_store(self.get_transport('lock-store'))
         self.knownFailure('not implemented yet.')
-        manager.track('b')
+        manager.track(['b'])
         self.assertFalse(manager.is_locked('b'))
         self.assertFalse(manager.is_tracked('a'))
         self.assertTrue(manager.is_tracked('b'))
@@ -325,9 +325,7 @@
         tree, manager = self.make_simple_tree_and_manager('tree')
         manager.initialize_lock_store(self.get_transport('lock-store'))
         ls = manager._lock_store
-        ls._locking_info.track('file-id')
-        ls._locking_info.track('dir-id')
-        ls._locking_info.track('not-in-this-tree-id')
+        ls._locking_info.track(['file-id', 'dir-id', 'not-in-this-tree-id'])
         self.assertEqual(['adir', 'afile'],
                          sorted(manager._get_tracked_paths()))
 
@@ -342,6 +340,10 @@
         self.assertEqual({'afile': None, 'adir': None, 'adir/subfile': None},
                          manager.tracked_at(['afile', 'adir', 'adir/subfile']))
 
+    def test_track(self):
+        tree, manager = self.make_simple_tree_and_manager('tree')
+        manager.track(['afile'])
+
 
 class TestFileLockStore(tests.TestCaseWithMemoryTransport):
 
@@ -442,32 +444,34 @@
             'locked-time: \\d+\n',
             flags=re.MULTILINE)
 
-    def test_track_id(self):
+    def test_track_ids(self):
         li, logger = self.make_locking_info()
-        li.track('file-id-3')
+        li.track(['file-id-2', 'file-id-3'])
         logger.flush()
         self.assertContainsRe(logger._transport.get_bytes(logger._filename),
             '\n'# end of previous action
             '\n'# extra separator line
-            'action: track file-id\n'
+            'action: track file-ids\n'
             'time: \\d+\n'
             'date: .*\n'
             'user: .*\n'
             'host: .*\n'
+            'file_id: file-id-2\n'
             'file_id: file-id-3\n',
             flags=re.MULTILINE)
 
     def test_stop_tracking(self):
         li, logger = self.make_locking_info()
-        li.stop_tracking('file-id-2')
+        li.stop_tracking(['file-id-1', 'file-id-2'])
         logger.flush()
         self.assertContainsRe(logger._transport.get_bytes(logger._filename),
             '\n'# end of previous action
             '\n'# extra separator line
-            'action: untrack file-id\n'
+            'action: untrack file-ids\n'
             'time: \\d+\n'
             'date: .*\n'
             'user: .*\n'
             'host: .*\n'
+            'file_id: file-id-1\n'
             'file_id: file-id-2\n',
             flags=re.MULTILINE)



More information about the bazaar-commits mailing list