From c2a2593b918c6095d821cfbb39180ac149fcc74b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20D=C3=B6rfelt?= Date: Sun, 14 Apr 2024 14:50:57 +0200 Subject: [PATCH] add hidden imports to pyinstaller --- .github/workflows/build.yml | 6 ++-- .gitignore | 1 - joplin_custom_importer.spec | 58 +++++++++++++++++++++++++++++++++++ src/joplin_custom_importer.py | 2 ++ 4 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 joplin_custom_importer.spec diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2412205..090af71 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -19,10 +19,12 @@ jobs: - name: Install dependencies run: pip install -r requirements-dev.txt - name: Build executable with pyinstaller - run: python -m PyInstaller src/joplin_custom_importer.py --onefile --collect-data pypandoc + run: python -m PyInstaller joplin_custom_importer.spec - uses: actions/upload-artifact@v4 with: name: joplin-custom-importer-${{ matrix.os }} path: ./dist/joplin_custom_importer* - name: Smoke test - run: ./dist/joplin_custom_importer test_inputs --dry-run + run: | + ./dist/joplin_custom_importer test_inputs --dry-run + ./dist/joplin_custom_importer test_inputs/obsidian_vault --app obsidian --dry-run diff --git a/.gitignore b/.gitignore index 9add11a..d870b44 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,6 @@ __pycache__ *.AppImage joplin_custom_importer.log -joplin_custom_importer.spec .obsidian build dist diff --git a/joplin_custom_importer.spec b/joplin_custom_importer.spec new file mode 100644 index 0000000..b1d3d7c --- /dev/null +++ b/joplin_custom_importer.spec @@ -0,0 +1,58 @@ +# -*- mode: python ; coding: utf-8 -*- +from PyInstaller.utils.hooks import collect_data_files + +# pypandoc: https://github.com/orgs/pyinstaller/discussions/8387 +datas = [] +datas += collect_data_files('pypandoc') + + +# Generate list of hidden imports +# hidden import for dynamically loaded modules "apps.*": +# - https://stackoverflow.com/a/77395744/7410886 +# - https://stackoverflow.com/a/35805418/7410886 +# - https://pyinstaller.org/en/stable/when-things-go-wrong.html#listing-hidden-imports +from pathlib import Path +def list_python_files(folder): + file_list = [] + for file_ in folder.iterdir(): + if file_.suffix == ".py" and file_.name != "__init__.py": + file_list.append(f"{folder.stem}.{file_.stem}") + return file_list + +hiddenimports = list_python_files(Path("src/apps")) + + +a = Analysis( + ['src/joplin_custom_importer.py'], + pathex=[], + binaries=[], + datas=datas, + hiddenimports=hiddenimports, + hookspath=[], + hooksconfig={}, + runtime_hooks=[], + excludes=[], + noarchive=False, +) +pyz = PYZ(a.pure) + +exe = EXE( + pyz, + a.scripts, + a.binaries, + a.datas, + [], + name='joplin_custom_importer', + debug=False, + bootloader_ignore_signals=False, + strip=False, + upx=True, + upx_exclude=[], + runtime_tmpdir=None, + console=True, + disable_windowed_traceback=False, + argv_emulation=False, + target_arch=None, + codesign_identity=None, + entitlements_file=None, +) diff --git a/src/joplin_custom_importer.py b/src/joplin_custom_importer.py index 25898b4..1ce478c 100644 --- a/src/joplin_custom_importer.py +++ b/src/joplin_custom_importer.py @@ -52,9 +52,11 @@ def convert_all_inputs(inputs: list[Path], app: str): # Try to use an app specific converter. If there is none, # fall back to the default converter. try: + LOGGER.debug(f"Try converting with converter {app}") module = importlib.import_module(f"apps.{app}") converter_ = module.Converter(app) except ModuleNotFoundError as exc: + LOGGER.debug(f"Fallback to default converter: {exc}") if str(exc) == f"No module named 'apps.{app}'": converter_ = converter.DefaultConverter(app) else: