Rev 32: Implement track_paths and tracked_at functionality in lp:~jameinel/+junk/file_locking

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


At lp:~jameinel/+junk/file_locking

------------------------------------------------------------
revno: 32
revision-id: john at arbash-meinel.com-20090922212258-es8a6g1v738rse21
parent: john at arbash-meinel.com-20090922210824-qaha0fffgcjsu4z0
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: file_locking
timestamp: Tue 2009-09-22 16:22:58 -0500
message:
  Implement track_paths and tracked_at functionality
  
  We can add new items to the tracked list, and then see that
  paths show up as being tracked after the fact.
-------------- next part --------------
=== modified file 'file_lock.py'
--- a/file_lock.py	2009-09-22 21:08:24 +0000
+++ b/file_lock.py	2009-09-22 21:22:58 +0000
@@ -212,7 +212,7 @@
         info = self._active_locks.pop(file_id)
         self._logger.lock_removed(info)
 
-    def track(self, file_ids):
+    def track_ids(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)
@@ -430,14 +430,30 @@
             self._lock_store = FileLockStore.open(trans)
         return self._lock_store
 
-    def track(self, path):
-        """Start tracking the given path."""
+    def track_paths(self, paths):
+        """Start tracking the given paths."""
+        ls = self._get_lock_store()
+        if ls is None:
+            raise ValueError('Cannot track paths without first initializing'
+                             ' a lock store.')
+        file_ids = []
+        for path in paths:
+            file_id = self._wt.path2id(path)
+            if file_id is None:
+                # TODO: Custom exception
+                raise ValueError('path %s not in wt %s'  % (path, self._wt))
+            file_ids.append(file_id)
+        ls._locking_info.track_ids(file_ids)
+        # TODO: We should make sure to take a write lock, and mark this as
+        #       dirty, etc.
 
     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()
+        if ls is None:
+            return []
         paths = []
         for file_id in ls._locking_info._tracked_ids:
             try:
@@ -468,7 +484,17 @@
         ls = self._get_lock_store()
         if ls is None:
             return dict.fromkeys(paths, None)
-        trackers = dict.fromkeys(paths, None)
+        tracked_paths = self._get_tracked_paths()
+        # TODO: Figure out matches when locks are nested
+        # tracked_paths.sort(key=len, reverse=True)
+        trackers = {}
+        for path in paths:
+            for t_path in tracked_paths:
+                if osutils.is_inside(t_path, path):
+                    trackers[path] = t_path
+                    break
+            else:
+                trackers[path] = None
         return trackers
 
     def is_tracked(self, path):

=== modified file 'tests/test_file_lock.py'
--- a/tests/test_file_lock.py	2009-09-22 21:08:24 +0000
+++ b/tests/test_file_lock.py	2009-09-22 21:22:58 +0000
@@ -84,7 +84,7 @@
     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_ids(['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)
@@ -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_paths(['b'])
         self.assertFalse(manager.is_locked('b'))
         self.assertFalse(manager.is_tracked('a'))
         self.assertTrue(manager.is_tracked('b'))
@@ -323,9 +323,10 @@
 
     def test__get_tracked_paths(self):
         tree, manager = self.make_simple_tree_and_manager('tree')
+        self.assertEqual([], manager._get_tracked_paths())
         manager.initialize_lock_store(self.get_transport('lock-store'))
         ls = manager._lock_store
-        ls._locking_info.track(['file-id', 'dir-id', 'not-in-this-tree-id'])
+        ls._locking_info.track_ids(['file-id', 'dir-id', 'not-in-this-tree-id'])
         self.assertEqual(['adir', 'afile'],
                          sorted(manager._get_tracked_paths()))
 
@@ -340,9 +341,23 @@
         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'])
+    def test_track_paths_not_in_tree(self):
+        tree, manager = self.make_simple_tree_and_manager('tree')
+        manager.initialize_lock_store(self.get_transport('lock-store'))
+        self.assertRaises(ValueError, manager.track_paths, ['no-such-file'])
+
+    def test_track_paths(self):
+        tree, manager = self.make_simple_tree_and_manager('tree')
+        # No lock store configured, can't track anything
+        # TODO: CustomError
+        self.assertRaises(ValueError, manager.track_paths, ['afile', 'adir'])
+        manager.initialize_lock_store(self.get_transport('lock-store'))
+        manager.track_paths(['afile', 'adir'])
+        self.assertEqual(['adir', 'afile'],
+                         sorted(manager._get_tracked_paths()))
+        self.assertEqual({'afile': 'afile', 'adir': 'adir',
+                          'adir/subfile': 'adir'},
+                         manager.tracked_at(['afile', 'adir', 'adir/subfile']))
 
 
 class TestFileLockStore(tests.TestCaseWithMemoryTransport):
@@ -446,7 +461,7 @@
 
     def test_track_ids(self):
         li, logger = self.make_locking_info()
-        li.track(['file-id-2', 'file-id-3'])
+        li.track_ids(['file-id-2', 'file-id-3'])
         logger.flush()
         self.assertContainsRe(logger._transport.get_bytes(logger._filename),
             '\n'# end of previous action



More information about the bazaar-commits mailing list