Skip to content

Commit

Permalink
Update module structure (#1)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Diviloper authored Apr 28, 2024
1 parent e0bb8eb commit 3edca3f
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 45 deletions.
37 changes: 14 additions & 23 deletions .github/workflows/build_pymeos_cffi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 }}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 }}
Expand All @@ -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
Expand All @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ dist
*.o
*.so

test.py
sandbox.py
sandbox.ipynb
4 changes: 3 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
include pymeos_cffi/builder/meos.h
graft builder
global-exclude */__pycache__/*
global-exclude *.pyc
File renamed without changes.
19 changes: 14 additions & 5 deletions pymeos_cffi/builder/build_header.py → builder/build_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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",
)
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -544,4 +545,4 @@ def build_function_string(


if __name__ == "__main__":
main(*sys.argv[1:])
build_pymeos_functions(*sys.argv[1:])
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 5 additions & 3 deletions pymeos_cffi/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"],
)

0 comments on commit 3edca3f

Please sign in to comment.