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

Make PyMuPDF always log to stderr #879

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions dangerzone/conversion/doc_to_pixels.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
import sys
from typing import Dict, Optional

# XXX: PyMUPDF logs to stdout by default [1]. The PyMuPDF devs provide a way [2] to log to
# stderr, but it's based on environment variables. These envvars are consulted at import
# time [3], so we have to set them here, before we import `fitz`.
#
# [1] https://github.com/freedomofpress/dangerzone/issues/877
# [2] https://github.com/pymupdf/PyMuPDF/issues/3135#issuecomment-1992625724
# [3] https://github.com/pymupdf/PyMuPDF/blob/9717935eeb2d50d15440d62575878214226795f9/src/__init__.py#L62-L63
os.environ["PYMUPDF_MESSAGE"] = "fd:2"
os.environ["PYMUPDF_LOG"] = "fd:2"


import fitz
import magic

Expand Down
25 changes: 17 additions & 8 deletions dangerzone/conversion/pixels_to_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@

from .common import DEFAULT_DPI, DangerzoneConverter, get_tessdata_dir, running_on_qubes

# XXX: PyMUPDF logs to stdout by default [1]. The PyMuPDF devs provide a way [2] to log to
# stderr, but it's based on environment variables. These envvars are consulted at import
# time [3], so we have to set them here, before we import `fitz`.
#
# [1] https://github.com/freedomofpress/dangerzone/issues/877
# [2] https://github.com/pymupdf/PyMuPDF/issues/3135#issuecomment-1992625724
# [3] https://github.com/pymupdf/PyMuPDF/blob/9717935eeb2d50d15440d62575878214226795f9/src/__init__.py#L62-L63
os.environ["PYMUPDF_MESSAGE"] = "fd:2"
os.environ["PYMUPDF_LOG"] = "fd:2"


class PixelsToPDF(DangerzoneConverter):
async def convert(
Expand Down Expand Up @@ -50,14 +60,13 @@ async def convert(
# The first few operations happen on a per-page basis.
page_size = len(untrusted_rgb_data)
total_size += page_size
with contextlib.redirect_stdout(io.StringIO()):
pixmap = fitz.Pixmap(
fitz.Colorspace(fitz.CS_RGB),
width,
height,
untrusted_rgb_data,
False,
)
pixmap = fitz.Pixmap(
fitz.Colorspace(fitz.CS_RGB),
width,
height,
untrusted_rgb_data,
False,
)
pixmap.set_dpi(DEFAULT_DPI, DEFAULT_DPI)
if ocr_lang: # OCR the document
self.update_progress(
Expand Down
7 changes: 6 additions & 1 deletion tests/test_large_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ def run_doc_test(self, doc: Path, tmp_path: Path) -> None:
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
out, _ = p.communicate()
try:
# Set a global timeout of 5 minutes for the processing of a document.
# This is hacky way to sidestep https://github.com/freedomofpress/dangerzone/issues/878
out, _ = p.communicate(timeout=5 * 60)
except subprocess.TimeoutExpired:
print(f"*** TIMEOUT EXCEEDED FOR DOCUMENT '{doc}' ***")
from strip_ansi import strip_ansi

print(strip_ansi(out.decode()))
Expand Down
Loading