From ec3ce93f8358d7ba51599013462e00ff2a447444 Mon Sep 17 00:00:00 2001 From: ggil Date: Tue, 1 Oct 2024 13:15:26 +0200 Subject: [PATCH] refs #631 - Modify 'runTorrentTracker.py' --- bin/runTorrentTracker.py | 63 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 4 deletions(-) diff --git a/bin/runTorrentTracker.py b/bin/runTorrentTracker.py index b898fd2..9a5d6c4 100644 --- a/bin/runTorrentTracker.py +++ b/bin/runTorrentTracker.py @@ -4,8 +4,23 @@ """ Este script inicia el tracker "bttrack" (o lo reinicia, si ya estuviera iniciado), finalizando previamente cualquier proceso activo, y borrando el archivo "/tmp/dstate". En principio, debería hacer lo mismo que el script bash original (cuyo nombre es "torrent-tracker"), que se ejecutaba por cron cada hora. +Creemos que debe ser llamado únicamente cuando se quiera hacer una descarga mediante P2P (junto al script "runTorrentSeeder.py"). +NOTA: El paquete no hace una búsqueda recursiva, por lo que se debe especificar el subdirectorio correspondiente a la OU, si es el caso. -No recibe ningún parámetro, y creemos que debe ser llamado únicamente cuando se quiera hacer una descarga mediante P2P (junto al script "runTorrentSeeder.py"). + Parámetros +------------ +sys.argv[1] - Subdirectorio correspondiente a la OU (o "none" si no es el caso). + - Ejemplo1: none + - Ejemplo2: ou_subdir + + Sintaxis +---------- +./runTorrentTracker.py none|ou_subdir + + Ejemplos + --------- +./runTorrentTracker.py none +./runTorrentTracker.py ou_subdir """ # -------------------------------------------------------------------------------------------- @@ -13,6 +28,7 @@ No recibe ningún parámetro, y creemos que debe ser llamado únicamente cuando # -------------------------------------------------------------------------------------------- import os +import sys import subprocess import time @@ -21,6 +37,7 @@ import time # VARIABLES # -------------------------------------------------------------------------------------------- +script_name = os.path.basename(__file__) repo_path = '/opt/opengnsys/images' bttrack_port = 6969 @@ -35,12 +52,41 @@ bttrack_allow_get = 0 # Este valor impide la descarga desde clientes no autoriza # -------------------------------------------------------------------------------------------- -def run_bttrack(): +def show_help(): + """ Imprime la ayuda, cuando se ejecuta el script con el parámetro "help". + """ + help_text = f""" + Sintaxis: {script_name} none|ou_subdir + Ejemplo1: {script_name} none + Ejemplo2: {script_name} ou_subdir + """ + print(help_text) + + + +def check_params(): + """ Comprueba que se haya enviado la cantidad correcta de parámetros, y en el formato correcto. + Si no es así, muestra un mensaje de error, y sale del script. + LLama a la función "show_help" cuando se ejecuta el script con el parámetro "help". + """ + # Si se ejecuta el script con el parámetro "help", se muestra la ayuda, y se sale del script: + if len(sys.argv) == 2 and sys.argv[1] == "help": + show_help() + sys.exit(0) + # Si se ejecuta el script con más o menos de 1 parámetro, se muestra un error y la ayuda, y se sale del script: + elif len(sys.argv) != 2: + print(f"{script_name} Error: Formato incorrecto: Se debe especificar 1 parámetro") + show_help() + sys.exit(1) + + + +def run_bttrack(torrent_path): """ Ejecuta el comando "bttrack", con sus parámetros correspondientes. Además, captura el resultado y los posibles errores, y los imprime. """ # Creamos una lista con el comando "bttrack" y sus parámetros, y lo imprimimos con espacios: - splitted_cmd = f"bttrack --port {bttrack_port} --dfile {bttrack_dfile} --save_dfile_interval {bttrack_interval} --reannounce_interval {bttrack_interval} --logfile {bttrack_log} --allowed_dir {repo_path} --allow_get {bttrack_allow_get}".split() + splitted_cmd = f"bttrack --port {bttrack_port} --dfile {bttrack_dfile} --save_dfile_interval {bttrack_interval} --reannounce_interval {bttrack_interval} --logfile {bttrack_log} --allowed_dir {torrent_path} --allow_get {bttrack_allow_get}".split() print(f"Sending command: {' '.join(splitted_cmd)}") # Ejecutamos el comando "bttrack" en el sistema, e imprimimos el resultado: @@ -63,6 +109,9 @@ def run_bttrack(): def main(): """ """ + # Evaluamos si se ha enviado la cantidad correcta de parámetros, y en el formato correcto: + check_params() + # Finalizamos el proceso "bttrack" (en caso de que estuviera corriendo): try: subprocess.run(f"pkill bttrack".split(), check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) @@ -76,8 +125,14 @@ def main(): # Esperamos 2 segundos: time.sleep(2) + # Construimos la ruta en la que buscar los torrents, en base al parámetro especificado: + if sys.argv[1] == 'none': + torrent_path = repo_path + else: + torrent_path = f"{repo_path}/{sys.argv[1]}" + # Ejecutamos el comando "bttrack" (para hacer tracking de los torrents): - run_bttrack() + run_bttrack(torrent_path)