ppf/new: Updates and new tests for parsing module.
authorRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Sat, 27 Aug 2011 08:12:48 +0000 (11:12 +0300)
committerRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Sat, 27 Aug 2011 08:12:50 +0000 (11:12 +0300)
ppf/new/parsing.py
ppf/new/tests/test_parsing.py

index 3177b5c..b303129 100644 (file)
@@ -12,6 +12,7 @@ import os.path
 import re
 import datetime
 import logging
+import socket
 
 import storage      # Use *Message classes.
 
@@ -55,7 +56,7 @@ class SessionLogParser(object):
         """
         return None
 
-    def get_current_parsing_filename():
+    def get_current_parsing_filename(self):
         """Return the name of the log file being currently parsed."""
         return self.parsing
 
@@ -82,21 +83,52 @@ class LibtorrentLogParser(SessionLogParser):
         for entry in os.listdir(self.path):
             entry_path = os.path.join(self.path, entry)
             if os.path.isfile(entry_path):
-                # TODO: If entry is file and name is IP_PORT.log add it to list.
-                if True:
+                # If entry is file and name is IP_PORT.log add it to list.
+                if self.is_verbose_log_filename(entry):
+                    logger.debug("Add entry path %s." %(entry_path))
                     self.to_parse.append(entry_path)
 
         status_file_path = os.path.join(self.path, "status.log")
-        # TODO: Check if status file exists and is a file.
-
-        # List functions as a stack. First files go to the end.
-        if priority == "verbose":
-            self.to_parse.insert(0, status_file_path)
-        else:
-            self.to_parse.append(status_file_path)
+        # In case status file doesn't exist, skip it.
+        if os.access(status_file_path, os.F_OK) and \
+                os.access(status_file_path, os.R_OK):
+            logger.debug("Status file exists: %s." %(status_file_path))
+            # List functions as a stack. First files go to the end.
+            if priority == "verbose":
+                self.to_parse.insert(0, status_file_path)
+            else:
+                self.to_parse.append(status_file_path)
 
         self.open_next_file()
 
+    def get_to_parse_list(self):
+        return self.to_parse
+
+    def get_have_parsed_list(self):
+        return self.have_parsed
+
+    def is_verbose_log_filename(self, filename):
+        r = re.compile(r'^[0-9]+(?:\.[0-9]+){3}_[0-9]+\.log$')
+        if not r.match(filename):
+            return False
+
+        # Check for valid IP address and port.
+        a = re.split('_', filename)
+        ip = a[0]
+        port = int(a[1].split('.')[0])
+
+        # Check valid port.
+        if port <= 0 or port > 65535:
+            return False
+
+        # Check valid IP address.
+        try:
+            socket.inet_aton(ip)
+        except socket.error, e:
+            return False
+
+        return True
+
     def open_next_file(self):
         """
         Open next log file from to_parse list.
@@ -108,8 +140,8 @@ class LibtorrentLogParser(SessionLogParser):
             self.f.close()
             self.have_parsed.append(parsing)
 
-        parsing = self.to_parse.pop()
-        self.f = open(parsing, 'r')
+        self.parsing = self.to_parse.pop()
+        self.f = open(self.parsing, 'r')
 
         # TODO: Log this information somewhere for snapshotting purpose.
         # In case an error occurs parsing would resume from that point.
index b354ccc..4840537 100644 (file)
@@ -21,6 +21,9 @@ class LibtorrentLogParserTest(unittest.TestCase):
     verbose_file1 = "141.85.224.201_12345.log"
     verbose_file2 = "141.85.224.202_12345.log"
     verbose_file3 = "141.85.224.203_12345.log"
+    bogus_file1 = "dht.log"
+    bogus_file2 = "141.85.224.203.log"
+    bogus_file3 = "12345.log"
 
     def setUp(self):
         # Create folder and log files.
@@ -33,6 +36,12 @@ class LibtorrentLogParserTest(unittest.TestCase):
                 self.verbose_file2)
         self.ok_verbose_file_path3 = os.path.join(self.ok_dir,
                 self.verbose_file3)
+        self.ok_bogus_file_path1 = os.path.join(self.ok_dir,
+                self.bogus_file1)
+        self.ok_bogus_file_path2 = os.path.join(self.ok_dir,
+                self.bogus_file2)
+        self.ok_bogus_file_path3 = os.path.join(self.ok_dir,
+                self.bogus_file3)
         f = open(self.ok_status_file_path, "w")
         f.close()
         f = open(self.ok_verbose_file_path1, "w")
@@ -41,6 +50,12 @@ class LibtorrentLogParserTest(unittest.TestCase):
         f.close()
         f = open(self.ok_verbose_file_path3, "w")
         f.close()
+        f = open(self.ok_bogus_file_path1, "w")
+        f.close()
+        f = open(self.ok_bogus_file_path2, "w")
+        f.close()
+        f = open(self.ok_bogus_file_path3, "w")
+        f.close()
 
         # Remove no_dir in case it exists.
         try:
@@ -78,9 +93,10 @@ class LibtorrentLogParserTest(unittest.TestCase):
         os.remove(self.ok_verbose_file_path2)
         os.remove(self.ok_verbose_file_path3)
 
+        exception_raised = False
         try:
             p = parsing.LibtorrentLogParser(self.ok_dir)
-        except (IOError, OSError), e:
+        except (IOError, OSError, IndexError), e:
             exception_raised = True
 
         self.assertEqual(exception_raised, True)
@@ -89,13 +105,61 @@ class LibtorrentLogParserTest(unittest.TestCase):
         """No status log file is present.
         Use get_current_parsing_filename() to get current log file.
         """
-        self.assertEqual(True, False)
+        # Remove status log file.
+        os.remove(self.ok_status_file_path)
+
+        p = parsing.LibtorrentLogParser(self.ok_dir)
+        current = p.get_current_parsing_filename()
+
+        value = (current == self.ok_verbose_file_path1 or
+                current == self.ok_verbose_file_path2 or
+                current == self.ok_verbose_file_path3)
+        self.assertEqual(value, True)
+
+    def test_get_to_parse_files_no_status_log_files(self):
+        """No status log file is present.
+        Use get_to_parse_list() to get files to be parsed.
+        """
+        # Remove status log file.
+        os.remove(self.ok_status_file_path)
+
+        # to_parse list should consists of 2 entries: the verbose log
+        # files minus the one that is currently being parsed.
+        p = parsing.LibtorrentLogParser(self.ok_dir)
+        count = len(p.get_to_parse_list())
+
+        self.assertEqual(count, 2)
 
     def test_get_parsing_file_no_verbose_log_files(self):
         """No verbose log file is present.
         Use get_current_parsing_filename() to get current log file.
         """
-        self.assertEqual(True, False)
+        # Remove status log file.
+        os.remove(self.ok_verbose_file_path1)
+        os.remove(self.ok_verbose_file_path2)
+        os.remove(self.ok_verbose_file_path3)
+
+        p = parsing.LibtorrentLogParser(self.ok_dir)
+        current = p.get_current_parsing_filename()
+
+        value = (current == self.ok_status_file_path)
+        self.assertEqual(value, True)
+
+    def test_get_to_parse_files_no_verbose_log_files(self):
+        """No verbose log file is present.
+        Use get_to_parse_list() to get files to be parsed.
+        """
+        # Remove status log file.
+        os.remove(self.ok_verbose_file_path1)
+        os.remove(self.ok_verbose_file_path2)
+        os.remove(self.ok_verbose_file_path3)
+
+        # to_parse list should be empty: no verbose files and the
+        # status file is currently being parsed.
+        p = parsing.LibtorrentLogParser(self.ok_dir)
+        count = len(p.get_to_parse_list())
+
+        self.assertEqual(count, 0)
 
     def test_get_parsing_status_priority(self):
         """Set priority as "status".