autorun: BitTorrentClientRun outputs python logging
authorAdriana Draghici <adriana008@gmail.com>
Thu, 29 Apr 2010 18:08:33 +0000 (18:08 +0000)
committerAdriana Draghici <adriana008@gmail.com>
Thu, 29 Apr 2010 18:08:33 +0000 (18:08 +0000)
autorun/server/BitTorrentClientRun.py
autorun/server/Server.py
autorun/server/TriblerRun.py

index 8d22d38..6bab9c5 100644 (file)
@@ -10,15 +10,18 @@ import os
 import os.path
 import subprocess
 import shlex
+import logging
 from string import Template
 
-DEBUG = True
-
 class BitTorrentClientRun:
+    my_logger = None 
     def __init__(self, base_path, simple_run_expr, logging_run_expr):
         self.base_path = base_path
         self.simple_run_expr = simple_run_expr
         self.logging_run_expr = logging_run_expr
+        
+        self.my_logger = logging.getLogger('autorun.BitTorrentClientRun')
+        self.my_logger.setLevel(logging.DEBUG) 
 
     def config_run(self, download_dir, output_dir, output_file, log_dir, log_file, port, torrent_file):
         self.download_dir = download_dir
@@ -68,21 +71,19 @@ class BitTorrentClientRun:
                 for j in range(i, len(args)):
                     args.pop(i)
                 break;
-        if(DEBUG):
-            print "BitTorrentClientRun: command =", args
+        #self.my_logger.debug(" BitTorrentClientRun: command =" +  str(args))
         
         log_redirect = open(self.log_dir+"/"+self.log_file,"w")
         output_redirect = open(self.output_dir+"/"+self.output_file,"w")
-        if(DEBUG):
-            print "output redirect to file ", (self.output_dir+"/"+self.output_file)
-            print "log redirect to file ", (self.log_dir+"/"+self.log_file)
+        
+        self.my_logger.debug(" output redirect to file " +  self.output_dir+"/"+self.output_file)
+        self.my_logger.debug(" log redirect to file " + self.log_dir+"/"+self.log_file)
         
         p=subprocess.Popen(args, shell=False, #does not create sh process
                             stdout=output_redirect,
                             stderr=log_redirect)
         pid = p.pid
-        if(DEBUG):
-            print "BitTorrentClientRun: pid =", pid
+        self.my_logger.debug(" BitTorrentClientRun: pid =" +  str(pid))
         return [pid, log_redirect, output_redirect]
         
 
index 8defe1b..b4739ff 100644 (file)
@@ -17,10 +17,13 @@ from TriblerRun import *
 from TriblerStatusParser import *
 
 class MyDaemon(Daemon):
+    """
+     Server class
+     2010, Adriana Draghici, adriana.draghici@cti.pub.ro
+    """
 
     ip = ""
     port = 0
-    DEBUG = True
 
     processes_fd = {}   # keeps lists of file descriptors for each process
     processes_info = {} # keeps lists of BT clients name, torrent name and status log name for each process
@@ -33,6 +36,7 @@ class MyDaemon(Daemon):
         logging.basicConfig(level = logging.DEBUG)
         self.logger = logging.getLogger('autorun.Server')
         self.logger.setLevel(logging.DEBUG) 
+
     def set_address(self, ip):
         self.ip = ip
     
@@ -54,7 +58,7 @@ class MyDaemon(Daemon):
                 break
         
         dd = pickle.loads(msg) 
-        self.logger.debug("  received message: ", dd)
+        self.logger.debug("  received message: " + str(dd))
         return dd
 
     def send_pickled_data (self, clientsock, data):
@@ -65,7 +69,7 @@ class MyDaemon(Daemon):
             if sent == 0:
                 raise RuntimeError,    "socket connection broken"
             totalsent = totalsent + sent
-        self.logger.debug("  sent message: ", data)
+        self.logger.debug("  sent message: " + str(data))
 
 
     def save_download_info(self, data):
@@ -104,7 +108,7 @@ class MyDaemon(Daemon):
         if LOG_FILE in info_dict:
             info_list.append((info_dict[LOG_FILE], LOG_FILE))
         
-        self.logger.debug("  read transfer log info: ", info_list)
+        self.logger.debug("  read transfer log info: " + str(info_list))
          
         file_list.append (info_list)
         return 0 # success
@@ -133,7 +137,7 @@ class MyDaemon(Daemon):
                 err_msg = "File does not exist: ", file_path
             else: archives_list.append(file_path)
         
-        self.logger.error(" ", err_msg)
+        self.logger.error(" " + err_msg)
 
     def read_download_info(self, file_list, create_archive = False, archives_list = []):
         """ Reads all the contents of the file that stores info about 
@@ -152,13 +156,12 @@ class MyDaemon(Daemon):
                 while line != '':
                     print line
                     if size == 0 :
-                        if info_dict != {} :
-                            if info_dict not in dicts:
-                                if self.add_to_output_msg(file_list, info_dict) < 0:
-                                    raise Exception
-                                dicts.append(info_dict)
-                                if create_archive:
-                                    self.archive_files(info_dict,archives_list)
+                        if info_dict != {} and info_dict not in dicts:
+                            if self.add_to_output_msg(file_list, info_dict) < 0:
+                                raise Exception
+                            dicts.append(info_dict)
+                            if create_archive:
+                                self.archive_files(info_dict,archives_list)
 
                         size = int(line)
                         info_dict = {}
@@ -186,22 +189,27 @@ class MyDaemon(Daemon):
 
 
     def get_client_status(self, transfer_id, line_parts = []):
+        """
+            Runs a parsers for the obtaining the last line from the 
+            torrent transfer's status file (output file).
+            @param transfer_id identifier for the transfer
+            @param line_parts list with line's components
+            @return error message if any
+        """
 
         if int(transfer_id) not in self.processes_info:
             return "Invalid transfer id: " + transfer_id
         status_file_path = self.processes_info[transfer_id][2]
         client = self.processes_info[transfer_id][0]
-        line = ""
         if client == TRIBLER:
             parser = TriblerStatusParser(status_file_path)
         elif client == HRKTORRENT:
             parser = TriblerStatusParser(status_file_path)
         else:
             return "Functionality not supported for client "+ client
-        line_parts.append(parser.parse_last_status_line(line))
+        line_parts.append(parser.parse_last_status_line(""))
         if line_parts == "":
             return "Error occured while reading status file " + status_file_path
-        self.logger.debug("  status_line = " + line)
         return ""
 
     def start_bt_client(self, bt_client_data):
@@ -232,7 +240,7 @@ class MyDaemon(Daemon):
                                     bt_client_data[TORRENT], 
                                     bt_client_data[OUT_DIR] + "/" + bt_client_data[OUT_FILE]]
         
-        self.logger.debug("  started client with pid = ", pid)
+        self.logger.debug("  started client with pid = " +  str(pid))
         return pid
 
     def stop_bt_client(self, pid):
@@ -249,7 +257,7 @@ class MyDaemon(Daemon):
         del self.processes_fd[int_pid]
         del self.processes_info[int_pid]
 
-        self.logger.debug("  killed process with pid = ", pid)
+        self.logger.debug("  killed process with pid = " + str(pid))
         return ""
 
     def set_linger(self,sock, l_onoff, l_linger):
@@ -270,7 +278,7 @@ class MyDaemon(Daemon):
         while(1):
             self.logger.debug("  accepting connections")
             (clientsock, address) = self.serversocket.accept();
-            self.logger.debug("  accepted connection from ", address)
+            self.logger.debug("  accepted connection from " +str(address))
             
             msg = self.recv_pickled_data(clientsock)
             
index dc06fce..d80cb61 100644 (file)
@@ -3,11 +3,13 @@
 """
  Base class for running Tribler client instances
  2010, Razvan Deaconescu, razvan.deaconescu@cs.pub.ro
+ 2010, Adriana Draghici, adriana.draghici@cti.pub.ro
 """
 
 import sys
 import os
 import os.path
+import logging
 from BitTorrentClientRun import BitTorrentClientRun
 
 DEBUG = True
@@ -21,6 +23,8 @@ class TriblerRun(BitTorrentClientRun):
                 "python $base_path/Tribler/Tools/cmdlinedl.py -o $download_dir -p $port $torrent_file 2> $log_dir/$log_file > $output_dir/$output_file",
                 "python $base_path/Tribler/Tools/cmdlinedl.py -o $download_dir -p $port $torrent_file 2> $log_dir/$log_file > $output_dir/$output_file")
          
+        self.my_logger = logging.getLogger('autorun.TriblerRun')
+        self.my_logger.setLevel(logging.DEBUG) 
 
     """
         Adds lines in cmdlinedl.py that set the PYTHONPATH variable.
@@ -29,7 +33,7 @@ class TriblerRun(BitTorrentClientRun):
         filename = base_path + "/Tribler/Tools/cmdlinedl.py"
         tmp_filename = filename + "_tmp"
         if not os.path.exists(filename) or not os.path.isfile(filename):
-            print "Error: invalid Tribler path ", filename
+            self.my_logger.error(" Invalid Tribler path ", filename)
             return
         f = open(filename, "r");
         f_copy = open(tmp_filename, "w");