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
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]
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
logging.basicConfig(level = logging.DEBUG)
self.logger = logging.getLogger('autorun.Server')
self.logger.setLevel(logging.DEBUG)
+
def set_address(self, ip):
self.ip = ip
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):
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):
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
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
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 = {}
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):
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):
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):
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)
"""
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
"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.
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");