diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/util.py	Mon Sep 15 11:37:51 2025 +0200
@@ -0,0 +1,28 @@
+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