Remove empty HoleBot log files.
authorW. Trevor King <wking@drexel.edu>
Tue, 7 Dec 2010 17:59:40 +0000 (12:59 -0500)
committerW. Trevor King <wking@drexel.edu>
Tue, 7 Dec 2010 17:59:40 +0000 (12:59 -0500)
bots/hole_bot.py

index 5274c6943617a570914f15e6ab03a6d6383d6991..0c5b576c2e19e6bf14deec46dad4ad78f26f561b 100755 (executable)
@@ -5,6 +5,7 @@
 
 import logging
 import os
+import os.path
 import sys
 
 
@@ -22,10 +23,15 @@ class HoleBot (object):
         if stdout == None:
             stdout = sys.stdout
         self.stdout = stdout
-        log_file = '%s_%d.log' % (self.__class__.__name__, os.getpid())
-        logging.basicConfig(filename=log_file, level=log_level)
+        self.log_file = '%s_%d.log' % (self.__class__.__name__, os.getpid())
+        logging.basicConfig(filename=self.log_file, level=log_level)
         self.log = logging
 
+    def cleanup_log(self):
+        self.log = None
+        if os.path.getsize(self.log_file) == 0:
+            os.remove(self.log_file)
+
     def run(self):
         state = self._initial_state()
         while True:
@@ -37,6 +43,7 @@ class HoleBot (object):
                 self._process(tag, type, data, state)
             except RunComplete:
                 break
+        self.cleanup_log()
 
     def _parse(self, line):
         self.log.debug('read: %s' % line)