Solve import errors by lazy-loading fitz module

Qubes does on-host pixels-to-pdf whereas the containers version doesn't.
This leads to an issue where on the containers version it tries to load
fitz, which isn't installed there, just because it's trying to check if
it should run the Qubes version.

The error it was showing was something like this:

    ImportError while loading conftest '/home/user/dangerzone/tests/conftest.py'.
        tests/__init__.py:8: in <module>
            from dangerzone.document import SAFE_EXTENSION
        dangerzone/__init__.py:16: in <module>
            from .gui import gui_main as main
        dangerzone/gui/__init__.py:28: in <module>
            from ..isolation_provider.qubes import Qubes, is_qubes_native_conversion
        dangerzone/isolation_provider/qubes.py:15: in <module>
            from ..conversion.pixels_to_pdf import PixelsToPDF
        dangerzone/conversion/pixels_to_pdf.py:16: in <module>
            import fitz
        E   ModuleNotFoundError: No module named 'fitz'

For context see discussion in [1].

[1]: https://github.com/freedomofpress/dangerzone/pull/622#issuecomment-1839164885
This commit is contained in:
deeplow 2023-12-20 16:44:55 +00:00
parent 773fcfa75b
commit 6f61e44502
No known key found for this signature in database
GPG key ID: 577982871529A52A

View file

@ -13,8 +13,6 @@ import shutil
import sys import sys
from typing import Optional from typing import Optional
import fitz
from .common import DEFAULT_DPI, DangerzoneConverter, get_tessdata_dir, running_on_qubes from .common import DEFAULT_DPI, DangerzoneConverter, get_tessdata_dir, running_on_qubes
@ -26,6 +24,9 @@ class PixelsToPDF(DangerzoneConverter):
if tempdir is None: if tempdir is None:
tempdir = "/tmp" tempdir = "/tmp"
# XXX lazy loading of fitz module to avoid import issues on non-Qubes systems
import fitz
num_pages = len(glob.glob(f"{tempdir}/dangerzone/page-*.rgb")) num_pages = len(glob.glob(f"{tempdir}/dangerzone/page-*.rgb"))
total_size = 0.0 total_size = 0.0