def sighandler(signum, frame): global sighandled if not sighandled: util.report_message("Received signal %s" % signum) # temporarily immunize from signals oldhandler = signal.signal(signum, signal.SIG_IGN) os.killpg(0, signum) signal.signal(signum, oldhandler) sighandled = True
client = config.get_client(cfg) # separate the wheat from the chaff # and when I say 'wheat' and 'chaff', I mean 'torrent files' and 'magnet links' is_magnet = lambda _: _.startswith("magnet:") is_torrent = lambda _: not is_magnet(_) # give all the torrents/magnets to the client failed = False for uploadable in args.torrents: try: if type(uploadable) is str: uploadable = uploadable.decode(sys.getfilesystemencoding()) if is_magnet(uploadable): client.upload_magnet_link(uploadable) util.report_message("%s submitted to seedbox" % uploadable) elif is_torrent(uploadable): client.upload_torrent(uploadable) util.report_message("%s submitted to seedbox" % os.path.basename(uploadable)) else: raise ValueError("%s is not a torrent or a magnet link" % uploadable) except Exception as e: if args.debug: raise extramessage = "" if isinstance(e, ConnectionError): if e.args[0].errno == -2: extramessage = "\nCheck the hostname in your seedboxtools configuration." util.report_error("error while uploading %s: %s%s" % (uploadable, e, extramessage)) failed = True
def download(client, remove_finished=False): for torrent, status, filename in client.get_files_to_download(): # Set loop vars up download_lockfile = ".%s.done" % filename fully_downloaded = os.path.exists(download_lockfile) seeding = status == "Seeding" # If the file is completely downloaded but not to be remotely removed, skip if fully_downloaded and not remove_finished: util.report_message("%s from %s is fully downloaded, continuing to next torrent" % (filename, torrent)) continue # If the remote files don't exist, skip util.report_message("Checking if %s from torrent %s exists on server" % (filename, torrent)) if not client.exists_on_server(filename): util.report_message("%s from %s is no longer available on server, continuing to next torrent" % (filename, torrent)) continue if not fully_downloaded: # Start download. util.report_message("Downloading %s from torrent %s" % (filename, torrent)) util.mark_dir_downloading_when_it_appears(filename) retvalue = client.transfer(filename) if retvalue != 0: # rsync failed util.mark_dir_error(filename) if retvalue == 20: util.report_error("Download of %s stopped -- rsync process interrupted" % (filename,)) util.report_message("Finishing by user request") return 2 elif retvalue < 0: util.report_error("Download of %s failed -- rsync process killed with signal %s" % (filename, -retvalue)) util.report_message("Aborting") return 1 else: util.report_error("Download of %s failed -- rsync process exited with return status %s" % (filename, retvalue)) util.report_message("Aborting") return 1 # Rsync successful # mark file as downloaded try: file(download_lockfile, "w").write("Done") except OSError, e: if e.errno != 17: raise # report successful download fully_downloaded = True util.mark_dir_complete(filename) util.report_message("Download of %s complete" % filename) else: if remove_finished: if seeding: util.report_message("%s from %s is complete but still seeding, not removing" % (filename, torrent)) else: client.remove_remote_download(filename) util.report_message("Removal of %s complete" % filename)
def do_guarded(): try: return download( client=client, remove_finished=opts.remove_finished ) except IOError, e: if e.errno == 4: pass else: traceback.print_exc() return 8 except TemporaryMalfunction, e: util.report_error(str(e)) except Exception, e: raise retvalue = 0 if opts.run_every is False: util.report_message("Starting download of finished torrents") retvalue = do_guarded() util.report_message("Download of finished torrents complete") else: util.report_message("Starting daemon for download of finished torrents") while not sighandled: retvalue = do_guarded() if not sighandled: util.report_message("Sleeping %s seconds" % opts.run_every) for _ in xrange(opts.run_every): if not sighandled: time.sleep(1) util.report_message("Download of finished torrents complete") if sighandled: return 0 return retvalue
[run_processor_program, filename], stdin=file(os.devnull) ) util.report_message( "Execution of %s %s exited with return value%s" % ( run_processor_program, filename, retval, ) ) except OSError, e: util.report_error("Program %r is not executable: %s" % ( run_processor_program, e )) if remove_finished: if seeding: util.report_message("%s from %s is complete but still seeding, not removing" % (filename, torrent)) else: client.remove_remote_download(filename) try: os.unlink(download_lockfile) except OSError, e: if e.errno != errno.ENOENT: raise util.report_message("Removal of %s complete" % filename) sighandled = False def sighandler(signum, frame): global sighandled if not sighandled: util.report_message("Received signal %s" % signum) # temporarily immunize from signals