# Add console handler to logger.
logger.addHandler(ch)
+# Define ONE_SECOND as a datatime.timedelta object.
+ONE_SECOND = datetime.timedelta(seconds=1)
+
+
class SessionLogParser(object):
"""
Top-level class for parsing log file(s) for a given BitTorrent session.
libtorrent-rasterbar log folder parser.
"""
- def __init__(self, path, priority=None):
+ def __init__(self, path, start_time_string, priority=None):
"""
If priority == "verbose" parse verbose log files first. Else, parse
status file first."
"""
super(LibtorrentLogParser, self).__init__(path)
+ self.start_time = datetime.datetime.strptime(start_time_string,
+ "%Y-%m-%d %H:%M:%S")
# to_parse: list of files to be parsed
# have_parsed: list of files that have been parsed
self.have_parsed = []
self.f = None # handler to file being parsed
+
+ # IP address and port are parsed from verbose log file name.
self.verbose_log_ip = None
self.verbose_log_port = None
- self.log_year = "2009"
+ self.statmsg_count = 0
for entry in os.listdir(self.path):
entry_path = os.path.join(self.path, entry)
ps: 1, dht: 8 <> dl: 119.51kb/s, ul: 3.63kb/s <> dld: 1mb, uld: 0mb, size: 698mb <> eta: 1h 39m 37s
"""
msg = storage.StatusMessage()
- # TODO: Fix timestamp.
- msg.timestamp = None
+
+ # Timestamp is swarm start_time plus a number of seconds equal to
+ # the number of messages. It is presumed that each message is
+ # delivered periodically each second.
+ msg.timestamp = self.start_time + self.statmsg_count * ONE_SECOND
+ self.statmsg_count = self.statmsg_count + 1
string_array = re.split("\ *[,<>]+\ *", line)
for string in string_array:
def parse_peer_status_log_line(self, line):
pass
- def string_to_timestamp(self, date_string):
- """Convert string 'Mon DD HH:MM:SS' to datetime."""
- try:
- my_time = time.strptime(date_string + " %s"
- % (self.log_year),
- "%b %d %H:%M:%S %Y")
- my_date = datetime.datetime(my_time[0], my_time[1],
- my_time[2], my_time[3], my_time[4],
- my_time[5], my_time[6])
- except ValueError:
- return None
-
- return my_date
-
def parse_verbose_log_line(self, line, message_type):
if self.verbose_log_ip == None or self.verbose_log_port == None:
# TODO: Initiate verbose_log_ip and verbose_log_port.
else:
transfer_direction = None
+ # Convert string 'Mon DD HH:MM:SS' to datetime.
parts = re.split(r'[<=>]+', line)
- timestamp = self.string_to_timestamp(parts[0].strip())
+ date_string = parts[0].strip() + " %s" %(self.start_time.year)
+ timestamp = datetime.datetime.strptime(date_string,
+ "%b %d %H:%M:%S %Y")
return (timestamp, transfer_direction)
"""
exception_raised = False
try:
- p = parsing.LibtorrentLogParser(self.no_dir)
+ p = parsing.LibtorrentLogParser(self.no_dir,
+ "2009-01-01 12:13:14")
except OSError, e:
exception_raised = True
exception_raised = False
try:
- p = parsing.LibtorrentLogParser(self.ok_dir)
+ p = parsing.LibtorrentLogParser(self.ok_dir,
+ "2009-01-01 12:13:14")
except (IOError, OSError, IndexError), e:
exception_raised = True
# Remove status log file.
os.remove(self.ok_status_file_path)
- p = parsing.LibtorrentLogParser(self.ok_dir)
+ p = parsing.LibtorrentLogParser(self.ok_dir, "2009-01-01 12:13:14")
current = p.get_current_parsing_filename()
value = (current == self.ok_verbose_file_path1 or
# 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)
+ p = parsing.LibtorrentLogParser(self.ok_dir, "2009-01-01 12:13:14")
count = len(p.get_to_parse_list())
self.assertEqual(count, 2)
os.remove(self.ok_verbose_file_path2)
os.remove(self.ok_verbose_file_path3)
- p = parsing.LibtorrentLogParser(self.ok_dir)
+ p = parsing.LibtorrentLogParser(self.ok_dir, "2009-01-01 12:13:14")
current = p.get_current_parsing_filename()
value = (current == self.ok_status_file_path)
# to_parse list should be empty: no verbose files and the
# status file is currently being parsed.
- p = parsing.LibtorrentLogParser(self.ok_dir)
+ p = parsing.LibtorrentLogParser(self.ok_dir, "2009-01-01 12:13:14")
count = len(p.get_to_parse_list())
self.assertEqual(count, 0)
"""Set priority as "status".
get_current_parsing_filename() should return status log file.
"""
- p = parsing.LibtorrentLogParser(self.ok_dir, priority="status")
+ p = parsing.LibtorrentLogParser(self.ok_dir,
+ "2009-01-01 12:13:14", priority="status")
current = p.get_current_parsing_filename()
value = (current == self.ok_status_file_path)
"""Set priority as "status".
get_current_parsing_filename() should return a verbose log file.
"""
- p = parsing.LibtorrentLogParser(self.ok_dir, priority="verbose")
+ p = parsing.LibtorrentLogParser(self.ok_dir,
+ "2009-01-01 12:13:14", priority="verbose")
current = p.get_current_parsing_filename()
value = (current == self.ok_verbose_file_path1 or
# Remove status log file.
os.remove(self.ok_status_file_path)
- p = parsing.LibtorrentLogParser(self.ok_dir)
+ p = parsing.LibtorrentLogParser(self.ok_dir, "2009-01-01 12:13:14")
msg = p.get_next_message()
self.assertEqual(msg, None)
os.remove(self.ok_verbose_file_path2)
os.remove(self.ok_verbose_file_path3)
- p = parsing.LibtorrentLogParser(self.ok_dir)
+ p = parsing.LibtorrentLogParser(self.ok_dir, "2009-01-01 12:13:14")
msg = p.get_next_message()
self.assertEqual(msg, None)
"""Log files are empty.
Returned message should be None.
"""
- p = parsing.LibtorrentLogParser(self.ok_dir)
+ p = parsing.LibtorrentLogParser(self.ok_dir, "2009-01-01 12:13:14")
msg = p.get_next_message()
self.assertEqual(msg, None)
self.add_bogus_line(self.ok_status_file_path)
self.add_verbose_line(self.ok_verbose_file_path1)
- p = parsing.LibtorrentLogParser(self.ok_dir, priority="status")
+ p = parsing.LibtorrentLogParser(self.ok_dir,
+ "2009-01-01 12:13:14", priority="status")
msg = p.get_next_message()
self.add_message_to_count(msg)
self.add_bogus_line(self.ok_verbose_file_path3)
self.add_status_line(self.ok_status_file_path)
- p = parsing.LibtorrentLogParser(self.ok_dir, priority="verbose")
+ p = parsing.LibtorrentLogParser(self.ok_dir,
+ "2009-01-01 12:13:14", priority="verbose")
msg = p.get_next_message()
self.add_message_to_count(msg)
"""Return a status message from status log file."""
self.add_status_line(self.ok_status_file_path)
- p = parsing.LibtorrentLogParser(self.ok_dir, priority="verbose")
+ p = parsing.LibtorrentLogParser(self.ok_dir,
+ "2009-01-01 12:13:14", priority="verbose")
msg = p.get_next_message()
self.add_message_to_count(msg)
self.add_status_line(self.ok_status_file_path)
self.add_status_line(self.ok_status_file_path)
- p = parsing.LibtorrentLogParser(self.ok_dir, priority="verbose")
+ p = parsing.LibtorrentLogParser(self.ok_dir,
+ "2009-01-01 12:13:14", priority="verbose")
msg1 = p.get_next_message()
msg2 = p.get_next_message()
self.add_status_line(self.ok_status_file_path)
self.add_status_line(self.ok_verbose_file_path1)
- p = parsing.LibtorrentLogParser(self.ok_dir, priority="verbose")
+ p = parsing.LibtorrentLogParser(self.ok_dir,
+ "2009-01-01 12:13:14", priority="verbose")
msg1 = p.get_next_message()
msg2 = p.get_next_message()
"""Return a peer status message from status log file."""
self.add_peer_status_line(self.ok_status_file_path)
- p = parsing.LibtorrentLogParser(self.ok_dir, priority="verbose")
+ p = parsing.LibtorrentLogParser(self.ok_dir,
+ "2009-01-01 12:13:14", priority="verbose")
msg = p.get_next_message()
self.add_message_to_count(msg)
self.add_peer_status_line(self.ok_status_file_path)
self.add_peer_status_line(self.ok_status_file_path)
- p = parsing.LibtorrentLogParser(self.ok_dir, priority="verbose")
+ p = parsing.LibtorrentLogParser(self.ok_dir,
+ "2009-01-01 12:13:14", priority="verbose")
msg1 = p.get_next_message()
msg2 = p.get_next_message()
self.add_peer_status_line(self.ok_status_file_path)
self.add_peer_status_line(self.ok_verbose_file_path1)
- p = parsing.LibtorrentLogParser(self.ok_dir, priority="verbose")
+ p = parsing.LibtorrentLogParser(self.ok_dir,
+ "2009-01-01 12:13:14", priority="verbose")
msg1 = p.get_next_message()
msg2 = p.get_next_message()
"""Return a verbose message from verbose log file."""
self.add_verbose_line(self.ok_verbose_file_path1)
- p = parsing.LibtorrentLogParser(self.ok_dir, priority="status")
+ p = parsing.LibtorrentLogParser(self.ok_dir,
+ "2009-01-01 12:13:14", priority="status")
msg = p.get_next_message()
self.add_message_to_count(msg)
self.add_verbose_line(self.ok_verbose_file_path1)
self.add_verbose_line(self.ok_verbose_file_path1)
- p = parsing.LibtorrentLogParser(self.ok_dir, priority="verbose")
+ p = parsing.LibtorrentLogParser(self.ok_dir,
+ "2009-01-01 12:13:14", priority="verbose")
msg1 = p.get_next_message()
msg2 = p.get_next_message()
self.add_verbose_line(self.ok_verbose_file_path1)
self.add_verbose_line(self.ok_verbose_file_path2)
- p = parsing.LibtorrentLogParser(self.ok_dir, priority="verbose")
+ p = parsing.LibtorrentLogParser(self.ok_dir,
+ "2009-01-01 12:13:14", priority="verbose")
msg1 = p.get_next_message()
msg2 = p.get_next_message()
self.add_peer_status_line(self.ok_status_file_path)
self.add_verbose_line(self.ok_status_file_path)
- p = parsing.LibtorrentLogParser(self.ok_dir, priority="verbose")
+ p = parsing.LibtorrentLogParser(self.ok_dir,
+ "2009-01-01 12:13:14", priority="verbose")
msg1 = p.get_next_message()
msg2 = p.get_next_message()
msg3 = p.get_next_message()
self.add_verbose_line(self.ok_verbose_file_path2)
self.add_verbose_line(self.ok_verbose_file_path2)
- p = parsing.LibtorrentLogParser(self.ok_dir, priority="verbose")
+ p = parsing.LibtorrentLogParser(self.ok_dir,
+ "2009-01-01 12:13:14", priority="verbose")
msg1 = p.get_next_message()
msg2 = p.get_next_message()
msg3 = p.get_next_message()
self.add_verbose_line(self.ok_verbose_file_path2)
self.add_verbose_line(self.ok_verbose_file_path2)
- p = parsing.LibtorrentLogParser(self.ok_dir, priority="verbose")
+ p = parsing.LibtorrentLogParser(self.ok_dir,
+ "2009-01-01 12:13:14", priority="verbose")
msg1 = p.get_next_message()
msg2 = p.get_next_message()
msg3 = p.get_next_message()