(self, url, destfile, digest=None, reporthook=None)
This is a convenience method for downloading a file from an URL.
        Normally, this will be a file from the index, though currently
        no check is made for this (i.e. a file can be downloaded from
        anywhere).

        The method is just like the :func:`urlretrieve` function in the
        standard library, except that it allows digest computation to be
        done during download and checking that the downloaded data
        matched any expected value.

        :param url: The URL of the file to be downloaded (assumed to be
                    available via an HTTP GET request).
        :param destfile: The pathname where the downloaded file is to be
                         saved.
        :param digest: If specified, this must be a (hasher, value)
                       tuple, where hasher is the algorithm used (e.g.
                       ``'md5'``) and ``value`` is the expected value.
        :param reporthook: The same as for :func:`urlretrieve` in the
                           standard library.
This is a convenience method for downloading a file from an URL.
        Normally, this will be a file from the index, though currently
        no check is made for this (i.e. a file can be downloaded from
        anywhere).
This is a convenience method for downloading a file from an URL . Normally this will be a file from the index though currently no check is made for this ( i . e . a file can be downloaded from anywhere ) .
def download_file(self, url, destfile, digest=None, reporthook=None):
        """
        This is a convenience method for downloading a file from an URL.
        Normally, this will be a file from the index, though currently
        no check is made for this (i.e. a file can be downloaded from
        anywhere).

        The method is just like the :func:`urlretrieve` function in the
        standard library, except that it allows digest computation to be
        done during download and checking that the downloaded data
        matched any expected value.

        :param url: The URL of the file to be downloaded (assumed to be
                    available via an HTTP GET request).
        :param destfile: The pathname where the downloaded file is to be
                         saved.
        :param digest: If specified, this must be a (hasher, value)
                       tuple, where hasher is the algorithm used (e.g.
                       ``'md5'``) and ``value`` is the expected value.
        :param reporthook: The same as for :func:`urlretrieve` in the
                           standard library.
        """
        if digest is None:
            digester = None
            logger.debug('No digest specified')
        else:
            if isinstance(digest, (list, tuple)):
                hasher, digest = digest
            else:
                hasher = 'md5'
            digester = getattr(hashlib, hasher)()
            logger.debug('Digest specified: %s' % digest)
        # The following code is equivalent to urlretrieve.
        # We need to do it this way so that we can compute the
        # digest of the file as we go.
        with open(destfile, 'wb') as dfp:
            # addinfourl is not a context manager on 2.x
            # so we have to use try/finally
            sfp = self.send_request(Request(url))
            try:
                headers = sfp.info()
                blocksize = 8192
                size = -1
                read = 0
                blocknum = 0
                if "content-length" in headers:
                    size = int(headers["Content-Length"])
                if reporthook:
                    reporthook(blocknum, blocksize, size)
                while True:
                    block = sfp.read(blocksize)
                    if not block:
                        break
                    read += len(block)
                    dfp.write(block)
                    if digester:
                        digester.update(block)
                    blocknum += 1
                    if reporthook:
                        reporthook(blocknum, blocksize, size)
            finally:
                sfp.close()

        # check that we got the whole file, if we can
        if size >= 0 and read < size:
            raise DistlibException(
                'retrieval incomplete: got only %d out of %d bytes'
                % (read, size))
        # if we have a digest, it must match.
        if digester:
            actual = digester.hexdigest()
            if digest != actual:
                raise DistlibException('%s digest mismatch for %s: expected '
                                       '%s, got %s' % (hasher, destfile,
                                                       digest, actual))
            logger.debug('Digest verified: %s', digest)
def download_file ( self , url , destfile , digest = None , reporthook = None ) : if digest is None : digester = None logger . debug ( 'No digest specified' ) else : if isinstance ( digest , ( list , tuple ) ) : hasher , digest = digest else : hasher = 'md5' digester = getattr ( hashlib , hasher ) ( ) logger . debug ( 'Digest specified: %s' % digest ) # The following code is equivalent to urlretrieve. # We need to do it this way so that we can compute the # digest of the file as we go. with open ( destfile , 'wb' ) as dfp : # addinfourl is not a context manager on 2.x # so we have to use try/finally sfp = self . send_request ( Request ( url ) ) try : headers = sfp . info ( ) blocksize = 8192 size = - 1 read = 0 blocknum = 0 if "content-length" in headers : size = int ( headers [ "Content-Length" ] ) if reporthook : reporthook ( blocknum , blocksize , size ) while True : block = sfp . read ( blocksize ) if not block : break read += len ( block ) dfp . write ( block ) if digester : digester . update ( block ) blocknum += 1 if reporthook : reporthook ( blocknum , blocksize , size ) finally : sfp . close ( ) # check that we got the whole file, if we can if size >= 0 and read < size : raise DistlibException ( 'retrieval incomplete: got only %d out of %d bytes' % ( read , size ) ) # if we have a digest, it must match. if digester : actual = digester . hexdigest ( ) if digest != actual : raise DistlibException ( '%s digest mismatch for %s: expected ' '%s, got %s' % ( hasher , destfile , digest , actual ) ) logger . debug ( 'Digest verified: %s' , digest )