Beispiel #1
0
def download(self, url, file_name, headers=None, show_progress=True):
    '''stream to a temporary file, rename on successful completion

        Parameters
        ==========
        file_name: the file name to stream to
        url: the url to stream from
        headers: additional headers to add
        force: If the final image exists, don't overwrite
        show_progress: boolean to show progress bar
    '''

    tmp_file = get_tmpfile(prefix="%s.tmp." % file_name)

    # Should we verify the request?
    verify = self._verify()

    # Check here if exists
    if requests.head(url, verify=verify).status_code in [200, 401]:
        response = self.stream(url,
                               headers=headers,
                               stream_to=tmp_file,
                               show_progress=show_progress)

        if isinstance(response, HTTPError):
            bot.exit("Error downloading %s, exiting." % url)

        shutil.move(tmp_file, file_name)
    else:
        bot.error("Invalid url or permissions %s" % url)
    return file_name
Beispiel #2
0
def test_get_tmpdir_tmpfile():
    print("Testing utils.get_tmpdir, get_tmpfile")
    from sregistry.utils import get_tmpdir, get_tmpfile
    tmpdir = get_tmpdir()
    assert os.path.exists(tmpdir)
    assert os.path.basename(tmpdir).startswith('sregistry')
    shutil.rmtree(tmpdir)
    tmpdir = get_tmpdir(prefix='name')
    assert os.path.basename(tmpdir).startswith('name')
    shutil.rmtree(tmpdir)
    tmpfile = get_tmpfile()
    assert 'sregistry' in tmpfile
    os.remove(tmpfile)
    tmpfile = get_tmpfile(prefix="pancakes")
    assert 'pancakes' in tmpfile
    os.remove(tmpfile)