view tests/util.py @ 29:f76e6575dca9 v1.26.4+1

+++++ v1.26.4+1
author Franz Glasner <fzglas.hg@dom66.de>
date Fri, 19 Sep 2025 19:59:23 +0200
parents 1d09e1dec1d9
children
line wrap: on
line source

import os
import subprocess


def download(url, name, size=None):
    '''
    Downloads from <url> to a local file and returns its path. 
    
    If file already exists and matches <size> we do not re-download it.
    
    We put local files within a `cache/` directory so that it is not deleted by
    `git clean` (unless `-d` is specified).
    '''
    path = os.path.normpath(f'{__file__}/../../tests/cache/{name}')
    if os.path.isfile(path) and (not size or os.stat(path).st_size == size):
        print(f'Using existing file {path=}.')
    else:
        print(f'Downloading from {url=}.')
        subprocess.run(f'pip install -U requests', check=1, shell=1)
        import requests
        r = requests.get(url, path, timeout=10)
        r.raise_for_status()
        if size is not None:
            assert len(r.content) == size
        os.makedirs(os.path.dirname(path), exist_ok=1)
        with open(path, 'wb') as f:
            f.write(r.content)
    return path