Leseratte10 / acsm-calibre-plugin

Calibre plugin for ACSM->EPUB and ACSM->PDF conversion.
https://www.mobileread.com/forums/showthread.php?t=341975
GNU General Public License v3.0
593 stars 23 forks source link

Fix imports for FreeBSD #87

Closed vasi closed 1 month ago

vasi commented 1 month ago

On recent versions of Calibre on BSD-based OSes, it appears that the plugin root directory (or ZIP file) is not always automatically added to the import path.

While we could change the plugin to always import using Calibre's plugin import system (eg: import calibre_plugins.deacsm.libadobe), that would be an invasive change. Instead, just force the directory/ZIP containing __init__.py to always be in the import path. A bit of a hack, but it works.

Also fix a missing sys import.

Tested on Calibre 7.8. Working:

vasi commented 1 month ago

Fixes #72

Leseratte10 commented 1 month ago

Thanks for that MR, but I haven't quite understood how / why this bugfix works. The init.py which you modified to include that line that modifies sys.path already exists in __calibre_compat_code.py and should already automatically be executed whenever the plugin is running inside of Calibre:

if "calibre" in sys.modules:
    # Explicitly allow importing everything ...
    if os.path.dirname(os.path.abspath(__file__)) not in sys.path:
        sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

Shouldn't that piece of code be enough and have the exact same functionality as your code change?

Are you trying to run the calibre plugin without running the bundle_calibre_plugin.sh script first? The missing sys import you added is also not strictly necessary (but probably a good idea to have clean code) since sys is already included from the file mentioned above.

If you say the issue only happens on FreeBSD, can you check / make sure that the statement if "calibre" in sys.module is true within Calibre's python environment?

vasi commented 1 month ago

Ahh I see what's happening! The bundle_calibre_plugin.sh script uses sed -i, which is only a GNU-sed thing. On platforms with non-GNU sed, it interprets -i as a filename, and doesn't edit in place. That's why this affects both macOS and BSDs—it probably affects distros like Chimera that have a non-GNU userland, too.

There's a few potential solutions, I'm not sure what I like most:

  1. Allow the script's caller to specify what sed to call, eg: by using a SED variable
  2. Have the script automatically choose sed vs gsed
  3. Instead of using sed -i, use a wrapper function that does read/write-to-tmpfile/replace
  4. Use some other program, instead of sed. Perl is the obvious choice?

While we're at it, we should probably fix the shebang in these files too. Not all systems have a /bin/bash, which can make bundle_calibre_plugin.sh's attempt to call package_modules.sh fail.

vasi commented 1 month ago

Closing in favor of #88