From ac213ea7b73156d61a1817093313a8a2fc080798 Mon Sep 17 00:00:00 2001 From: Timmy Welch Date: Sun, 26 Nov 2023 13:14:59 -0800 Subject: [PATCH] Add a PyInstaller hook and a test to verify it works --- isocodes/__pyinstaller/__init__.py | 5 +++ isocodes/__pyinstaller/hook-isocodes.py | 3 ++ isocodes/__pyinstaller/test_pyinstaller.py | 36 ++++++++++++++++++++++ pyproject.toml | 6 +++- requirements/test-requirements.txt | 21 ++++++++++--- tox.ini | 1 - 6 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 isocodes/__pyinstaller/__init__.py create mode 100644 isocodes/__pyinstaller/hook-isocodes.py create mode 100644 isocodes/__pyinstaller/test_pyinstaller.py diff --git a/isocodes/__pyinstaller/__init__.py b/isocodes/__pyinstaller/__init__.py new file mode 100644 index 00000000..1c52aadf --- /dev/null +++ b/isocodes/__pyinstaller/__init__.py @@ -0,0 +1,5 @@ +import os + + +def get_hook_dirs(): + return [os.path.dirname(__file__)] diff --git a/isocodes/__pyinstaller/hook-isocodes.py b/isocodes/__pyinstaller/hook-isocodes.py new file mode 100644 index 00000000..4e585f65 --- /dev/null +++ b/isocodes/__pyinstaller/hook-isocodes.py @@ -0,0 +1,3 @@ +from PyInstaller.utils.hooks import collect_data_files + +datas = collect_data_files("isocodes") diff --git a/isocodes/__pyinstaller/test_pyinstaller.py b/isocodes/__pyinstaller/test_pyinstaller.py new file mode 100644 index 00000000..cf981208 --- /dev/null +++ b/isocodes/__pyinstaller/test_pyinstaller.py @@ -0,0 +1,36 @@ +import subprocess + +from PyInstaller import __main__ as pyi_main +import textwrap +import pathlib + + +# Tests +# ===== +# Test out the package by importing it, then running functions from it. +def test_pyi_isocodes(tmp_path: pathlib.Path) -> None: + app_name = "isocodes_pyinstaller" + workpath = tmp_path / "build" + distpath = tmp_path / "dist" + app = tmp_path / (f"{app_name}.py") + + app.write_text( + textwrap.dedent( + """ + import isocodes + assert isocodes.languages.get(name='Spanish') + """ + ) + ) + args = [ + # Place all generated files in ``tmp_path``. + "--workpath", + str(workpath), + "--distpath", + str(distpath), + "--specpath", + str(tmp_path), + str(app), + ] + pyi_main.run(args) + subprocess.run([str(distpath / app_name / app_name)], check=True) diff --git a/pyproject.toml b/pyproject.toml index c7a70edd..974f3100 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,6 +28,10 @@ dev = ["pre-commit == 3.5.0", "ruff == 0.1.6", "mypy == 1.7.0"] doc = ["mkdocs == 1.5.3", "mkdocs-material == 9.4.11", "mkdocstrings == 0.23.0"] -test = ["pytest == 7.4.3", "tox == 4.11.3"] +test = ["pytest == 7.4.3", "tox == 4.11.3", "pyinstaller[hook_testing] == 6.2.0", "psutil == 5.9.6"] build = ["hatch == 1.7.0"] + +[project.entry-points.pyinstaller40] +hook-dirs = "isocodes.__pyinstaller:get_hook_dirs" +tests = "isocodes.__pyinstaller:get_hook_dirs" diff --git a/requirements/test-requirements.txt b/requirements/test-requirements.txt index 859c3da8..7c2724ab 100644 --- a/requirements/test-requirements.txt +++ b/requirements/test-requirements.txt @@ -2,10 +2,12 @@ # This file is autogenerated by pip-compile with Python 3.10 # by the following command: # -# pip-compile --extra=test --output-file=requirements/test-requirements.txt --resolver=backtracking pyproject.toml +# pip-compile --extra=test --output-file=requirements/test-requirements.txt pyproject.toml # -attrs==23.1.0 - # via pytest +altgraph==0.17.4 + # via + # macholib + # pyinstaller cachetools==5.3.1 # via tox chardet==5.2.0 @@ -14,7 +16,7 @@ colorama==0.4.6 # via tox distlib==0.3.7 # via virtualenv -exceptiongroup==1.1.3 +exceptiongroup==1.2.0 # via pytest filelock==3.12.4 # via @@ -22,8 +24,11 @@ filelock==3.12.4 # virtualenv iniconfig==2.0.0 # via pytest +macholib==1.16.3 + # via pyinstaller packaging==23.2 # via + # pyinstaller # pyproject-api # pytest # tox @@ -35,6 +40,14 @@ pluggy==1.3.0 # via # pytest # tox +psutil==5.9.6 + # via isocodes (pyproject.toml) +pyinstaller[hook-testing]==6.2.0 + # via + # isocodes (pyproject.toml) + # pyinstaller +pyinstaller-hooks-contrib==2023.10 + # via pyinstaller pyproject-api==1.6.1 # via tox pytest==7.4.3 diff --git a/tox.ini b/tox.ini index e429f87c..9eb34c8b 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,4 @@ [tox] -skipsdist = true envlist = py38, py39, py310, py311, py312 [testenv]