<s> from zurllib import urlopen <EOL> from urlparse import urlparse <EOL> from BT1 . btformats import check_message <EOL> from BT1 . Choker import Choker <EOL> from BT1 . Storage import Storage <EOL> from BT1 . StorageWrapper import StorageWrapper <EOL> from BT1 . FileSelector import FileSelector <EOL> from BT1 . Uploader import Upload <EOL> from BT1 . Downloader import Downloader <EOL> from BT1 . HTTPDownloader import HTTPDownloader <EOL> from BT1 . Connecter import Connecter <EOL> from RateLimiter import RateLimiter <EOL> from BT1 . Encrypter import Encoder <EOL> from RawServer import RawServer , autodetect_ipv6 , autodetect_socket_style <EOL> from BT1 . Rerequester import Rerequester <EOL> from BT1 . DownloaderFeedback import DownloaderFeedback <EOL> from RateMeasure import RateMeasure <EOL> from CurrentRateMeasure import Measure <EOL> from BT1 . PiecePicker import PiecePicker <EOL> from BT1 . Statistics import Statistics <EOL> from ConfigDir import ConfigDir <EOL> from bencode import bencode , bdecode <EOL> from natpunch import UPnP_test <EOL> from hashlib import sha1 <EOL> from os import path , makedirs , listdir <EOL> from parseargs import parseargs , formatDefinitions , defaultargs <EOL> from socket import error as socketerror <EOL> from random import seed <EOL> from threading import Thread , Event <EOL> from clock import clock <EOL> from BTcrypto import CRYPTO_OK <EOL> from __init__ import createPeerID <EOL> try : <EOL> True <EOL> except : <EOL> True = <NUM_LIT:1> <EOL> False = <NUM_LIT:0> <EOL> defaults = [ <EOL> ( '<STR_LIT>' , <NUM_LIT:7> , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:2> ** <NUM_LIT> , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:10> , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:2> ** <NUM_LIT> , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> , '<STR_LIT>' + <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT:url>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , int ( CRYPTO_OK ) , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> , <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:10> , <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:2> ** <NUM_LIT> , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , autodetect_socket_style ( ) , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> , <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> , <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:5> * <NUM_LIT> , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:20> , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:4> , <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:2> , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:5> , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:4> , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:50> , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:30> , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , <EOL> "<STR_LIT>" ) , <EOL> ] <EOL> argslistheader = '<STR_LIT>' <EOL> def _failfunc ( x ) : <EOL> print x <EOL> def download ( params , filefunc , statusfunc , finfunc , errorfunc , doneflag , cols , <EOL> pathFunc = None , presets = { } , exchandler = None , <EOL> failed = _failfunc , paramfunc = None ) : <EOL> try : <EOL> config = parse_params ( params , presets ) <EOL> except ValueError , e : <EOL> failed ( '<STR_LIT>' + str ( e ) + '<STR_LIT>' ) <EOL> return <EOL> if not config : <EOL> errorfunc ( get_usage ( ) ) <EOL> return <EOL> myid = createPeerID ( ) <EOL> seed ( myid ) <EOL> rawserver = RawServer ( doneflag , config [ '<STR_LIT>' ] , <EOL> config [ '<STR_LIT>' ] , ipv6_enable = config [ '<STR_LIT>' ] , <EOL> failfunc = failed , errorfunc = exchandler ) <EOL> upnp_type = UPnP_test ( config [ '<STR_LIT>' ] ) <EOL> try : <EOL> listen_port = rawserver . find_and_bind ( config [ '<STR_LIT>' ] , config [ '<STR_LIT>' ] , <EOL> config [ '<STR_LIT>' ] , ipv6_socket_style = config [ '<STR_LIT>' ] , <EOL> upnp = upnp_type , randomizer = config [ '<STR_LIT>' ] ) <EOL> except socketerror , e : <EOL> failed ( "<STR_LIT>" + str ( e ) ) <EOL> return <EOL> response = get_response ( config [ '<STR_LIT>' ] , config [ '<STR_LIT:url>' ] , failed ) <EOL> if not response : <EOL> return <EOL> infohash = sha1 ( bencode ( response [ '<STR_LIT:info>' ] ) ) . digest ( ) <EOL> d = BT1Download ( statusfunc , finfunc , errorfunc , exchandler , doneflag , <EOL> config , response , infohash , myid , rawserver , listen_port ) <EOL> if not d . saveAs ( filefunc ) : <EOL> return <EOL> if pathFunc : <EOL> pathFunc ( d . getFilename ( ) ) <EOL> hashcheck = d . initFiles ( old_style = True ) <EOL> if not hashcheck : <EOL> return <EOL> if not hashcheck ( ) : <EOL> return <EOL> if not d . startEngine ( ) : <EOL> return <EOL> d . startRerequester ( ) <EOL> d . autoStats ( ) <EOL> statusfunc ( activity = '<STR_LIT>' ) <EOL> if paramfunc : <EOL> paramfunc ( { '<STR_LIT>' : d . setUploadRate , <EOL> '<STR_LIT>' : d . setConns , <EOL> '<STR_LIT>' : listen_port , <EOL> '<STR_LIT>' : myid , <EOL> '<STR_LIT>' : infohash , <EOL> '<STR_LIT>' : d . _startConnection , <EOL> } ) <EOL> rawserver . listen_forever ( d . getPortHandler ( ) ) <EOL> d . shutdown ( ) <EOL> def parse_params ( params , presets = { } ) : <EOL> if len ( params ) == <NUM_LIT:0> : <EOL> return None <EOL> config , args = parseargs ( params , defaults , <NUM_LIT:0> , <NUM_LIT:1> , presets = presets ) <EOL> if args : <EOL> if config [ '<STR_LIT>' ] or config [ '<STR_LIT:url>' ] : <EOL> raise ValueError , '<STR_LIT>' <EOL> if path . isfile ( args [ <NUM_LIT:0> ] ) : <EOL> config [ '<STR_LIT>' ] = args [ <NUM_LIT:0> ] <EOL> else : <EOL> try : <EOL> urlparse ( args [ <NUM_LIT:0> ] ) <EOL> except : <EOL> raise ValueError , '<STR_LIT>' <EOL> config [ '<STR_LIT:url>' ] = args [ <NUM_LIT:0> ] <EOL> elif ( config [ '<STR_LIT>' ] == '<STR_LIT>' ) == ( config [ '<STR_LIT:url>' ] == '<STR_LIT>' ) : <EOL> raise ValueError , '<STR_LIT>' <EOL> return config <EOL> def get_usage ( defaults = defaults , cols = <NUM_LIT:100> , presets = { } ) : <EOL> return ( argslistheader + formatDefinitions ( defaults , cols , presets ) ) <EOL> def get_response ( file , url , errorfunc ) : <EOL> try : <EOL> if file : <EOL> h = open ( file , '<STR_LIT:rb>' ) <EOL> try : <EOL> line = h . read ( <NUM_LIT:10> ) <EOL> front , garbage = line . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> assert front [ <NUM_LIT:0> ] == '<STR_LIT:d>' <EOL> int ( front [ <NUM_LIT:1> : ] ) <EOL> except : <EOL> errorfunc ( file + '<STR_LIT>' ) <EOL> return None <EOL> try : <EOL> h . seek ( <NUM_LIT:0> ) <EOL> except : <EOL> try : <EOL> h . close ( ) <EOL> except : <EOL> pass <EOL> h = open ( file , '<STR_LIT:rb>' ) <EOL> else : <EOL> try : <EOL> h = urlopen ( url ) <EOL> except : <EOL> errorfunc ( url + '<STR_LIT>' ) <EOL> return None <EOL> response = h . read ( ) <EOL> except IOError , e : <EOL> errorfunc ( '<STR_LIT>' + str ( e ) ) <EOL> return None <EOL> try : <EOL> h . close ( ) <EOL> except : <EOL> pass <EOL> try : <EOL> try : <EOL> response = bdecode ( response ) <EOL> except : <EOL> errorfunc ( "<STR_LIT>" ) <EOL> response = bdecode ( response , sloppy = <NUM_LIT:1> ) <EOL> check_message ( response ) <EOL> except ValueError , e : <EOL> errorfunc ( "<STR_LIT>" + str ( e ) ) <EOL> return None <EOL> return response <EOL> class BT1Download : <EOL> def __init__ ( self , statusfunc , finfunc , errorfunc , excfunc , doneflag , <EOL> config , response , infohash , id , rawserver , port , <EOL> appdataobj = None ) : <EOL> self . statusfunc = statusfunc <EOL> self . finfunc = finfunc <EOL> self . errorfunc = errorfunc <EOL> self . excfunc = excfunc <EOL> self . doneflag = doneflag <EOL> self . config = config <EOL> self . response = response <EOL> self . infohash = infohash <EOL> self . myid = id <EOL> self . rawserver = rawserver <EOL> self . port = port <EOL> self . info = self . response [ '<STR_LIT:info>' ] <EOL> self . pieces = [ self . info [ '<STR_LIT>' ] [ x : x + <NUM_LIT:20> ] <EOL> for x in xrange ( <NUM_LIT:0> , len ( self . info [ '<STR_LIT>' ] ) , <NUM_LIT:20> ) ] <EOL> self . len_pieces = len ( self . pieces ) <EOL> self . argslistheader = argslistheader <EOL> self . unpauseflag = Event ( ) <EOL> self . unpauseflag . set ( ) <EOL> self . downloader = None <EOL> self . storagewrapper = None <EOL> self . fileselector = None <EOL> self . super_seeding_active = False <EOL> self . filedatflag = Event ( ) <EOL> self . spewflag = Event ( ) <EOL> self . superseedflag = Event ( ) <EOL> self . whenpaused = None <EOL> self . finflag = Event ( ) <EOL> self . rerequest = None <EOL> self . tcp_ack_fudge = config [ '<STR_LIT>' ] <EOL> self . selector_enabled = config [ '<STR_LIT>' ] <EOL> if appdataobj : <EOL> self . appdataobj = appdataobj <EOL> elif self . selector_enabled : <EOL> self . appdataobj = ConfigDir ( ) <EOL> self . appdataobj . deleteOldCacheData ( config [ '<STR_LIT>' ] , <EOL> [ self . infohash ] ) <EOL> self . excflag = self . rawserver . get_exception_flag ( ) <EOL> self . failed = False <EOL> self . checking = False <EOL> self . started = False <EOL> self . picker = PiecePicker ( self . len_pieces , config [ '<STR_LIT>' ] , <EOL> config [ '<STR_LIT>' ] ) <EOL> self . choker = Choker ( config , rawserver . add_task , <EOL> self . picker , self . finflag . isSet ) <EOL> def checkSaveLocation ( self , loc ) : <EOL> if self . info . has_key ( '<STR_LIT>' ) : <EOL> return path . exists ( loc ) <EOL> for x in self . info [ '<STR_LIT>' ] : <EOL> if path . exists ( path . join ( loc , x [ '<STR_LIT:path>' ] [ <NUM_LIT:0> ] ) ) : <EOL> return True <EOL> return False <EOL> def saveAs ( self , filefunc , pathfunc = None ) : <EOL> try : <EOL> def make ( f , forcedir = False ) : <EOL> if not forcedir : <EOL> f = path . split ( f ) [ <NUM_LIT:0> ] <EOL> if f != '<STR_LIT>' and not path . exists ( f ) : <EOL> makedirs ( f ) <EOL> if self . info . has_key ( '<STR_LIT>' ) : <EOL> file_length = self . info [ '<STR_LIT>' ] <EOL> file = filefunc ( self . info [ '<STR_LIT:name>' ] , file_length , <EOL> self . config [ '<STR_LIT>' ] , False ) <EOL> if file is None : <EOL> return None <EOL> make ( file ) <EOL> files = [ ( file , file_length ) ] <EOL> else : <EOL> file_length = <NUM_LIT:0> L <EOL> for x in self . info [ '<STR_LIT>' ] : <EOL> file_length += x [ '<STR_LIT>' ] <EOL> file = filefunc ( self . info [ '<STR_LIT:name>' ] , file_length , <EOL> self . config [ '<STR_LIT>' ] , True ) <EOL> if file is None : <EOL> return None <EOL> existing = <NUM_LIT:0> <EOL> if path . exists ( file ) : <EOL> if not path . isdir ( file ) : <EOL> self . errorfunc ( file + '<STR_LIT>' ) <EOL> return None <EOL> if len ( listdir ( file ) ) > <NUM_LIT:0> : <EOL> for x in self . info [ '<STR_LIT>' ] : <EOL> if path . exists ( path . join ( file , x [ '<STR_LIT:path>' ] [ <NUM_LIT:0> ] ) ) : <EOL> existing = <NUM_LIT:1> <EOL> if not existing : <EOL> file = path . join ( file , self . info [ '<STR_LIT:name>' ] ) <EOL> if path . exists ( file ) and not path . isdir ( file ) : <EOL> if file [ - <NUM_LIT:8> : ] == '<STR_LIT>' : <EOL> file = file [ : - <NUM_LIT:8> ] <EOL> if path . exists ( file ) and not path . isdir ( file ) : <EOL> self . errorfunc ( "<STR_LIT>" + self . info [ '<STR_LIT:name>' ] ) <EOL> return None <EOL> make ( file , True ) <EOL> if pathfunc != None : <EOL> pathfunc ( file ) <EOL> files = [ ] <EOL> for x in self . info [ '<STR_LIT>' ] : <EOL> n = file <EOL> for i in x [ '<STR_LIT:path>' ] : <EOL> n = path . join ( n , i ) <EOL> files . append ( ( n , x [ '<STR_LIT>' ] ) ) <EOL> make ( n ) <EOL> except OSError , e : <EOL> self . errorfunc ( "<STR_LIT>" + str ( e ) ) <EOL> return None <EOL> self . filename = file <EOL> self . files = files <EOL> self . datalength = file_length <EOL> return file <EOL> def getFilename ( self ) : <EOL> return self . filename <EOL> def _finished ( self ) : <EOL> self . finflag . set ( ) <EOL> try : <EOL> self . storage . set_readonly ( ) <EOL> except ( IOError , OSError ) , e : <EOL> self . errorfunc ( '<STR_LIT>' + str ( e ) ) <EOL> if self . superseedflag . isSet ( ) : <EOL> self . _set_super_seed ( ) <EOL> self . choker . set_round_robin_period ( <EOL> max ( self . config [ '<STR_LIT>' ] , <EOL> self . config [ '<STR_LIT>' ] * <EOL> self . info [ '<STR_LIT>' ] / <NUM_LIT> ) ) <EOL> self . rerequest_complete ( ) <EOL> self . finfunc ( ) <EOL> def _data_flunked ( self , amount , index ) : <EOL> self . ratemeasure_datarejected ( amount ) <EOL> if not self . doneflag . isSet ( ) : <EOL> self . errorfunc ( '<STR_LIT>' % index ) <EOL> def _failed ( self , reason ) : <EOL> self . failed = True <EOL> self . doneflag . set ( ) <EOL> if reason is not None : <EOL> self . errorfunc ( reason ) <EOL> def initFiles ( self , old_style = False , statusfunc = None ) : <EOL> if self . doneflag . isSet ( ) : <EOL> return None <EOL> if not statusfunc : <EOL> statusfunc = self . statusfunc <EOL> disabled_files = None <EOL> if self . selector_enabled : <EOL> self . priority = self . config [ '<STR_LIT>' ] <EOL> if self . priority : <EOL> try : <EOL> self . priority = self . priority . split ( '<STR_LIT:U+002C>' ) <EOL> assert len ( self . priority ) == len ( self . files ) <EOL> self . priority = [ int ( p ) for p in self . priority ] <EOL> for p in self . priority : <EOL> assert p >= - <NUM_LIT:1> <EOL> assert p <= <NUM_LIT:2> <EOL> except : <EOL> self . errorfunc ( '<STR_LIT>' ) <EOL> self . priority = None <EOL> data = self . appdataobj . getTorrentData ( self . infohash ) <EOL> try : <EOL> d = data [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> assert len ( d ) == len ( self . files ) <EOL> disabled_files = [ x == - <NUM_LIT:1> for x in d ] <EOL> except : <EOL> try : <EOL> disabled_files = [ x == - <NUM_LIT:1> for x in self . priority ] <EOL> except : <EOL> pass <EOL> try : <EOL> try : <EOL> self . storage = Storage ( self . files , self . info [ '<STR_LIT>' ] , <EOL> self . doneflag , self . config , disabled_files ) <EOL> except IOError , e : <EOL> self . errorfunc ( '<STR_LIT>' + str ( e ) ) <EOL> return None <EOL> if self . doneflag . isSet ( ) : <EOL> return None <EOL> self . storagewrapper = StorageWrapper ( self . storage , self . config [ '<STR_LIT>' ] , <EOL> self . pieces , self . info [ '<STR_LIT>' ] , self . _finished , self . _failed , <EOL> statusfunc , self . doneflag , self . config [ '<STR_LIT>' ] , <EOL> self . _data_flunked , self . rawserver . add_task , <EOL> self . config , self . unpauseflag ) <EOL> except ValueError , e : <EOL> self . _failed ( '<STR_LIT>' + str ( e ) ) <EOL> except IOError , e : <EOL> self . _failed ( '<STR_LIT>' + str ( e ) ) <EOL> if self . doneflag . isSet ( ) : <EOL> return None <EOL> if self . selector_enabled : <EOL> self . fileselector = FileSelector ( self . files , self . info [ '<STR_LIT>' ] , <EOL> self . appdataobj . getPieceDir ( self . infohash ) , <EOL> self . storage , self . storagewrapper , <EOL> self . rawserver . add_task , <EOL> self . _failed ) <EOL> if data : <EOL> data = data . get ( '<STR_LIT>' ) <EOL> if data : <EOL> self . fileselector . unpickle ( data ) <EOL> self . checking = True <EOL> if old_style : <EOL> return self . storagewrapper . old_style_init ( ) <EOL> return self . storagewrapper . initialize <EOL> def getCachedTorrentData ( self ) : <EOL> return self . appdataobj . getTorrentData ( self . infohash ) <EOL> def _make_upload ( self , connection , ratelimiter , totalup ) : <EOL> return Upload ( connection , ratelimiter , totalup , <EOL> self . choker , self . storagewrapper , self . picker , <EOL> self . config ) <EOL> def _kick_peer ( self , connection ) : <EOL> def k ( connection = connection ) : <EOL> connection . close ( ) <EOL> self . rawserver . add_task ( k , <NUM_LIT:0> ) <EOL> def _ban_peer ( self , ip ) : <EOL> self . encoder_ban ( ip ) <EOL> def _received_raw_data ( self , x ) : <EOL> if self . tcp_ack_fudge : <EOL> x = int ( x * self . tcp_ack_fudge ) <EOL> self . ratelimiter . adjust_sent ( x ) <EOL> def _received_data ( self , x ) : <EOL> self . downmeasure . update_rate ( x ) <EOL> self . ratemeasure . data_came_in ( x ) <EOL> def _received_http_data ( self , x ) : <EOL> self . downmeasure . update_rate ( x ) <EOL> self . ratemeasure . data_came_in ( x ) <EOL> self . downloader . external_data_received ( x ) <EOL> def _cancelfunc ( self , pieces ) : <EOL> self . downloader . cancel_piece_download ( pieces ) <EOL> self . httpdownloader . cancel_piece_download ( pieces ) <EOL> def _reqmorefunc ( self , pieces ) : <EOL> self . downloader . requeue_piece_download ( pieces ) <EOL> def startEngine ( self , ratelimiter = None , statusfunc = None ) : <EOL> if self . doneflag . isSet ( ) : <EOL> return False <EOL> if not statusfunc : <EOL> statusfunc = self . statusfunc <EOL> self . checking = False <EOL> if not CRYPTO_OK : <EOL> if self . config [ '<STR_LIT>' ] : <EOL> self . errorfunc ( '<STR_LIT>' ) <EOL> self . config [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> self . config [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> self . config [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> for i in xrange ( self . len_pieces ) : <EOL> if self . storagewrapper . do_I_have ( i ) : <EOL> self . picker . complete ( i ) <EOL> self . upmeasure = Measure ( self . config [ '<STR_LIT>' ] , <EOL> self . config [ '<STR_LIT>' ] ) <EOL> self . downmeasure = Measure ( self . config [ '<STR_LIT>' ] ) <EOL> if ratelimiter : <EOL> self . ratelimiter = ratelimiter <EOL> else : <EOL> self . ratelimiter = RateLimiter ( self . rawserver . add_task , <EOL> self . config [ '<STR_LIT>' ] , <EOL> self . setConns ) <EOL> self . ratelimiter . set_upload_rate ( self . config [ '<STR_LIT>' ] ) <EOL> self . ratemeasure = RateMeasure ( ) <EOL> self . ratemeasure_datarejected = self . ratemeasure . data_rejected <EOL> self . downloader = Downloader ( self . storagewrapper , self . picker , <EOL> self . config [ '<STR_LIT>' ] , self . config [ '<STR_LIT>' ] , <EOL> self . len_pieces , self . config [ '<STR_LIT>' ] , <EOL> self . _received_data , self . config [ '<STR_LIT>' ] , self . config [ '<STR_LIT>' ] , <EOL> self . _kick_peer , self . _ban_peer ) <EOL> self . downloader . set_download_rate ( self . config [ '<STR_LIT>' ] ) <EOL> self . connecter = Connecter ( self . _make_upload , self . downloader , self . choker , <EOL> self . len_pieces , self . upmeasure , self . config , <EOL> self . ratelimiter , self . rawserver . add_task ) <EOL> self . encoder = Encoder ( self . connecter , self . rawserver , <EOL> self . myid , self . config [ '<STR_LIT>' ] , self . rawserver . add_task , <EOL> self . config [ '<STR_LIT>' ] , self . infohash , <EOL> self . _received_raw_data , self . config ) <EOL> self . encoder_ban = self . encoder . ban <EOL> self . httpdownloader = HTTPDownloader ( self . storagewrapper , self . picker , <EOL> self . rawserver , self . finflag , self . errorfunc , self . downloader , <EOL> self . config [ '<STR_LIT>' ] , self . infohash , self . _received_http_data , <EOL> self . connecter . got_piece ) <EOL> if self . response . has_key ( '<STR_LIT>' ) and not self . finflag . isSet ( ) : <EOL> for u in self . response [ '<STR_LIT>' ] : <EOL> self . httpdownloader . make_download ( u ) <EOL> if self . selector_enabled : <EOL> self . fileselector . tie_in ( self . picker , self . _cancelfunc , <EOL> self . _reqmorefunc , self . rerequest_ondownloadmore ) <EOL> if self . priority : <EOL> self . fileselector . set_priorities_now ( self . priority ) <EOL> self . appdataobj . deleteTorrentData ( self . infohash ) <EOL> if self . config [ '<STR_LIT>' ] : <EOL> self . set_super_seed ( ) <EOL> self . started = True <EOL> return True <EOL> def rerequest_complete ( self ) : <EOL> if self . rerequest : <EOL> self . rerequest . announce ( <NUM_LIT:1> ) <EOL> def rerequest_stopped ( self ) : <EOL> if self . rerequest : <EOL> self . rerequest . announce ( <NUM_LIT:2> ) <EOL> def rerequest_lastfailed ( self ) : <EOL> if self . rerequest : <EOL> return self . rerequest . last_failed <EOL> return False <EOL> def rerequest_ondownloadmore ( self ) : <EOL> if self . rerequest : <EOL> self . rerequest . hit ( ) <EOL> def startRerequester ( self , seededfunc = None , force_rapid_update = False ) : <EOL> if self . response . has_key ( '<STR_LIT>' ) : <EOL> trackerlist = self . response [ '<STR_LIT>' ] <EOL> else : <EOL> trackerlist = [ [ self . response [ '<STR_LIT>' ] ] ] <EOL> self . rerequest = Rerequester ( self . port , self . myid , self . infohash , <EOL> trackerlist , self . config , <EOL> self . rawserver . add_task , self . rawserver . add_task , <EOL> self . errorfunc , self . excfunc , <EOL> self . encoder . start_connections , <EOL> self . connecter . how_many_connections , <EOL> self . storagewrapper . get_amount_left , <EOL> self . upmeasure . get_total , self . downmeasure . get_total , <EOL> self . upmeasure . get_rate , self . downmeasure . get_rate , <EOL> self . doneflag , self . unpauseflag , seededfunc , force_rapid_update ) <EOL> self . rerequest . start ( ) <EOL> def _init_stats ( self ) : <EOL> self . statistics = Statistics ( self . upmeasure , self . downmeasure , <EOL> self . connecter , self . httpdownloader , self . ratelimiter , <EOL> self . rerequest_lastfailed , self . filedatflag ) <EOL> if self . info . has_key ( '<STR_LIT>' ) : <EOL> self . statistics . set_dirstats ( self . files , self . info [ '<STR_LIT>' ] ) <EOL> if self . config [ '<STR_LIT>' ] : <EOL> self . spewflag . set ( ) <EOL> def autoStats ( self , displayfunc = None ) : <EOL> if not displayfunc : <EOL> displayfunc = self . statusfunc <EOL> self . _init_stats ( ) <EOL> DownloaderFeedback ( self . choker , self . httpdownloader , self . rawserver . add_task , <EOL> self . upmeasure . get_rate , self . downmeasure . get_rate , <EOL> self . ratemeasure , self . storagewrapper . get_stats , <EOL> self . datalength , self . finflag , self . spewflag , self . statistics , <EOL> displayfunc , self . config [ '<STR_LIT>' ] ) <EOL> def startStats ( self ) : <EOL> self . _init_stats ( ) <EOL> d = DownloaderFeedback ( self . choker , self . httpdownloader , self . rawserver . add_task , <EOL> self . upmeasure . get_rate , self . downmeasure . get_rate , <EOL> self . ratemeasure , self . storagewrapper . get_stats , <EOL> self . datalength , self . finflag , self . spewflag , self . statistics ) <EOL> return d . gather <EOL> def getPortHandler ( self ) : <EOL> return self . encoder <EOL> def shutdown ( self , torrentdata = { } ) : <EOL> if self . checking or self . started : <EOL> self . storagewrapper . sync ( ) <EOL> self . storage . close ( ) <EOL> self . rerequest_stopped ( ) <EOL> if self . fileselector and self . started : <EOL> if not self . failed : <EOL> self . fileselector . finish ( ) <EOL> torrentdata [ '<STR_LIT>' ] = self . fileselector . pickle ( ) <EOL> try : <EOL> self . appdataobj . writeTorrentData ( self . infohash , torrentdata ) <EOL> except : <EOL> self . appdataobj . deleteTorrentData ( self . infohash ) <EOL> return not self . failed and not self . excflag . isSet ( ) <EOL> def setUploadRate ( self , rate ) : <EOL> try : <EOL> def s ( self = self , rate = rate ) : <EOL> self . config [ '<STR_LIT>' ] = rate <EOL> self . ratelimiter . set_upload_rate ( rate ) <EOL> self . rawserver . add_task ( s ) <EOL> except AttributeError : <EOL> pass <EOL> def setConns ( self , conns , conns2 = None ) : <EOL> if not conns2 : <EOL> conns2 = conns <EOL> try : <EOL> def s ( self = self , conns = conns , conns2 = conns2 ) : <EOL> self . config [ '<STR_LIT>' ] = conns <EOL> self . config [ '<STR_LIT>' ] = conns2 <EOL> if ( conns > <NUM_LIT:30> ) : <EOL> self . config [ '<STR_LIT>' ] = conns + <NUM_LIT:10> <EOL> self . rawserver . add_task ( s ) <EOL> except AttributeError : <EOL> pass <EOL> def setDownloadRate ( self , rate ) : <EOL> try : <EOL> def s ( self = self , rate = rate ) : <EOL> self . config [ '<STR_LIT>' ] = rate <EOL> self . downloader . set_download_rate ( rate ) <EOL> self . rawserver . add_task ( s ) <EOL> except AttributeError : <EOL> pass <EOL> def startConnection ( self , ip , port , id ) : <EOL> self . encoder . _start_connection ( ( ip , port ) , id ) <EOL> def _startConnection ( self , ipandport , id ) : <EOL> self . encoder . _start_connection ( ipandport , id ) <EOL> def setInitiate ( self , initiate ) : <EOL> try : <EOL> def s ( self = self , initiate = initiate ) : <EOL> self . config [ '<STR_LIT>' ] = initiate <EOL> self . rawserver . add_task ( s ) <EOL> except AttributeError : <EOL> pass <EOL> def getConfig ( self ) : <EOL> return self . config <EOL> def getDefaults ( self ) : <EOL> return defaultargs ( defaults ) <EOL> def getUsageText ( self ) : <EOL> return self . argslistheader <EOL> def reannounce ( self , special = None ) : <EOL> try : <EOL> def r ( self = self , special = special ) : <EOL> if special is None : <EOL> self . rerequest . announce ( ) <EOL> else : <EOL> self . rerequest . announce ( specialurl = special ) <EOL> self . rawserver . add_task ( r ) <EOL> except AttributeError : <EOL> pass <EOL> def getResponse ( self ) : <EOL> try : <EOL> return self . response <EOL> except : <EOL> return None <EOL> def Pause ( self ) : <EOL> if not self . storagewrapper : <EOL> return False <EOL> self . unpauseflag . clear ( ) <EOL> self . rawserver . add_task ( self . onPause ) <EOL> return True <EOL> def onPause ( self ) : <EOL> self . whenpaused = clock ( ) <EOL> if not self . downloader : <EOL> return <EOL> self . downloader . pause ( True ) <EOL> self . encoder . pause ( True ) <EOL> self . choker . pause ( True ) <EOL> def Unpause ( self ) : <EOL> self . unpauseflag . set ( ) <EOL> self . rawserver . add_task ( self . onUnpause ) <EOL> def onUnpause ( self ) : <EOL> if not self . downloader : <EOL> return <EOL> self . downloader . pause ( False ) <EOL> self . encoder . pause ( False ) <EOL> self . choker . pause ( False ) <EOL> if self . rerequest and self . whenpaused and clock ( ) - self . whenpaused > <NUM_LIT> : <EOL> self . rerequest . announce ( <NUM_LIT:3> ) <EOL> def set_super_seed ( self ) : <EOL> try : <EOL> self . superseedflag . set ( ) <EOL> def s ( self = self ) : <EOL> if self . finflag . isSet ( ) : <EOL> self . _set_super_seed ( ) <EOL> self . rawserver . add_task ( s ) <EOL> except AttributeError : <EOL> pass <EOL> def _set_super_seed ( self ) : <EOL> if not self . super_seeding_active : <EOL> self . super_seeding_active = True <EOL> self . errorfunc ( '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> def s ( self = self ) : <EOL> self . downloader . set_super_seed ( ) <EOL> self . choker . set_super_seed ( ) <EOL> self . rawserver . add_task ( s ) <EOL> if self . finflag . isSet ( ) : <EOL> def r ( self = self ) : <EOL> self . rerequest . announce ( <NUM_LIT:3> ) <EOL> self . rawserver . add_task ( r ) <EOL> def am_I_finished ( self ) : <EOL> return self . finflag . isSet ( ) <EOL> def get_transfer_stats ( self ) : <EOL> return self . upmeasure . get_total ( ) , self . downmeasure . get_total ( ) </s>
