From 01aa8c18411c56bde1c6295c4c2094b03e6bdd8e Mon Sep 17 00:00:00 2001 From: Adriana Draghici Date: Sat, 17 Apr 2010 14:47:24 +0000 Subject: [PATCH] autorun: server receives ARCHIVE msg, archives and removes logs files, returns files list. --- autorun/PROTOCOL | 11 ++++--- autorun/Util.py | 17 ++++++++++ autorun/server/Client.py | 9 ++++-- autorun/server/Server.py | 70 +++++++++++++++++++++++++++++++++------- 4 files changed, 88 insertions(+), 19 deletions(-) diff --git a/autorun/PROTOCOL b/autorun/PROTOCOL index 14e6c5e..e036edc 100644 --- a/autorun/PROTOCOL +++ b/autorun/PROTOCOL @@ -29,16 +29,19 @@ Serverul pastreaza un fisier cu info despre toate fisierele downloadate: 1. Mesaje GET_CLIENTS -----> tipul mesajului GET_CLIENTS - <----- lista de liste [client_name, pid, torrent_file] - + <----- [ACK, lista de liste [client_name, pid, torrent_file]] + sau [ERROR, msg] 2. Mesaje GET_OUTPUT -----> tipul mesajului GET_OUTPUT - <----- lista : [client_name, metafile, [(file_type, file_path), ...]] + <----- [ACK, lista : [client_name, metafile, [(file_type, file_path), ...]]] + 3. Mesaje ARCHIVE -----> tipul mesajului ARCHIVE - <----- ACK/ERROR + <----- [ACK, lista fisiere arhivate] + sau [ERROR, msg] + - Serverul: - arhiveaza fis din fisierul local de sessions - sterge fis care au fost arhivate diff --git a/autorun/Util.py b/autorun/Util.py index 0cbb4ed..192123a 100644 --- a/autorun/Util.py +++ b/autorun/Util.py @@ -38,6 +38,7 @@ UP_LIMIT = "upload_limit" DL_LIMIT = "download_limit" DL_DIR = "download_dir" LOG_DIR = "logging_dir" +OUT_DIR = "output_dir" OUT_FILE = "output_file" LOG_FILE = "log_file" PORT = "port" @@ -48,3 +49,19 @@ TRIBLER = "tribler" TRANSMISSION = "transmission" HRKTORRENT = "hrktorrent" +import os.path +import time +def create_archive_file(file_path): + print "Archive file: ", file_path + if os.path.isfile(file_path): + # archive file + archive_name = file_path + time.strftime("%d%m%Y_%H%M%S", time.localtime()) + ".zip" + command = "zip " + archive_name + " " + file_path + print command + os.system(command) + # remove file + command = "rm " + file_path + os.system(command) + return 0 + return -1 + diff --git a/autorun/server/Client.py b/autorun/server/Client.py index c5c6614..f34f0fa 100644 --- a/autorun/server/Client.py +++ b/autorun/server/Client.py @@ -88,6 +88,7 @@ def test_all_commands(torrent_file): TORRENT:"/home/p2p/p2p-meta/" + torrent_name+".torrent", DL_DIR: "/home/p2p/p2p-dld/tribler", LOG_DIR: "/home/p2p/p2p-log/tribler", + OUT_DIR: "/home/p2p/p2p-log/tribler", OUT_FILE: torrent_name + ".out", #status messages LOG_FILE: "tribler-" + torrent_name + ".log", #verbose messages @@ -98,6 +99,7 @@ def test_all_commands(torrent_file): TORRENT: "/home/p2p/p2p-meta/" + torrent_name+".torrent", DL_DIR: "/home/p2p/p2p-dld/transmission", LOG_DIR: "/home/p2p/p2p-log/transmission", + OUT_DIR: "/home/p2p/p2p-log/transmission", OUT_FILE: torrent_name + ".out", #status messages LOG_FILE: "transmission-" + torrent_name + ".log", #verbose messages @@ -109,13 +111,14 @@ def test_all_commands(torrent_file): TORRENT: "/home/p2p/p2p-meta/" + torrent_name+".torrent", DL_DIR: "/home/p2p/p2p-dld/hrktorrent", LOG_DIR: "/home/p2p/p2p-log/hrktorrent", + OUT_DIR: "/home/p2p/p2p-log/hrktorrent", OUT_FILE: torrent_name + ".out", #status messages LOG_FILE: "hrktorrent-" + torrent_name + ".log", #verbose messages } - #print s.send_command(GET_OUTPUT, "") - response = s.send_command(START_MSG, start_data_tribler) + print s.send_command(ARCHIVE, "") + """response = s.send_command(START_MSG, start_data_tribler) print response pid1 = response[1] print pid1 @@ -140,7 +143,7 @@ def test_all_commands(torrent_file): s.connect(SERVER_HOST, SERVER_PORT) response = s.send_command(STOP_MSG, pid1) print response - + """ def test_send_recv(): # test 1 diff --git a/autorun/server/Server.py b/autorun/server/Server.py index a64c1cc..c46d79f 100644 --- a/autorun/server/Server.py +++ b/autorun/server/Server.py @@ -1,6 +1,7 @@ #!/usr/bin/env python import sys, os, socket +import os.path import time import pickle import signal @@ -92,6 +93,8 @@ class MyDaemon(Daemon): info_list = [info_dict[CLIENT],info_dict[TORRENT]] if LOG_DIR in info_dict: info_list.append((info_dict[LOG_DIR], LOG_DIR)) + if OUT_DIR in info_dict: + info_list.append((info_dict[OUT_DIR], OUT_DIR)) if OUT_FILE in info_dict: info_list.append((info_dict[OUT_FILE], OUT_FILE)) if LOG_FILE in info_dict: @@ -102,12 +105,37 @@ class MyDaemon(Daemon): file_list.append (info_list) return 0 # success + + def archive_files(self, info_dict, archives_list): + """ Archives status and verbose message log files. """ + log_path = "" + out_path = "" + err_msg = "" + if LOG_DIR in info_dict: + log_path = info_dict[LOG_DIR] + if LOG_FILE in info_dict: + file_path = log_path + "/" + info_dict[LOG_FILE] + if create_archive_file(file_path) < -1: + err_msg = "File does not exist: ", file_path + else: archives_list.append(file_path) + + if OUT_DIR in info_dict: + out_path = info_dict[OUT_DIR] + else: + out_path = log_path + + if OUT_FILE in info_dict: + file_path = out_path + "/" + info_dict[OUT_FILE] + if create_archive_file(file_path) < -1: + err_msg = "File does not exist: ", file_path + else: archives_list.append(file_path) + + print "Server Error: ", err_msg - def read_download_info(self, file_list): + def read_download_info(self, file_list, create_archive = False, archives_list = []): """ Reads all the contents of the file that stores info about logging files and folders. """ - import os.path if not os.path.exists(SESSIONS_FILE): print "Server Error: No sessions_file" return -1 @@ -118,11 +146,16 @@ class MyDaemon(Daemon): info_dict = {} try: while line != '': + print line if size == 0 : - if info_dict != {} and self.add_to_output_msg(file_list, info_dict) < 0: + if info_dict != {} : + if self.add_to_output_msg(file_list, info_dict) < 0: raise Exception - size = int(line) - info_dict = {} + if create_archive: + self.archive_files(info_dict,archives_list) + + size = int(line) + info_dict = {} else: parts = line.strip().split(':') if len(parts) != 2: @@ -131,18 +164,24 @@ class MyDaemon(Daemon): size = size - 1 line = f.readline() - if size == 0 and info_dict != {} and self.add_to_output_msg(file_list, info_dict) < 0: - raise Exception - - except Exception: + if size == 0 and info_dict != {}: + if self.add_to_output_msg(file_list, info_dict) < 0: + raise Exception + if create_archive: + self.archive_files(info_dict, archives_list) + + except Exception, e: print "Server Error: wrong file type for sessions file" - err_msg = "Error encountered while reading file" + print e f.close() return -1 f.close() return 0 + + + def start_bt_client(self, bt_client_data): """ Starts a process for a BitTorrent client and returns its pid. @return: -1, if any error is encountered @@ -159,7 +198,7 @@ class MyDaemon(Daemon): else: return -1 - btcr.config_run(bt_client_data[DL_DIR], bt_client_data[LOG_DIR], + btcr.config_run(bt_client_data[DL_DIR], bt_client_data[OUT_DIR], bt_client_data[OUT_FILE], bt_client_data[LOG_DIR], bt_client_data[LOG_FILE], bt_client_data[PORT], bt_client_data[TORRENT]) @@ -236,7 +275,14 @@ class MyDaemon(Daemon): response.append([self.processes_info[k][0], k, self.processes_info[k][1]]) # client_name, pid, torrent file except Exception: err_msg = "Error: could not retrive running clients list" - + + elif msg[0] == ARCHIVE: + response = [] + if self.read_download_info([], True, response) < 0: # read file path and create archives + err_msg = "Error: could not archive log files." + else: # erase sessions file + f = open(SESSIONS_FILE, "w") + f.close() else: err_msg = "Error: wrong message type"; -- 2.20.1