Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to rebase stage 2 - import fitz gets rebased implementation. #2917

Merged
merged 8 commits into from
Dec 20, 2023
Merged
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
# built documents.
#
# The full version, including alpha/beta/rc tags.
_path = os.path.abspath(f'{__file__}/../../fitz/version.i')
_path = os.path.abspath(f'{__file__}/../../src/__init__.py')
with open(_path) as f:
for line in f:
match = re.search('VersionBind = "([0-9][.][0-9]+[.][0-9]+(rc[0-9]+)?)"', line)
Expand Down
511 changes: 0 additions & 511 deletions fitz/__init__.py

This file was deleted.

517 changes: 517 additions & 0 deletions fitz_old/__init__.py

Large diffs are not rendered by default.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions fitz/utils.py → fitz_old/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import typing
import warnings

from fitz import *
from fitz_old import *

TESSDATA_PREFIX = os.getenv("TESSDATA_PREFIX")
point_like = "point_like"
Expand Down Expand Up @@ -275,7 +275,7 @@ def delete_image(page: Page, xref: int):
xref: xref of the image to delete.
"""
# make a small 100% transparent pixmap (of just any dimension)
pix = fitz.Pixmap(fitz.csGRAY, (0, 0, 1, 1), 1)
pix = fitz_old.Pixmap(fitz_old.csGRAY, (0, 0, 1, 1), 1)
pix.clear_with() # clear all samples bytes to 0x00
page.replace_image(xref, pixmap=pix)

Expand Down
File renamed without changes.
150 changes: 19 additions & 131 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,6 @@
If '0' we do not rebuild if only fitz/helper-git-versions.i has
changed.

PYMUPDF_SETUP_SKELETON
If '1' we build minimal wheel for testing.

WDEV_VS_YEAR
If set, we use as Visual Studio year, for example '2019' or '2022'.

Expand Down Expand Up @@ -513,115 +510,6 @@ def build():
'''
pipcl.py `build_fn()` callback.
'''
skeleton = os.environ.get( 'PYMUPDF_SETUP_SKELETON')
log( f'{skeleton=}')
if skeleton == '1':
ret = list()
log( f'{g_flavour=}')
run( f'ls -l wheelhouse', check=0)
if 'b' in g_flavour:
with open( 'foo.c', 'w') as f:
f.write( textwrap.dedent( '''
int foo(int x)
{
return x+1;
}
'''))
run(f'cc -fPIC -shared -o {g_root}/libfoo.so foo.c')
ret.append( f'{g_root}/libfoo.so')
ret.append( (f'{g_root}/READMErb.md', '$dist-info/README.md'))
if 'p' in g_flavour:
with open( 'bar.c', 'w') as f:
f.write( textwrap.dedent( '''
int bar(int x)
{
return x+1;
}
'''))
run(f'cc -fPIC -shared -o {g_root}/_bar.so bar.c')
with open( 'bar.py', 'w') as f:
f.write( textwrap.dedent( '''
def bar(x):
return x - 1
'''))
ret.append( f'{g_root}/bar.py')
ret.append( f'{g_root}/_bar.so')
ret.append( (f'{g_root}/README.md', '$dist-info/README.md'))
return ret

elif skeleton == '2':
os.makedirs( 'src-skeleton2', exist_ok=True)
ret = list()
#cc, pythonflags = pipcl.base_compiler()
#ld, pythonflags = pipcl.base_linker()
if 1:
# Build minimal libmupdf.so.
cc, _ = pipcl.base_compiler()
with open( 'src-skeleton2/mupdf.c', 'w') as f:
f.write( textwrap.dedent('''
int foo(int x)
{
return x + 1;
}
'''))
# Use of rpath here is Linux/OpenBSD-specific.
run(f'{cc} -o src-skeleton2/libmupdf.so src-skeleton2/mupdf.c -fPIC -shared -Wl,-rpath,\'$ORIGIN\',-z,origin')
ret.append( ('src-skeleton2/libmupdf.so', ''))
if 'p' in g_flavour:
# Build extension module `fitz`.
with open( 'src-skeleton2/fitz.i', 'w') as f:
f.write( textwrap.dedent('''
%module fitz

%{
int foo(int x);
int bar(int x)
{
return foo(x) * 2;
}
%}

int bar(int x);
'''))
path_so_leaf_a = pipcl.build_extension(
name = 'fitz',
path_i = 'src-skeleton2/fitz.i',
outdir = 'src-skeleton2',
cpp = False,
libpaths = ['src-skeleton2'],
libs = ['mupdf'],
)

with open( 'src-skeleton2/fitz.i', 'w') as f:
f.write( textwrap.dedent('''
%module fitz_new

%{
int foo(int x);
int bar(int x)
{
return foo(x) * 2;
}
%}

int bar(int x);
'''))
path_so_leaf_b = pipcl.build_extension(
name = 'fitz_new',
path_i = 'src-skeleton2/fitz.i',
outdir = 'src-skeleton2',
cpp = False,
libpaths = ['src-skeleton2'],
libs = ['mupdf'],
)
ret.append( (f'src-skeleton2/{path_so_leaf_a}', ''))
ret.append( (f'src-skeleton2/fitz.py', ''))
ret.append( (f'src-skeleton2/{path_so_leaf_b}', ''))
ret.append( (f'src-skeleton2/fitz_new.py', ''))
ret.append( (f'{g_root}/README.md', '$dist-info/README.md'))
return ret


# Download MuPDF.
#
mupdf_local = get_mupdf()
Expand All @@ -634,7 +522,7 @@ def bar(x):
#
env_extra = dict()
if mupdf_local:
from_ = f'{g_root}/fitz/_config.h'
from_ = f'{g_root}/fitz_old/_config.h'
to_ = f'{mupdf_local}/include/mupdf/fitz/config.h'
if os.environ.get('PYMUPDF_SETUP_MUPDF_OVERWRITE_CONFIG') == '0':
# Use MuPDF default config.
Expand All @@ -657,7 +545,7 @@ def bar(x):
mupdf_build_dir = build_mupdf_unix( mupdf_local, env_extra, build_type)
log( f'build(): mupdf_build_dir={mupdf_build_dir!r}')

# Build rebased `extra` module and/or PyMuPDF `fitz` module.
# Build rebased `extra` module and/or PyMuPDF `fitz_old` module.
#
path_so_leaf_a, path_so_leaf_b = _build_extensions(
mupdf_local,
Expand All @@ -667,7 +555,7 @@ def bar(x):

for d in (
mupdf_build_dir,
f'{g_root}/fitz',
f'{g_root}/fitz_old',
f'{g_root}/src',
):
if d:
Expand All @@ -684,13 +572,13 @@ def add( ret, from_, to_):

if path_so_leaf_a:
# Add classic implementation files.
to_dir = 'fitz/'
add( ret_p, f'{g_root}/fitz/__init__.py', to_dir)
add( ret_p, f'{g_root}/fitz/__main__.py', to_dir)
add( ret_p, f'{g_root}/fitz/fitz.py', to_dir)
add( ret_p, f'{g_root}/fitz/table.py', to_dir)
add( ret_p, f'{g_root}/fitz/utils.py', to_dir)
add( ret_p, f'{g_root}/fitz/{path_so_leaf_a}', to_dir)
to_dir = 'fitz_old/'
add( ret_p, f'{g_root}/fitz_old/__init__.py', to_dir)
add( ret_p, f'{g_root}/fitz_old/__main__.py', to_dir)
add( ret_p, f'{g_root}/fitz_old/fitz_old.py', to_dir)
add( ret_p, f'{g_root}/src/table.py', to_dir)
add( ret_p, f'{g_root}/fitz_old/utils.py', to_dir)
add( ret_p, f'{g_root}/fitz_old/{path_so_leaf_a}', to_dir)

if mupdf_local:
# Add mupdf shared library next to `path_so_leaf_a` so it will be
Expand All @@ -708,11 +596,11 @@ def add( ret, from_, to_):

if path_so_leaf_b:
# Add rebased implementation files.
to_dir = 'fitz_new/' if path_so_leaf_a else 'fitz/'
to_dir = 'fitz/'
add( ret_p, f'{g_root}/src/__init__.py', to_dir)
add( ret_p, f'{g_root}/src/__main__.py', to_dir)
add( ret_p, f'{g_root}/src/fitz.py', to_dir)
add( ret_p, f'{g_root}/fitz/table.py', to_dir)
add( ret_p, f'{g_root}/src/table.py', to_dir)
add( ret_p, f'{g_root}/src/utils.py', to_dir)
add( ret_p, f'{g_root}/src/extra.py', to_dir)
add( ret_p, f'{g_root}/src/{path_so_leaf_b}', to_dir)
Expand Down Expand Up @@ -959,10 +847,10 @@ def write_git(name, directory):
if mupdf_local:
write_git('mupdf', mupdf_local)
f.write('%}\n')
_fs_update( f.getvalue(), 'fitz/helper-git-versions.i')
_fs_update( f.getvalue(), 'fitz_old/helper-git-versions.i')
if os.environ.get( 'PYMUPDF_SETUP_REBUILD_GIT_DETAILS') == '0':
log( f'Marking fitz/helper-git-versions.i as old because PYMUPDF_SETUP_REBUILD_GIT_DETAILS=0')
os.utime( 'fitz/helper-git-versions.i', (1, 1))
log( f'Marking fitz_old/helper-git-versions.i as old because PYMUPDF_SETUP_REBUILD_GIT_DETAILS=0')
os.utime( 'fitz_old/helper-git-versions.i', (1, 1))

if windows:
compiler_extra_c = ''
Expand All @@ -972,7 +860,7 @@ def write_git(name, directory):
' -Wno-pointer-sign'
' -Wno-sign-compare'
)
prerequisites_swig = glob.glob( f'{g_root}/fitz/*.i')
prerequisites_swig = glob.glob( f'{g_root}/fitz_old/*.i')
if os.environ.get( 'PYMUPDF_SETUP_REBUILD_GIT_DETAILS') == '0':
# Remove helper-git-versions.i from prerequisites_swig so
# it doesn't force rebuild on its own. [Cannot easily use
Expand All @@ -986,9 +874,9 @@ def write_git(name, directory):
assert 0, f'Cannot find *helper-git-versions.i in prerequisites_swig: {prerequisites_swig}'

path_so_leaf_a = pipcl.build_extension(
name = 'fitz',
path_i = f'{g_root}/fitz/fitz.i',
outdir = f'{g_root}/fitz',
name = 'fitz_old',
path_i = f'{g_root}/fitz_old/fitz_old.i',
outdir = f'{g_root}/fitz_old',
includes = includes,
defines = defines,
libpaths = libpaths,
Expand Down
File renamed without changes.
29 changes: 15 additions & 14 deletions tests/run_compound.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
'''
Runs a command three times:

1. Run unchanged. This will use the default `fitz` implementation of PyMuPDF.
1. Run unchanged. This will use the default `fitz_old` implementation of PyMuPDF.

2. Run with PYTHONPATH set up to use the `fitz_new` implementation of PyMuPDF.
2. Run with PYTHONPATH set up to use the `fitz` implementation of PyMuPDF.

3. As 2 but also set PYMUPDF_USE_EXTRA=0 to disable use of C++ optimisations.

Expand Down Expand Up @@ -33,14 +33,9 @@ def main():

args = sys.argv[1:]

# Run with default `fitz`.
#
log( f'Running using fitz: {shlex.join(args)}')
e1 = subprocess.run( args, shell=0, check=0).returncode

# Run with `fitz_new`. We create a file fitz.py that does `from fitz_new
# Run with `fitz_old` (classic). We create a file fitz.py that does `from fitz_old
# import *` and prepend it to PYTHONPATH. So `import fitz` will actually
# import fitz_new as fitz.
# import fitz_old as fitz.
#
d = os.path.abspath( f'{__file__}/../resources')

Expand All @@ -51,22 +46,28 @@ def main():
f.write( textwrap.dedent( f'''
#import sys
#print(f'{{__file__}}: {{sys.path=}}')
#print(f'{{__file__}}: Importing * from fitz_new')
#print(f'{{__file__}}: Importing * from fitz_old')
#sys.stdout.flush()
from fitz_new import *
from fitz_old import *
'''))

env = os.environ.copy()
pp = env.get( 'PYTHONPATH')
pp = d if pp is None else f'{d}:{pp}'
env[ 'PYTHONPATH'] = pp
log(f'Running using fitz_new, PYTHONPATH={pp}: {shlex.join(args)}')
log(f'Running using fitz_old (classic), PYTHONPATH={pp}: {shlex.join(args)}')
e2 = subprocess.run( args, shell=0, check=0, env=env).returncode

# Run with `fitz_new` again, this time with PYMUPDF_USE_EXTRA=0.
# Run with default `fitz` (rebased).
#
log( f'Running using fitz (rebased): {shlex.join(args)}')
e1 = subprocess.run( args, shell=0, check=0).returncode

# Run with `fitz` (rebased) again, this time with PYMUPDF_USE_EXTRA=0.
#
env = os.environ.copy()
env[ 'PYMUPDF_USE_EXTRA'] = '0'
log(f'Running using fitz_new without optimisations, PYTHONPATH={pp}: {shlex.join(args)}')
log(f'Running using fitz (rebased) without optimisations, PYTHONPATH={pp}: {shlex.join(args)}')
e3 = subprocess.run( args, shell=0, check=0, env=env).returncode

log( f'{e1=} {e2=} {e3=}')
Expand Down
2 changes: 1 addition & 1 deletion tests/test_flake8.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@ def run(command):
print(f'test_flake8(): Running: {command}')
subprocess.run(command, shell=1, check=1)
run(f'pip install flake8')
run(f'flake8 --ignore={ignores} --statistics {root}/src/__init__.py {root}/src/utils.py {root}/fitz/table.py')
run(f'flake8 --ignore={ignores} --statistics {root}/src/__init__.py {root}/src/utils.py {root}/src/table.py')
print(f'test_flake8(): flake8 succeeded.')
Loading