From 719a286f2d3749e4934dea7000c276735f46d393 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20Jonglez?= <clement@jonglez.space>
Date: Mon, 8 Apr 2024 17:22:04 +0200
Subject: [PATCH] add hook to make orekit_jpype pyinstallable

Heavily inspired from https://github.com/jpype-project/jpype/pull/877
---
 orekit_jpype/_pyinstaller/entry_points.py     | 16 +++++++++++
 .../_pyinstaller/hook-orekit_jpype.py         | 10 +++++++
 .../_pyinstaller/orekit_data_load_example.py  |  8 ++++++
 .../test_orekit_jpype_pyinstaller.py          | 27 +++++++++++++++++++
 pyproject.toml                                |  8 +++++-
 5 files changed, 68 insertions(+), 1 deletion(-)
 create mode 100644 orekit_jpype/_pyinstaller/entry_points.py
 create mode 100644 orekit_jpype/_pyinstaller/hook-orekit_jpype.py
 create mode 100644 orekit_jpype/_pyinstaller/orekit_data_load_example.py
 create mode 100644 orekit_jpype/_pyinstaller/test_orekit_jpype_pyinstaller.py

diff --git a/orekit_jpype/_pyinstaller/entry_points.py b/orekit_jpype/_pyinstaller/entry_points.py
new file mode 100644
index 0000000..aae4219
--- /dev/null
+++ b/orekit_jpype/_pyinstaller/entry_points.py
@@ -0,0 +1,16 @@
+import os
+from pathlib import Path
+
+
+fspath = getattr(os, 'fspath', str)
+
+
+_pyinstaller_path = Path(__file__).parent
+
+
+def get_hook_dirs():
+    return [fspath(str(_pyinstaller_path))]
+
+
+def get_PyInstaller_tests():
+    return [fspath(_pyinstaller_path)]
diff --git a/orekit_jpype/_pyinstaller/hook-orekit_jpype.py b/orekit_jpype/_pyinstaller/hook-orekit_jpype.py
new file mode 100644
index 0000000..bb26057
--- /dev/null
+++ b/orekit_jpype/_pyinstaller/hook-orekit_jpype.py
@@ -0,0 +1,10 @@
+import os
+from pathlib import Path
+
+import orekit_jpype
+
+fspath = getattr(os, 'fspath', str)
+
+jar_path_glob = Path(orekit_jpype.__file__).parent.joinpath("jars", "*.jar")
+
+datas = [[fspath(jar_path_glob), os.path.join("orekit_jpype", "jars")]]
diff --git a/orekit_jpype/_pyinstaller/orekit_data_load_example.py b/orekit_jpype/_pyinstaller/orekit_data_load_example.py
new file mode 100644
index 0000000..d5bda9c
--- /dev/null
+++ b/orekit_jpype/_pyinstaller/orekit_data_load_example.py
@@ -0,0 +1,8 @@
+import orekit_jpype as orekit
+orekit.initVM()
+
+from orekit_jpype.pyhelpers import setup_orekit_data
+setup_orekit_data(from_pip_library=True)
+
+from org.orekit.utils import Constants
+assert(Constants.WGS84_EARTH_EQUATORIAL_RADIUS == 6378137.0)
diff --git a/orekit_jpype/_pyinstaller/test_orekit_jpype_pyinstaller.py b/orekit_jpype/_pyinstaller/test_orekit_jpype_pyinstaller.py
new file mode 100644
index 0000000..e4fd310
--- /dev/null
+++ b/orekit_jpype/_pyinstaller/test_orekit_jpype_pyinstaller.py
@@ -0,0 +1,27 @@
+import os
+from pathlib import Path
+from subprocess import run
+
+import PyInstaller.__main__
+
+
+fspath = getattr(os, 'fspath', str)
+
+
+test_file = Path(__file__).parent.joinpath('orekit_data_load_example.py')
+
+
+def test_start_and_stop(tmp_path):
+    name = 'orekit_jpype_test_app'
+    dist = tmp_path.joinpath('dist')
+    work = tmp_path.joinpath('build')
+
+    PyInstaller.__main__.run([
+        '--name', name,
+        '--distpath', fspath(dist),
+        '--workpath', fspath(work),
+        '--collect-data', 'orekitdata',
+        fspath(test_file)
+    ])
+
+    run([str(dist / name / name)], check=True)
diff --git a/pyproject.toml b/pyproject.toml
index fa32798..7348e31 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -40,5 +40,11 @@ tests = [
 ]
 
 [pytest]
-testpaths = "test"
+testpaths = [
+    "test",
+    "orekit_jpype/_pyinstaller"
+]
 
+[project.entry-points.pyinstaller40]
+"hook-dirs" = "orekit_jpype._pyinstaller.entry_points:get_hook_dirs"
+"tests" = "orekit_jpype._pyinstaller.entry_points:get_PyInstaller_tests"
-- 
GitLab