freedomofpress / dangerzone

Take potentially dangerous PDFs, office documents, or images and convert them to safe PDFs
https://dangerzone.rocks/
GNU Affero General Public License v3.0
3.68k stars 172 forks source link

On-host pixels to PDF conversion broke executables built with cx_freeze #974

Open jkarasti opened 1 week ago

jkarasti commented 1 week ago

What happened?

After #748 got merged, running dangerzone.exe or dangerzone-cli.exe both result in an error.

operating system version

Windows 10 Pro 22H2 (OS Build 19045.5073)

Dangerzone version

Top of main branch

Docker info

No response

Document conversion logs

No response

Additional info

Here's the error log I got:

Traceback (most recent call last):
  File "C:\Users\juhok\AppData\Local\pypoetry\Cache\virtualenvs\dangerzone-_8ncJGJf-py3.12\Lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 140, in run
    module_init.run(f"__main__{name}")
  File "C:\Users\juhok\AppData\Local\pypoetry\Cache\virtualenvs\dangerzone-_8ncJGJf-py3.12\Lib\site-packages\cx_Freeze\initscripts\console.py", line 25, in run
    exec(code, main_globals)
  File "install\windows\dangerzone-cli.py", line 1, in <module>
  File "C:\GitHub\dangerzone\dangerzone\__init__.py", line 27, in <module>
    from .cli import cli_main as main
  File "C:\GitHub\dangerzone\dangerzone\cli.py", line 10, in <module>
    from .isolation_provider.container import Container
  File "C:\GitHub\dangerzone\dangerzone\isolation_provider\container.py", line 12, in <module>
    from .base import IsolationProvider, terminate_process_group
  File "C:\GitHub\dangerzone\dangerzone\isolation_provider\base.py", line 12, in <module>
    import fitz
  File "C:\Users\juhok\AppData\Local\pypoetry\Cache\virtualenvs\dangerzone-_8ncJGJf-py3.12\Lib\site-packages\fitz\__init__.py", line 2, in <module>
    from pymupdf import *
  File "C:\Users\juhok\AppData\Local\pypoetry\Cache\virtualenvs\dangerzone-_8ncJGJf-py3.12\Lib\site-packages\pymupdf\__init__.py", line 21706, in <module>
    from . import utils
ImportError: cannot import name 'utils' from partially initialized module 'pymupdf' (most likely due to a circular import) (C \GitHub\dangerzone\build\exe.win-amd64-3.12\lib\pymupdf\__init__.pyc)
almet commented 1 week ago

Hey, thanks for the report. I'm curious: how did you run the dangerzone.exe, and did you update the installed dependencies? It seems weird that this error wasn't catched by the CI!

jkarasti commented 1 week ago

Hey, thanks for the report. I'm curious: how did you run the dangerzone.exe

I've built the executables following the Windows section in BUILD.md with poetry run python .\setup-windows.py build and then tried to launch dangerzone.exe inside exe.win-amd64-3.12 but hit the above error. Interestingly enough, running Dangerzone with poetry run .\dev_scripts\dangerzone.bat instead of the executables produced by cx_freeze works a-okay.

Did you update the installed dependencies?

I've tried to get Dangerzone running with a myriad of combinations of versions of cx_freeze between 7.2.0 and latest and pymupdf between 1.23.3 and latest. As far as I can tell, Dangerzone runs from commit 4abd4720 (commit before on-host PR) regradless of which version of cx_freeze or pymupdf is installed

It seems weird that this error wasn't catched by the CI!

At a cursory glance, I don't think CI runs the .exe that cx_freeze builds, so I don't think this particular edge case can get caught in the first place.

apyrgio commented 1 week ago

Ok, I just managed to reproduce it on my Windows machine. I have notified the developer of cx-Freeze to fix the underlying issue: https://github.com/marcelotduarte/cx_Freeze/issues/2653

(well, the real underling issue is PyMuPDF's... particular way of using __init__.py, but I digress)

You can apply this workaround in the meantime to make progress:

diff --git a/setup-windows.py b/setup-windows.py
index deae943..70291ad 100644
--- a/setup-windows.py
+++ b/setup-windows.py
@@ -4,7 +4,7 @@ from cx_Freeze import Executable, setup
 with open("share/version.txt") as f:
     version = f.read().strip()

-packages = ["dangerzone", "dangerzone.gui"]
+packages = ["dangerzone", "dangerzone.gui", "pymupdf.utils"]

 setup(
     name="dangerzone",

As always, muchas gracias for the help on the Wix front. I plan to be more involved with your PR once we're done with the 0.8.0 release.