Rev 25: Add logging of when new file-ids are tracked. in lp:~jameinel/+junk/file_locking

John Arbash Meinel john at arbash-meinel.com
Mon Sep 21 22:06:42 BST 2009


At lp:~jameinel/+junk/file_locking

------------------------------------------------------------
revno: 25
revision-id: john at arbash-meinel.com-20090921210631-yzluxr602ci0ziek
parent: john at arbash-meinel.com-20090921205645-90miwm287elvhgvg
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: file_locking
timestamp: Mon 2009-09-21 16:06:31 -0500
message:
  Add logging of when new file-ids are tracked.
  
  Refactor the internals a bit so that all actions generally have the same
  header information about who did what.
-------------- next part --------------
=== modified file 'file_lock.py'
--- a/file_lock.py	2009-09-21 20:56:45 +0000
+++ b/file_lock.py	2009-09-21 21:06:31 +0000
@@ -214,6 +214,7 @@
 
     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 stop_tracking(self, file_id):
@@ -236,15 +237,8 @@
     @classmethod
     def initialize(cls, transport, filename):
         """Create a new Log instance."""
-        global_conf = _mod_config.GlobalConfig()
-        stanza = rio.Stanza()
-        stanza.add('action', 'Lock Store created')
-        t = int(time.time())
-        stanza.add('time', str(t))
-        stanza.add('date', osutils.format_date(t,
-            offset=osutils.local_time_offset()))
-        stanza.add('user', global_conf.username())
-        stanza.add('host', osutils.get_host_name())
+        logger = cls(transport, filename=filename)
+        stanza = logger._create_action('Lock Store created')
         transport.put_bytes_non_atomic('log', stanza.to_string())
         return cls(transport, filename=filename)
 
@@ -253,6 +247,17 @@
         self._pending.append('\n')
         self._pending.extend(stanza.to_lines())
 
+    def _create_action(self, action):
+        global_conf = _mod_config.GlobalConfig()
+        stanza = rio.Stanza(action=action)
+        t = int(time.time())
+        stanza.add('time', str(t))
+        stanza.add('date', osutils.format_date(t,
+            offset=osutils.local_time_offset()))
+        stanza.add('user', global_conf.username())
+        stanza.add('host', osutils.get_host_name())
+        return stanza
+
     def flush(self):
         if self._pending:
             pending = self._pending
@@ -261,25 +266,27 @@
 
     def lock_created(self, info):
         """A lock was created, log this info."""
-        s = rio.Stanza(action='lock created')
-        s.add('date', osutils.format_date(int(info['time']),
-            offset=osutils.local_time_offset()))
+        stanza = self._create_action('lock created')
         for key, value in info.iter_pairs():
-            s.add(key, value)
-        self._log_stanza(s)
+            if key in ('time', 'user', 'host'):
+                continue
+            stanza.add(key, value)
+        self._log_stanza(stanza)
 
     def lock_removed(self, info):
-        global_conf = _mod_config.GlobalConfig()
-        s = rio.Stanza(action='lock removed')
-        t = int(time.time())
-        s.add('time', str(t))
-        s.add('date', osutils.format_date(t,
-            offset=osutils.local_time_offset()))
-        s.add('user', global_conf.username())
-        s.add('host', osutils.get_host_name())
-        s.add('file_id', info['file_id'])
-        s.add('nonce', info['nonce'])
-        self._log_stanza(s)
+        """Someone removed a lock."""
+        stanza = self._create_action('lock removed')
+        stanza.add('file_id', info['file_id'])
+        stanza.add('nonce', info['nonce'])
+        stanza.add('locking-user', info['user'])
+        stanza.add('locked-time', info['time'])
+        self._log_stanza(stanza)
+
+    def track_id(self, file_id):
+        """We started tracking a new file id."""
+        stanza = self._create_action('track file-id')
+        stanza.add('file_id', file_id)
+        self._log_stanza(stanza)
 
 
 class NullFileLockLogger(FileLockLogger):

=== modified file 'tests/test_file_lock.py'
--- a/tests/test_file_lock.py	2009-09-21 20:56:45 +0000
+++ b/tests/test_file_lock.py	2009-09-21 21:06:31 +0000
@@ -373,9 +373,11 @@
             '\n'# end of previous action
             '\n'# extra separator line
             'action: lock created\n'
+            'time: \\d.*\n'
             'date: .*\n'
+            'user: .*\n'
+            'host: .*\n'
             'file_id: file-id-1\n'
-            'host: .*\n'
             'nonce: .*\n'
             '.*\n', flags=re.MULTILINE)
 
@@ -395,5 +397,24 @@
             'user: .*\n' #User that unlocked it
             'host: .*\n'
             'file_id: .*\n'
-            'nonce: .*\n',
+            'nonce: .*\n'
+            'locking-user: .*\n' # User that created the lock
+            'locked-time: \\d+\n',
+            flags=re.MULTILINE)
+
+    def test_track_id(self):
+        logger = self.make_logger()
+        li = file_lock.FileLockingInfo(['file-id-1', 'file-id-2'], {},
+                                       logger)
+        li.track('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'
+            'time: \\d+\n'
+            'date: .*\n'
+            'user: .*\n'
+            'host: .*\n'
+            'file_id: .*\n',
             flags=re.MULTILINE)



More information about the bazaar-commits mailing list