Mercurial > hgrepos > Python2 > PyMuPDF
comparison tests/run_compound.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 #! /usr/bin/env python3 | |
| 2 | |
| 3 ''' | |
| 4 Runs a command using different implementations of PyMuPDF: | |
| 5 | |
| 6 1. Run with rebased implementation of PyMuPDF. | |
| 7 | |
| 8 2. As 1 but also set PYMUPDF_USE_EXTRA=0 to disable use of C++ optimisations. | |
| 9 | |
| 10 Example usage: | |
| 11 | |
| 12 ./PyMuPDF/tests/run_compound.py python -m pytest -s PyMuPDF | |
| 13 | |
| 14 Use `-i <implementations>` to select which implementations to use. In | |
| 15 `<implementations>`, `r` means rebased, `R` means rebased without | |
| 16 optimisations. | |
| 17 | |
| 18 For example use the rebased and unoptimised rebased implementations with: | |
| 19 | |
| 20 ./PyMuPDF/tests/run_compound.py python -m pytest -s PyMuPDF | |
| 21 ''' | |
| 22 | |
| 23 import shlex | |
| 24 import os | |
| 25 import platform | |
| 26 import subprocess | |
| 27 import sys | |
| 28 import textwrap | |
| 29 import time | |
| 30 | |
| 31 | |
| 32 def log(text): | |
| 33 print(textwrap.indent(text, 'PyMuPDF:tests/run_compound.py: ')) | |
| 34 sys.stdout.flush() | |
| 35 | |
| 36 | |
| 37 def log_star(text): | |
| 38 log('#' * 40) | |
| 39 log(text) | |
| 40 log('#' * 40) | |
| 41 | |
| 42 | |
| 43 def main(): | |
| 44 | |
| 45 implementations = 'rR' | |
| 46 timeout = None | |
| 47 i = 1 | |
| 48 while i < len(sys.argv): | |
| 49 arg = sys.argv[i] | |
| 50 if arg == '-i': | |
| 51 i += 1 | |
| 52 implementations = sys.argv[i] | |
| 53 elif arg == '-t': | |
| 54 i += 1 | |
| 55 timeout = float(sys.argv[i]) | |
| 56 elif arg.startswith('-'): | |
| 57 raise Exception(f'Unrecognised {arg=}.') | |
| 58 else: | |
| 59 break | |
| 60 i += 1 | |
| 61 args = sys.argv[i:] | |
| 62 | |
| 63 e_rebased = None | |
| 64 e_rebased_unoptimised = None | |
| 65 | |
| 66 endtime = None | |
| 67 if timeout: | |
| 68 endtime = time.time() + timeout | |
| 69 | |
| 70 # Check `implementations`. | |
| 71 implementations_seen = set() | |
| 72 for i in implementations: | |
| 73 assert i not in implementations_seen, f'Duplicate implementation {i!r} in {implementations!r}.' | |
| 74 if i == 'r': | |
| 75 name = 'rebased' | |
| 76 elif i == 'R': | |
| 77 name = 'rebased (unoptimised)' | |
| 78 else: | |
| 79 assert 0, f'Unrecognised implementation {i!r} in {implementations!r}.' | |
| 80 log(f' {i!r}: will run with PyMuPDF {name}.') | |
| 81 implementations_seen.add(i) | |
| 82 | |
| 83 for i in implementations: | |
| 84 log(f'run_compound.py: {i=}') | |
| 85 | |
| 86 cpu_bits = int.bit_length(sys.maxsize+1) | |
| 87 log(f'{os.getcwd()=}') | |
| 88 log(f'{platform.machine()=}') | |
| 89 log(f'{platform.platform()=}') | |
| 90 log(f'{platform.python_version()=}') | |
| 91 log(f'{platform.system()=}') | |
| 92 if sys.implementation.name != 'graalpy': | |
| 93 log(f'{platform.uname()=}') | |
| 94 log(f'{sys.executable=}') | |
| 95 log(f'{sys.version=}') | |
| 96 log(f'{sys.version_info=}') | |
| 97 log(f'{list(sys.version_info)=}') | |
| 98 log(f'{cpu_bits=}') | |
| 99 | |
| 100 timeout = None | |
| 101 if endtime: | |
| 102 timeout = max(0, endtime - time.time()) | |
| 103 if i == 'r': | |
| 104 | |
| 105 # Run with default `pymupdf` (rebased). | |
| 106 # | |
| 107 log_star( f'Running using pymupdf (rebased): {shlex.join(args)}') | |
| 108 e_rebased = subprocess.run( args, shell=0, check=0, timeout=timeout).returncode | |
| 109 | |
| 110 elif i == 'R': | |
| 111 | |
| 112 # Run with `pymupdf` (rebased) again, this time with PYMUPDF_USE_EXTRA=0. | |
| 113 # | |
| 114 env = os.environ.copy() | |
| 115 env[ 'PYMUPDF_USE_EXTRA'] = '0' | |
| 116 log_star(f'Running using pymupdf (rebased) with PYMUPDF_USE_EXTRA=0: {shlex.join(args)}') | |
| 117 e_rebased_unoptimised = subprocess.run( args, shell=0, check=0, env=env, timeout=timeout).returncode | |
| 118 | |
| 119 else: | |
| 120 raise Exception(f'Unrecognised implementation {i!r}.') | |
| 121 | |
| 122 if e_rebased is not None: | |
| 123 log(f'{e_rebased=}') | |
| 124 if e_rebased_unoptimised is not None: | |
| 125 log(f'{e_rebased_unoptimised=}') | |
| 126 | |
| 127 if e_rebased or e_rebased_unoptimised: | |
| 128 log('Test(s) failed.') | |
| 129 return 1 | |
| 130 | |
| 131 | |
| 132 if __name__ == '__main__': | |
| 133 try: | |
| 134 sys.exit(main()) | |
| 135 except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as e: | |
| 136 # Terminate relatively quietly, failed commands will usually have | |
| 137 # generated diagnostics. | |
| 138 log(str(e)) | |
| 139 sys.exit(1) |
