refs #1101 add ogTorrentStart()

pull/1/head
Natalia Serrano 2024-11-13 18:29:22 +01:00
parent a19e01b078
commit 7c1cb0168c
2 changed files with 116 additions and 3 deletions

View File

@ -233,9 +233,9 @@ def ogGetPath (src=None, file=None):
#*/ ##
#ogGetParentPath ([ str_repo | int_ndisk int_npartition ] path_filepath
#ogGetParentPath ('/mnt/sda1/windows/system32') ==> '/mnt/sda1/WINDOWS'
#ogGetParentPath ('REPO', '/etc/fstab') ==> '/opt/opengnsys/images/etc'
#ogGetParentPath ('1 1', '/windows/system32') ==> '/mnt/sda1/WINDOWS'
#ogGetParentPath ( file='/mnt/sda1/windows/system32') ==> '/mnt/sda1/WINDOWS'
#ogGetParentPath (src='REPO', file='/etc/fstab') ==> '/opt/opengnsys/images/etc'
#ogGetParentPath (src='1 1', file='/windows/system32') ==> '/mnt/sda1/WINDOWS'
def ogGetParentPath (src=None, file=None):
if file is None:
SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_FORMAT, '')

View File

@ -658,6 +658,119 @@ def ogMcastRequest (img, proto):
#@return
#@note protocoloTORRENT=mode:time mode=seeder -> Dejar el equipo seedeando hasta que transcurra el tiempo indicado o un kill desde consola, mode=peer -> seedear mientras descarga mode=leecher -> NO seedear mientras descarga time tiempo que una vez descargada la imagen queremos dejar al cliente como seeder.
#*/ ##
#ogTorrentStart ( torrentfile='/opt/opengnsys/cache/linux.iso', torrentsess='peer:60')
#ogTorrentStart (container='CACHE', torrentfile='/PS1_PH1.img.torrent', torrentsess='seeder:10000')
#ogTorrentStart (disk=1, par=1, torrentfile='/linux.iso.torrent', torrentsess='leecher:60')
def ogTorrentStart (disk=None, par=None, container=None, torrentfile=None, torrentsess=None):
if torrentfile is None:
raise TypeError ('missing required argument: "torrentfile"')
if torrentsess is None:
raise TypeError ('missing required argument: "torrentsess"')
if container is not None:
if disk is None and par is None:
## we were given container=
if 'CACHE' != container.upper():
SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_FORMAT, 'La descarga torrent solo se hace desde local, copia el torrent a la cache y realiza la operación desde esa ubicación')
return None
source = FileLib.ogGetPath (src=container, file=torrentfile)
dev_err = f'{container} {torrentfile}'
print (f'ogGetPath (src=({container}), file=({torrentfile})) = source ({source})')
else:
raise TypeError ('argument "container" can be specified along neither "disk" nor "par"')
else:
if disk is not None and par is not None:
## we were given disk= par=
source = FileLib.ogGetPath (src=f'{disk} {par}', file=torrentfile)
dev_err = f'{disk} {par} {torrentfile}'
print (f'ogGetPath (src=({disk} {par}), file=({torrentfile})) = source ({source})')
elif disk is None and par is None:
## we were given nothing
if torrentfile.startswith ('/opt/opengnsys/images'):
SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_FORMAT, 'La descarga torrent solo se hace desde local, copia el torrent a la cache y realiza la operación desde esa ubicación')
return None
source = FileLib.ogGetPath (file=torrentfile)
dev_err = torrentfile
print (f'ogGetPath (file=({torrentfile})) = source ({source})')
else:
raise TypeError ('if one of "disk" and "par" are specified, then both must be')
if not source:
SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_NOTFOUND, f'device or torrentfile {dev_err} not found')
return
if subprocess.run (['ctorrent', '-x', source]).returncode:
SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_NOTFOUND, '')
return None
target = re.sub (r'\.torrent$', '', source)
dirsource = FileLib.ogGetParentPath (file=source)
ERROR = None
sess = torrentsess.split (':')
if 2 != len (sess):
SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_FORMAT, 'parametros session Torrent no completa: modo:tiempo')
return None
mode = sess[0].lower()
if mode not in ['seeder', 'peer', 'leecher']:
SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_FORMAT, f'valor modo Torrent no valido {sess[0]}')
return None
time = sess[1]
if not re.search (r'^[0-9]{1,10}$', time):
SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_FORMAT, f'valor tiempo no valido {sess[1]}')
return None
time = int (time)
OPTION = None
cwd = os.getcwd()
# si No fichero .bf, y Si fichero destino imagen ya descargada y su chequeo fue comprobado en su descarga inicial.
if not os.path.exists (f'{source}.bf') and os.path.exists (target):
print ('imagen ya descargada')
if 'seeder' != mode: return 'success' ## return any true value
print ('MODE seeder ctorrent')
os.chdir (dirsource)
subprocess.run (['timeout', '--signal', 'INT', time, 'ctorrent', '-f', source])
os.chdir (cwd)
return 'success'
#Si no existe bf ni fichero destino descarga inicial.
if not os.path.exists (f'{source}.bf') and not os.path.exists (target):
print ('descarga inicial')
OPTION = 'DOWNLOAD'
# Si fichero bf descarga anterior no completada -.
if os.path.exists (f'{source}.bf') and os.path.exists (target):
print ('Continuar con Descargar inicial no terminada.')
OPTION = 'DOWNLOAD'
if 'DOWNLOAD' != OPTION: return 'success'
os.chdir (dirsource)
if 'peer' == mode:
print ('Donwloading Torrent as peer')
# Creamos el fichero de resumen por defecto
with open (f'{source}.bf', 'w') as fd: pass
# ctorrent controla otro fichero -b ${SOURCE}.bfog
subprocess.run (['ctorrent', '-f', '-c', '-X', f'sleep {time}; kill -2 $(pidof ctorrent)', '-C', '100', source, '-s', target, '-b', f'{source}.bfog'])
elif 'leecher' == mode:
print ('Donwloading Torrent as leecher')
subprocess.run (['ctorrent', '${SOURCE}', '-X', 'sleep 30; kill -2 $(pidof ctorrent)', '-C', '100', '-U', '0'])
elif 'seeder' == mode:
print ('MODE seeder ctorrent')
# Creamos el fichero de resumen por defecto
with open (f'{source}.bf', 'w') as fd: pass
# ctorrent controla otro fichero -b ${SOURCE}.bfog
subprocess.run (['ctorrent', '-f', '-c', '-X', f'sleep {time}; kill -2 $(pidof ctorrent)', '-C', '100', source, '-s', target, '-b', f'{source}.bfog'])
else:
print ('this should not happen')
return None
os.chdir (cwd)
#/**
# ogCreateTorrent [ str_repo | int_ndisk int_npart ] Relative_path_file