Rev 23: Create a Logger class. in lp:~jameinel/+junk/file_locking

John Arbash Meinel john at arbash-meinel.com
Mon Sep 21 21:25:43 BST 2009


At lp:~jameinel/+junk/file_locking

------------------------------------------------------------
revno: 23
revision-id: john at arbash-meinel.com-20090921202534-ayf0xttklwoo5jcv
parent: john at arbash-meinel.com-20090921201949-sj8qm5wm6ahbemnu
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: file_locking
timestamp: Mon 2009-09-21 15:25:34 -0500
message:
  Create a Logger class.
-------------- next part --------------
=== modified file 'file_lock.py'
--- a/file_lock.py	2009-09-21 20:19:49 +0000
+++ b/file_lock.py	2009-09-21 20:25:34 +0000
@@ -216,6 +216,30 @@
         if file_id in self._active_locks:
             raise ValueError('{%s} has an active lock' % (file_id,))
         self._tracked_ids.remove(file_id)
+
+
+class FileLockLogger(object):
+    """Track actions that mutate the store."""
+
+    def __init__(self, transport, filename='log'):
+        """Create a new Logger."""
+        self._transport = transport
+        self._filename = filename
+
+    @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())
+        transport.put_bytes_non_atomic('log', stanza.to_string())
+        return cls(transport, filename=filename)
         
 
 class FileLockStore(object):
@@ -227,7 +251,7 @@
         3) Logging of locking/unlocking actions.
     """
 
-    def __init__(self, transport, locking_info, lock):
+    def __init__(self, transport, locking_info, lock, logger):
         """You should not call this directly.
 
         Instead call FileLockStore.initialize() or FileLockStore.open()
@@ -235,6 +259,7 @@
         self._transport = transport
         self._lock = lock
         self._locking_info = locking_info
+        self._logger = logger
 
     @classmethod
     def initialize(cls, transport):
@@ -259,17 +284,8 @@
             # the 'info' file. Which means that we have 1 less object to read
             # from remote.
             transport.put_bytes_non_atomic('info', bytes)
-            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())
-            transport.put_bytes_non_atomic('log', stanza.to_string())
-            store = cls(transport, locking_info, lock)
+            logger = FileLockLogger.initialize(transport, 'log')
+            store = cls(transport, locking_info, lock, logger)
         except:
             lock.unlock()
             raise
@@ -284,7 +300,8 @@
         s = FileLockingInfoSerializer()
         locking_info = s.from_bytes(info_bytes)
         lock = lockdir.LockDir(transport, 'lock')
-        return cls(transport, locking_info, lock)
+        logger = FileLockLogger(transport, 'log')
+        return cls(transport, locking_info, lock, logger)
 
     def unlock(self):
         self._lock.unlock()

=== modified file 'tests/test_file_lock.py'
--- a/tests/test_file_lock.py	2009-09-21 20:19:49 +0000
+++ b/tests/test_file_lock.py	2009-09-21 20:25:34 +0000
@@ -323,6 +323,7 @@
             'date: .*\n'
             'user: .*\n'
             'host: .*\n', flags=re.MULTILINE)
+        self.assertIsNot(None, fls._logger)
 
     def test_open_not_there(self):
         trans = self.get_transport('lock-store')



More information about the bazaar-commits mailing list