Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
Build script for PyMuPDF, supporting PEP-517 and simple command-line usage.

We hard-code the URL of the MuPDF .tar.gz file that we require. This
generally points to a particular source release on mupdf.com.
generally points to a particular source release on mupdf.com. We also
hard-code its SHA-256, and check it after downloading.

Default behaviour:

Expand Down Expand Up @@ -219,6 +220,7 @@
'''

import glob
import hashlib
import io
import os
import textwrap
Expand Down Expand Up @@ -275,6 +277,10 @@

version_mupdf = '1.28.6'

# SHA-256 of the default MuPDF .tar.gz download, as listed on
# https://mupdf.com/releases. Must be updated with version_mupdf.
version_mupdf_sha256 = 'dc85000640a45d90ca9b77b061f6ca67cce0c4d4fbecf8819647baf573eef101'

g_requires_dist = list()

if PYMUPDF_SETUP_PATH_LAYOUT:
Expand Down Expand Up @@ -338,6 +344,16 @@ def mupdf_win32_infix():
return 'win32'


def sha256_check(path, sha256):
'''
Raises an exception if the SHA-256 of file <path> is not <sha256>.
'''
with open(path, 'rb') as f:
sha256_actual = hashlib.file_digest(f, 'sha256').hexdigest()
if sha256_actual != sha256:
raise Exception(f'SHA-256 mismatch for {path!r}: expected {sha256}, got {sha256_actual}.')


def tar_check(path, mode='r:gz', prefix=None, remove=False):
'''
Checks items in tar file have same <top-directory>, or <prefix> if not None.
Expand Down Expand Up @@ -437,8 +453,10 @@ def get_mupdf_internal(out, location=None, local_tgz=None):
'''
log(f'get_mupdf_internal(): {out=} {location=}')
assert out in ('dir', 'tgz')
sha256 = None
if location is None:
location = f'https://mupdf.com/downloads/archive/mupdf-{version_mupdf}-source.tar.gz'
sha256 = version_mupdf_sha256
#location = 'git:--branch master https://github.com/ArtifexSoftware/mupdf.git'

if location == '':
Expand Down Expand Up @@ -468,14 +486,18 @@ def get_mupdf_internal(out, location=None, local_tgz=None):
if os.path.exists(local_tgz):
try:
tar_check(local_tgz, 'r:gz', prefix=f'{name}/')
if sha256:
sha256_check(local_tgz, sha256)
except Exception as e:
log(f'Not using existing file {local_tgz} because invalid tar data: {e}')
log(f'Not using existing file {local_tgz} because invalid: {e}')
pipcl.fs_remove( local_tgz)
if os.path.exists(local_tgz):
log(f'Not downloading from {location} because already present: {local_tgz!r}')
else:
log(f'Downloading from {location=} to {local_tgz=}.')
urllib.request.urlretrieve( location, local_tgz + '-')
if sha256:
sha256_check(local_tgz + '-', sha256)
os.rename(local_tgz + '-', local_tgz)
assert os.path.exists( local_tgz)
tar_check( local_tgz, 'r:gz', prefix=f'{name}/')
Expand Down