From 3edca3fbae2a18c1094fe22cbc76fbabc4f60d6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Div=C3=AD?= <43449526+Diviloper@users.noreply.github.com> Date: Sun, 28 Apr 2024 22:31:03 +0200 Subject: [PATCH] Update module structure (#1) * Move builder submodule to independent module. Remove pre-built pymeos_cffi/__init__.py and functions.py from git. Update build process to generate these files on installation. Remove builder module from wheels. * Update build workflow trigger tags * Add back python versions 3.8 and 3.9 for macos-14 * Fix paths in build workflow * Update header path in build_pymeos.py * Remove pycache from source distribution --- .github/workflows/build_pymeos_cffi.yml | 37 +++++++------------ .gitignore | 3 +- MANIFEST.in | 4 +- {pymeos_cffi/builder => builder}/__init__.py | 0 .../builder => builder}/build_header.py | 19 +++++++--- .../builder => builder}/build_pymeos.py | 2 +- .../build_pymeos_functions.py | 13 ++++--- .../build_pymeos_functions_modifiers.py | 0 {pymeos_cffi/builder => builder}/meos.h | 0 {pymeos_cffi/builder => builder}/objects.py | 0 .../templates/functions.py | 0 .../builder => builder}/templates/init.py | 0 pymeos_cffi/functions.py | 8 ++-- setup.py | 10 ++--- 14 files changed, 51 insertions(+), 45 deletions(-) rename {pymeos_cffi/builder => builder}/__init__.py (100%) rename {pymeos_cffi/builder => builder}/build_header.py (86%) rename {pymeos_cffi/builder => builder}/build_pymeos.py (90%) rename {pymeos_cffi/builder => builder}/build_pymeos_functions.py (97%) rename {pymeos_cffi/builder => builder}/build_pymeos_functions_modifiers.py (100%) rename {pymeos_cffi/builder => builder}/meos.h (100%) rename {pymeos_cffi/builder => builder}/objects.py (100%) rename {pymeos_cffi/builder => builder}/templates/functions.py (100%) rename {pymeos_cffi/builder => builder}/templates/init.py (100%) diff --git a/.github/workflows/build_pymeos_cffi.yml b/.github/workflows/build_pymeos_cffi.yml index 1f74323..b0d141a 100644 --- a/.github/workflows/build_pymeos_cffi.yml +++ b/.github/workflows/build_pymeos_cffi.yml @@ -3,7 +3,7 @@ name: Build PyMEOS CFFI on: create: tags: - - "pymeos-cffi-[0-9]+.[0-9]+.[0-9]+*" + - "v[0-9]+.[0-9]+.[0-9]+*" jobs: build_sdist: @@ -25,15 +25,15 @@ jobs: python -m pip install build - name: Build sdist - working-directory: pymeos_cffi run: | python -m build -s ls -l dist - - uses: actions/upload-artifact@v4 + - name: Upload artifacts + uses: actions/upload-artifact@v4 with: name: pymeos_cffi-sdist - path: ./pymeos_cffi/dist/pymeos_cffi-*.tar.gz + path: ./dist/pymeos_cffi-*.tar.gz build_wheels: name: Build PyMEOS CFFI for ${{ matrix.os }} @@ -105,7 +105,6 @@ jobs: echo "LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib64" >> $GITHUB_ENV - name: Build wheels - working-directory: pymeos_cffi run: | export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${{ matrix.ld_prefix }}/lib export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:${{ matrix.ld_prefix }}/lib @@ -135,13 +134,14 @@ jobs: make -j && make install - # Skip tests since they will be thoroughly tested in the next job + # Skip tests since they will be tested in the next job CIBW_TEST_SKIP: "*" - - uses: actions/upload-artifact@v4 + - name: Upload artifacts + uses: actions/upload-artifact@v4 with: name: pymeos_cffi-wheels-${{ matrix.os }} - path: ./pymeos_cffi/wheelhouse/*.whl + path: ./wheelhouse/*.whl test_wheels: name: Test PyMEOS CFFI wheel - Python ${{ matrix.python-version }} on ${{ matrix.os }} @@ -152,15 +152,6 @@ jobs: matrix: python-version: [ "3.8", "3.9", "3.10", "3.11", "3.12" ] os: [ ubuntu-latest, macos-13, macos-14 ] - exclude: - # Necessary due to issue with macOS runners. See - # https://github.com/actions/setup-python/issues/808 - # Can be removed once this PR is merged: - # https://github.com/actions/python-versions/pull/259 - - os: macos-14 - python-version: "3.8" - - os: macos-14 - python-version: "3.9" steps: - name: Checkout @@ -178,21 +169,21 @@ jobs: python-version: ${{ matrix.python-version }} cache: "pip" - - name: Install PyMEOS dependencies + - name: Install PyMEOS-CFFI wheels run: | python -m pip install --upgrade pip pip install -f ./pymeos_cffi_wheels pymeos_cffi - pip install -r pymeos/dev-requirements.txt - - name: Test PyMEOS with pytest - working-directory: pymeos - run: pytest + - name: Run PyMEOS-CFFI check + run: | + python -c "import pymeos_cffi; print(pymeos_cffi.__version__);" + python -c "from pymeos_cffi import *; meos_initialize(None); print(tpoint_out(tgeompoint_in('POINT(2 3)@2000-01-01'), 3)); meos_finalize();" upload_pypi: name: Upload to PyPI needs: [ test_wheels, build_sdist ] runs-on: ubuntu-22.04 - if: github.repository == 'MobilityDB/PyMEOS' + if: github.repository == 'MobilityDB/PyMEOS-CFFI' permissions: id-token: write steps: diff --git a/.gitignore b/.gitignore index a8a6cd3..d3ea605 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,5 @@ dist *.o *.so -test.py \ No newline at end of file +sandbox.py +sandbox.ipynb diff --git a/MANIFEST.in b/MANIFEST.in index 07c7e8b..17b1230 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1 +1,3 @@ -include pymeos_cffi/builder/meos.h \ No newline at end of file +graft builder +global-exclude */__pycache__/* +global-exclude *.pyc \ No newline at end of file diff --git a/pymeos_cffi/builder/__init__.py b/builder/__init__.py similarity index 100% rename from pymeos_cffi/builder/__init__.py rename to builder/__init__.py diff --git a/pymeos_cffi/builder/build_header.py b/builder/build_header.py similarity index 86% rename from pymeos_cffi/builder/build_header.py rename to builder/build_header.py index de092f1..44f55de 100644 --- a/pymeos_cffi/builder/build_header.py +++ b/builder/build_header.py @@ -56,7 +56,7 @@ def remove_if_repeated(m): return content, seen_functions -def main(include_dir, so_path=None, destination_path="pymeos_cffi/builder/meos.h"): +def build_header_file(include_dir, so_path=None, destination_path="builder/meos.h"): files = ["meos.h", "meos_catalog.h", "meos_internal.h"] global_content = "" functions = set() @@ -102,12 +102,21 @@ def main(include_dir, so_path=None, destination_path="pymeos_cffi/builder/meos.h if __name__ == "__main__": if len(sys.argv) > 1: - main(*sys.argv[1:]) + build_header_file(*sys.argv[1:]) else: if sys.platform == "linux": - main("/usr/local/include", "/usr/local/lib/libmeos.so") + build_header_file( + "/usr/local/include", + "/usr/local/lib/libmeos.so", + ) elif sys.platform == "darwin": if platform.processor() == "arm": - main("/opt/homebrew/include", "/opt/homebrew/lib/libmeos.dylib") + build_header_file( + "/opt/homebrew/include", + "/opt/homebrew/lib/libmeos.dylib", + ) else: - main("/usr/local/include", "/usr/local/lib/libmeos.dylib") + build_header_file( + "/usr/local/include", + "/usr/local/lib/libmeos.dylib", + ) diff --git a/pymeos_cffi/builder/build_pymeos.py b/builder/build_pymeos.py similarity index 90% rename from pymeos_cffi/builder/build_pymeos.py rename to builder/build_pymeos.py index 92a2bc3..56f6a72 100644 --- a/pymeos_cffi/builder/build_pymeos.py +++ b/builder/build_pymeos.py @@ -4,7 +4,7 @@ ffibuilder = FFI() -with open("./pymeos_cffi/builder/meos.h", "r") as f: +with open(os.path.join(os.path.dirname(__file__), "meos.h"), "r") as f: content = f.read() ffibuilder.cdef(content) diff --git a/pymeos_cffi/builder/build_pymeos_functions.py b/builder/build_pymeos_functions.py similarity index 97% rename from pymeos_cffi/builder/build_pymeos_functions.py rename to builder/build_pymeos_functions.py index 0915f0a..c9b64f1 100644 --- a/pymeos_cffi/builder/build_pymeos_functions.py +++ b/builder/build_pymeos_functions.py @@ -230,7 +230,7 @@ def check_modifiers(functions: List[str]) -> None: ) -def main(header_path="pymeos_cffi/builder/meos.h"): +def build_pymeos_functions(header_path="builder/meos.h"): with open(header_path) as f: content = f.read() # Regex lines: @@ -252,14 +252,15 @@ def main(header_path="pymeos_cffi/builder/meos.h"): f_regex, "".join(content.splitlines()), flags=re.RegexFlag.MULTILINE ) - template_path = os.path.join(os.path.dirname(__file__), "templates/functions.py") - init_template_path = os.path.join(os.path.dirname(__file__), "templates/init.py") + file_path = os.path.dirname(__file__) + template_path = os.path.join(file_path, "templates/functions.py") + init_template_path = os.path.join(file_path, "templates/init.py") with open(template_path) as f, open(init_template_path) as i: base = f.read() init_text = i.read() - functions_path = os.path.join(os.path.dirname(__file__), "../functions.py") - init_path = os.path.join(os.path.dirname(__file__), "../__init__.py") + functions_path = os.path.join(file_path, "../pymeos_cffi/functions.py") + init_path = os.path.join(file_path, "../pymeos_cffi/__init__.py") with open(functions_path, "w+") as file: file.write(base) @@ -544,4 +545,4 @@ def build_function_string( if __name__ == "__main__": - main(*sys.argv[1:]) + build_pymeos_functions(*sys.argv[1:]) diff --git a/pymeos_cffi/builder/build_pymeos_functions_modifiers.py b/builder/build_pymeos_functions_modifiers.py similarity index 100% rename from pymeos_cffi/builder/build_pymeos_functions_modifiers.py rename to builder/build_pymeos_functions_modifiers.py diff --git a/pymeos_cffi/builder/meos.h b/builder/meos.h similarity index 100% rename from pymeos_cffi/builder/meos.h rename to builder/meos.h diff --git a/pymeos_cffi/builder/objects.py b/builder/objects.py similarity index 100% rename from pymeos_cffi/builder/objects.py rename to builder/objects.py diff --git a/pymeos_cffi/builder/templates/functions.py b/builder/templates/functions.py similarity index 100% rename from pymeos_cffi/builder/templates/functions.py rename to builder/templates/functions.py diff --git a/pymeos_cffi/builder/templates/init.py b/builder/templates/init.py similarity index 100% rename from pymeos_cffi/builder/templates/init.py rename to builder/templates/init.py diff --git a/pymeos_cffi/functions.py b/pymeos_cffi/functions.py index 4151409..ddd39e2 100644 --- a/pymeos_cffi/functions.py +++ b/pymeos_cffi/functions.py @@ -205,11 +205,13 @@ def meos_get_intervalstyle() -> str: def meos_initialize(tz_str: "Optional[str]") -> None: + if "PROJ_DATA" not in os.environ and "PROJ_LIB" not in os.environ: - # Assume we are in a wheel and the PROJ data is in the package proj_dir = os.path.join(os.path.dirname(__file__), "proj_data") - os.environ["PROJ_DATA"] = proj_dir - os.environ["PROJ_LIB"] = proj_dir + if os.path.exists(proj_dir): + # Assume we are in a wheel and the PROJ data is in the package + os.environ["PROJ_DATA"] = proj_dir + os.environ["PROJ_LIB"] = proj_dir tz_str_converted = tz_str.encode("utf-8") if tz_str is not None else _ffi.NULL _lib.meos_initialize(tz_str_converted, _lib.py_error_handler) diff --git a/setup.py b/setup.py index 7681da2..58e1c6a 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ from setuptools import setup - +# Copy PROJ data to package data package_data = [] # Conditionally copy PROJ DATA to make self-contained wheels @@ -28,10 +28,10 @@ else: print("Not copying PROJ data to package data") + setup( - packages=["pymeos_cffi", "pymeos_cffi.builder"], - setup_requires=["cffi"], - include_package_data=True, + packages=["pymeos_cffi"], package_data={"pymeos_cffi": package_data}, - cffi_modules=["pymeos_cffi/builder/build_pymeos.py:ffibuilder"], + setup_requires=["cffi"], + cffi_modules=["builder/build_pymeos.py:ffibuilder"], )