def transformEnclosure(self, url, enclosure, data): # TODO: Remove this call from here: from BitTorrent.ConvertedMetainfo import set_filesystem_encoding set_filesystem_encoding('', None) try: metainfo = ConvertedMetainfo(bdecode(data)) except BTFailure, e: message = 'Error reading .torrent file %s: %s' % (url, e) self.failTorrent(url, enclosure, message) return
def __init__(self, config, doneflag, errorfunc, listen_fail_ok=False): self.config = dict(config) self.errorfunc = errorfunc self.rawserver = RawServer(doneflag, config, errorfunc=errorfunc, tos=config['peer_socket_tos']) self.singleport_listener = SingleportListener(self.rawserver) self._find_port(listen_fail_ok) self.filepool = FilePool(config['max_files_open']) self.ratelimiter = RateLimiter(self.rawserver.add_task) self.ratelimiter.set_parameters(config['max_upload_rate'], config['upload_unit_size']) set_filesystem_encoding(config['filesystem_encoding'], errorfunc)
def __init__(self, config, doneflag, errorfunc, listen_fail_ok=False): self.dht = None self.config = config self.errorfunc = errorfunc self.rawserver = RawServer(doneflag, config, errorfunc=errorfunc, tos=config['peer_socket_tos']) set_zurllib_rawserver(self.rawserver) add_unsafe_thread() self.nattraverser = NatTraverser(self.rawserver, logfunc=errorfunc) self.singleport_listener = SingleportListener(self.rawserver, self.nattraverser) self.ratelimiter = RateLimiter(self.rawserver.add_task) self.ratelimiter.set_parameters(config['max_upload_rate'], config['upload_unit_size']) self._find_port(listen_fail_ok) self.filepool = FilePool(config['max_files_open']) set_filesystem_encoding(config['filesystem_encoding'], errorfunc)
os.popen("/usr/bin/btmaketorrent.py --target "+filename+" http://"+host+":"+str(port)+"/announce "+destfile).close() def retrieve_files(host, filelist, destdir='.', indicator=None): """ <Purpose> This retrieves files from a host to a destdir. <Arguments> host: 'host' holds two things, a server name and target directory. For example, if you want to retrieve files from '/tmp/' directory in 'quadrus.cs.arizona.edu' server, the 'host' will be 'quadrus.cs.arizona.edu/tmp'. filelist: 'filelist' is a list of files which need to be retrieved. junk_hashlist: 'junk_hashlist' is a list of the hashes for this list of files. It should be a list of strings. destdir: 'destdir' is a destination directory where retrieved files will be placed. A user should have 'destdir' exist before retrieving files. 'destdir' should be a string. Default is a current dir. indicator: 'indicator' is a module which has set_filename and download_indicator functions. 'indicator' will be passed in 'urlretrieve' function so that progress bar will be shown while downloading files. Default is 'None'. <Exceptions> All exceptions should be caught. <Side Effects> If the storkbtshare daemon is set to be running, this method will create hard links, torrent files, and ensure that the daemon is running. <Returns> (True, grabbed_list) 'grabbed_list' is a list of files which are retrieved """ global defaultset # set grabbed_list as a empty list. Later it will be appended with retrieved files grabbed_list = [] #arizonareport.send_out(3, "[Bittorrent Debug]: retrieve_files: "+str(filelist)+" "+str(junk_hashlist) ) # hack to make bt work try: def errorfunc(): pass from BitTorrent.ConvertedMetainfo import set_filesystem_encoding set_filesystem_encoding("ascii", errorfunc) except: pass # check if host is a string if not isinstance(host, str): arizonareport.send_syslog(arizonareport.ERR, "retrieve_files(): host should be a string") # return false and empty list return (False, grabbed_list) # check if filelist contains only strings # Later should just use something like justin.valid_sl TODO!!! # TODO - check for valid tuple list #if not valid_sl(filelist): # arizonareport.send_syslog(arizonareport.ERR, "retrieve_files(): filelist should be a list of strings") # # return false and empty list # return (False, grabbed_list) # check if destdir is a string if not isinstance(destdir,str): arizonareport.send_syslog(arizonareport.ERR, "retrieve_files(): destdir should be a string") # return false and empty list return (False, grabbed_list) # check that the destination directory exists if not os.path.isdir(destdir): arizonareport.send_syslog(arizonareport.ERR, "\nretrieve_files(): The destination directory '" + destdir + "' for a requested does not exist") # return false and empty list return (False, grabbed_list) # if destdir is a empty string, then make it as a current directory if destdir == '': destdir = '.' #verify the connection and that the directory exists # this has issues with this transfer stub due to the way bittorrent may # interoperate with certain standard modules (and possibly their naming # conventions). #if not __verify_connection(host): # # return false and empty list # return (False, grabbed_list) #make sure the sharing program is working: set=defaultset[:]#[60,"/usr/local/stork/torrents/files","/usr/local/stork/torrents/updates",0,True,"quadrus.cs.arizona.edu",6880,30] set[0]=get_option("arizonabittorrentcheckinterval",set[0]) set[1]=get_option("arizonabittorrentfiledir",set[1]) set[2]=get_option("arizonabittorrentupdatedir",set[2]) glo_updatedir=set[2] set[3]=get_option("arizonabittorrentuploadrate",set[3]) set[4]=get_option("arizonabittorrentsharing",str(set[4])).lower().strip()=="true" set[5]=get_option("arizonabittorrenttrackerhost",set[5]) set[6]=get_option("arizonabittorrenttrackerport",set[6]) set[7]=get_option("arizonabittorrentseedlookuptimeout",set[7]) #the share daemon should take care of itself now. #ensure_settings(set[0],set[1],set[2],set[3],set[4]) #make sure the torrent and update directories exist if not os.path.isdir(set[1]): os.makedirs(set[1]) if not os.path.isdir(set[2]): os.makedirs(set[2]) # go through every file in the file list for file in filelist: filename = file['filename'] hash = file.get('hash', None) if hash: filename = "/" + hash + "/" + filename starttime = time() # build url which specifies host and filename to be retrieved thisurl = __build_url(host,filename) # find the name of the torrent file and its directory ufilename=thisurl[thisurl.rfind('/')+1:] #make sure it's the right length: if len(ufilename)>255:ufilename=ufilename[-255:] udir=thisurl[:thisurl.rfind('/')] #find the real file name to download to rfilename=filename[filename.rfind('/')+1:] #to allow for multiple users to have differing files of the same name and still # find and use the corrent .torrent file, the torrent file names are long enough to # hopefully include enough of the file's subdirectory(ies) to be distinguishable. tfilename=ufilename+".torrent" #since python cannot handle files with names longer than 255 characters, cut off the front characters # (this will keep the hash) if len(tfilename)>255:tfilename=tfilename[-255:] #arizonareport.send_out(3, "[Bittorrent Debug]: about to try to download: "+udir+"/"+tfilename ) # download the file btargs=["storkbtdownloadheadless.py","--display_interval","4.0", "--max_upload_rate", str(set[3]),"--save_as",destdir+"/"+rfilename,"--url",udir+'/'+tfilename] # if the file already exists, it must be unlinked to avoid possible errors with overwriting hard-linked files #if os.path.exists(destdir+"/"+rfilename): # os.unlink(destdir+"/"+rfilename) prog_indic=None #try downloading status=-1 # if an idicator is passed in if (indicator): # make indicator_file store a file name which will be used in download_indicator function indicator_file = rfilename try: # set the filename so that indicator module can use the name to show for progress bar indicator.set_filename(indicator_file) # indicator doesn't have method set_filename or download_indicator except AttributeError: arizonareport.send_syslog(arizonareport.ERR, 'retrieve_files(): indicator module passed in is incorrect') return (False, grabbed_list) # if indicator_file which used for set_filename is not a string except TypeError: arizonareport.send_syslog(arizonareport.ERR, 'retrieve_files(): indicator_file is incorrect') return (False, grabbed_list) prog_indic=indicator.download_indicator arizonareport.send_out(3, "") try: #operating in quiet mode - only output should be via the progess indicator. #try this when assured the that host argument is correct: #status=btdownloadheadless(btargs,host,6969,1,prog_indic) status=btdownloadheadless(btargs,set[5],set[6],1,prog_indic,0,None,set[7]) except IOError,e: arizonareport.send_syslog(arizonareport.ERR, "retrieve_files(): azbt - error reading torrent file: " + str(e)) return (False, grabbed_list) except BTFailure,e:
from BitTorrent import version from BitTorrent import configfile from BitTorrent.defaultargs import get_defaults from BitTorrent.makemetafile import make_meta_files from BitTorrent.parseargs import makeHelp from BitTorrent.platform import btspawn from BitTorrent.ConvertedMetainfo import set_filesystem_encoding defaults = get_defaults('maketorrent') defconfig = dict([(name, value) for (name, value, doc) in defaults]) del name, value, doc ui_options = ('torrent_dir','piece_size_pow2','tracker_list','use_tracker') def sfe_ef(e,s): print s set_filesystem_encoding(defconfig['filesystem_encoding'], sfe_ef) EXTENSION = '.torrent' MAXIMUM_NODES = 8 class MainWindow(Window): def __init__(self, config): Window.__init__(self) self.mainwindow = self # temp hack to make modal win32 file choosers work self.tooltips = gtk.Tooltips() self.connect('destroy', self.quit) self.set_title(_("%s torrent file creator %s")%(app_name, version)) self.set_border_width(SPACING)