comparison tests/util.py @ 1:1d09e1dec1d9 upstream

ADD: PyMuPDF v1.26.4: the original sdist. It does not yet contain MuPDF. This normally will be downloaded when building PyMuPDF.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:37:51 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 1:1d09e1dec1d9
1 import os
2 import subprocess
3
4
5 def download(url, name, size=None):
6 '''
7 Downloads from <url> to a local file and returns its path.
8
9 If file already exists and matches <size> we do not re-download it.
10
11 We put local files within a `cache/` directory so that it is not deleted by
12 `git clean` (unless `-d` is specified).
13 '''
14 path = os.path.normpath(f'{__file__}/../../tests/cache/{name}')
15 if os.path.isfile(path) and (not size or os.stat(path).st_size == size):
16 print(f'Using existing file {path=}.')
17 else:
18 print(f'Downloading from {url=}.')
19 subprocess.run(f'pip install -U requests', check=1, shell=1)
20 import requests
21 r = requests.get(url, path, timeout=10)
22 r.raise_for_status()
23 if size is not None:
24 assert len(r.content) == size
25 os.makedirs(os.path.dirname(path), exist_ok=1)
26 with open(path, 'wb') as f:
27 f.write(r.content)
28 return path