Exemplo n.º 1
0
    def setup(self, dcfg = None, pstate = None, initialdlstatus = None, lm_network_engine_wrapper_created_callback = None, lm_network_vod_event_callback = None, wrapperDelay = 0):
        """
        Create a Download object. Used internally by Session.
        @param dcfg DownloadStartupConfig or None (in which case 
        a new DownloadConfig() is created and the result 
        becomes the runtime config of this Download.
        """
        # Called by any thread, assume sessionlock is held
        try:
            with self.dllock:
                # Copy dlconfig, from default if not specified
                if dcfg is None:
                    cdcfg = DownloadStartupConfig()
                else:
                    cdcfg = dcfg
                self.dlconfig = copy.copy(cdcfg.dlconfig)
                
                metainfo = self.tdef.get_metainfo()
                self.set_filepieceranges(metainfo)
    
                # Things that only exist at runtime
                self.dlruntimeconfig= {}
                self.dlruntimeconfig['max_desired_upload_rate'] = 0
                self.dlruntimeconfig['max_desired_download_rate'] = 0
                
                # H4xor this so the 'name' field is safe
                self.correctedinfoname = fix_filebasename(self.tdef.get_name_as_unicode())

                # Allow correctinfoname to be overwritten for multifile torrents only
                if 'files' in metainfo['info'] and dcfg.get_corrected_filename() and dcfg.get_corrected_filename() != '':
                    self.correctedinfoname = dcfg.get_corrected_filename()
        
                self.files = []
                if 'files' in metainfo['info']:
                    for x in metainfo['info']['files']:
                        savepath = torrentfilerec2savefilename(x)
                        full = savefilenames2finaldest(self.get_content_dest(), savepath)
                        # Arno: TODO: this sometimes gives too long filenames for 
                        # Windows. When fixing this take into account that 
                        # Download.get_dest_files() should still produce the same
                        # filenames as your modifications here.
                        self.files.append((full, x['length']))
                else:
                    self.files.append((self.get_content_dest(), metainfo['info']['length']))
        
                if DEBUG:
                    print >> sys.stderr, "LibtorrentDownloadImpl: setup: initialdlstatus", self.tdef.get_infohash(), initialdlstatus
                    
                if initialdlstatus == DLSTATUS_STOPPED:
                    self.pause_after_next_hashcheck = True
                    
                self.create_engine_wrapper(lm_network_engine_wrapper_created_callback, pstate, lm_network_vod_event_callback, initialdlstatus = initialdlstatus, wrapperDelay = wrapperDelay)
                
            self.pstate_for_restart = pstate

        except Exception, e:
            with self.dllock:
                self.error = e
                print_exc()
    def set_files(self):
        metainfo = self.tdef.get_metainfo()
        self.set_filepieceranges(metainfo)

        # Allow correctinfoname to be overwritten for multifile torrents only
        if 'files' in metainfo['info'] and self.dlconfig['correctedfilename'] and self.dlconfig['correctedfilename'] != '':
            self.correctedinfoname = self.dlconfig['correctedfilename']

        self.files = []
        if 'files' in metainfo['info']:
            for x in metainfo['info']['files']:
                savepath = torrentfilerec2savefilename(x)
                full = savefilenames2finaldest(self.get_content_dest(), savepath)
                # Arno: TODO: this sometimes gives too long filenames for 
                # Windows. When fixing this take into account that 
                # Download.get_dest_files() should still produce the same
                # filenames as your modifications here.
                self.files.append((full, x['length']))
        else:
            self.files.append((self.get_content_dest(), metainfo['info']['length']))
    def set_files(self):
        metainfo = self.tdef.get_metainfo()
        self.set_filepieceranges(metainfo)

        # Allow correctinfoname to be overwritten for multifile torrents only
        if 'files' in metainfo['info'] and self.dlconfig['correctedfilename'] and self.dlconfig['correctedfilename'] != '':
            self.correctedinfoname = self.dlconfig['correctedfilename']

        self.files = []
        if 'files' in metainfo['info']:
            for x in metainfo['info']['files']:
                savepath = torrentfilerec2savefilename(x)
                full = savefilenames2finaldest(self.get_content_dest(), savepath)
                # Arno: TODO: this sometimes gives too long filenames for 
                # Windows. When fixing this take into account that 
                # Download.get_dest_files() should still produce the same
                # filenames as your modifications here.
                self.files.append((full, x['length']))
        else:
            self.files.append((self.get_content_dest(), metainfo['info']['length']))
    def get_dest_files(self, exts = None):
        """
        You can give a list of extensions to return. If None: return all dest_files
        @return list of (torrent,disk) filename tuples.
        """

        def get_ext(filename):
            _, ext = os.path.splitext(filename)
            if ext != '' and ext[0] == '.':
                ext = ext[1:]
            return ext
        
        with self.dllock:
            f2dlist = []
            metainfo = self.tdef.get_metainfo() 
            if metainfo:
                if 'files' not in metainfo['info']:
                    # single-file torrent
                    diskfn = self.get_content_dest()
                    _, filename = os.path.split(diskfn)
                    f2dtuple = (filename, diskfn)
                    ext = get_ext(diskfn)
                    if exts is None or ext in exts:
                        f2dlist.append(f2dtuple)
                else:
                    # multi-file torrent
                    if len(self.dlconfig['selected_files']) > 0:
                        fnlist = self.dlconfig['selected_files']
                    else:
                        fnlist = self.tdef.get_files(exts=exts)
                        
                    for filename in fnlist:
                        filerec = maketorrent.get_torrentfilerec_from_metainfo(filename,metainfo)
                        savepath = maketorrent.torrentfilerec2savefilename(filerec)
                        diskfn = maketorrent.savefilenames2finaldest(self.get_content_dest(),savepath)
                        ext = get_ext(diskfn)
                        if exts is None or ext in exts:
                            f2dtuple = (filename,diskfn)
                            f2dlist.append(f2dtuple)
            return f2dlist
    def get_dest_files(self, exts = None):
        """
        You can give a list of extensions to return. If None: return all dest_files
        @return list of (torrent,disk) filename tuples.
        """

        def get_ext(filename):
            _, ext = os.path.splitext(filename)
            if ext != '' and ext[0] == '.':
                ext = ext[1:]
            return ext
        
        with self.dllock:
            f2dlist = []
            metainfo = self.tdef.get_metainfo() 
            if metainfo:
                if 'files' not in metainfo['info']:
                    # single-file torrent
                    diskfn = self.get_content_dest()
                    _, filename = os.path.split(diskfn)
                    f2dtuple = (filename, diskfn)
                    ext = get_ext(diskfn)
                    if exts is None or ext in exts:
                        f2dlist.append(f2dtuple)
                else:
                    # multi-file torrent
                    if len(self.dlconfig['selected_files']) > 0:
                        fnlist = self.dlconfig['selected_files']
                    else:
                        fnlist = self.tdef.get_files(exts=exts)
                        
                    for filename in fnlist:
                        filerec = maketorrent.get_torrentfilerec_from_metainfo(filename,metainfo)
                        savepath = maketorrent.torrentfilerec2savefilename(filerec)
                        diskfn = maketorrent.savefilenames2finaldest(self.get_content_dest(),savepath)
                        ext = get_ext(diskfn)
                        if exts is None or ext in exts:
                            f2dtuple = (filename,diskfn)
                            f2dlist.append(f2dtuple)
            return f2dlist