ppf/new: Add util.py.
authorRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Fri, 26 Aug 2011 10:34:47 +0000 (13:34 +0300)
committerRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Fri, 26 Aug 2011 10:34:49 +0000 (13:34 +0300)
util.py contains the LiveLogFile for live analysis of log files. A
specialized status file holds the current position of processing the log
file. In case of error, the analyzer would restart, read the status file
and resume parsing from where it left off.

ppf/new/util.py [new file with mode: 0644]

diff --git a/ppf/new/util.py b/ppf/new/util.py
new file mode 100644 (file)
index 0000000..d807af5
--- /dev/null
@@ -0,0 +1,71 @@
+"""
+Utility classes.
+
+2011, Razvan Deaconescu, razvan.deaconescu@cs.pub.ro
+"""
+
+class LiveLogStatus(object):
+    """
+    Status information regarding live log processing. Stored in the
+    '.lstatus' file.
+    """
+
+    def __init__(self, position=0):
+        self.position = 0
+
+
+class LiveLogFile(object):
+    """
+    Encapsulate file that is currently updated/appended as it is used
+    by a log file.
+    A live status file is used for storing status information (such as
+    current position in log file processing). The live status file name is the
+    log file name with ".lstatus" appended to it.
+    """
+
+    def __init__(self, log_file_name):
+        self.log_file_name = log_file_name
+        self.status_file_name = log_file_name + ".lstatus"
+
+        self.log_file = open(self.log_file_name, "tt")
+        self.status_file = open(self.status_file_name, "ab+")
+        self.status_file.seek(0)
+
+        self.load_status()
+        self.log_file.seek(self.status.position)
+
+    def close(self):
+        self.log_file.close()
+        self.status_file.close()
+
+    def load_status(self):
+        self.status_file.seek(0)
+
+        # If line is empty (file is just created), position is 0
+        line = self.status_file.readline()
+        try:
+            position = int(line)
+        except TypeError, e:
+            position = 0
+        self.status = LiveLogStatus(position)
+
+        self.status_file.seek(0)
+
+    def store_status(self):
+        self.status_file.seek(0)
+        self.status.position = self.log_file.tell()
+        self.status_file.write(str(self.status.position) + "\n")
+        self.status_file.seek(0)
+
+    def readline(self):
+        current_position = self.log_file.tell()
+        line = self.log_file.readline()
+
+        # In case of complete line, update status and return it.
+        if "\n" in line:
+            self.store_status()
+            return line
+
+        # In case of incomplete line, rewind and return "".
+        self.log_file.seek(current_position)
+        return ""