Skip to content

Commit

Permalink
#16: bindings: Switch to modern python package build method and clean…
Browse files Browse the repository at this point in the history
… up usage
  • Loading branch information
pierrepebay committed Nov 23, 2023
1 parent 7484892 commit eea2cc7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 68 deletions.
33 changes: 0 additions & 33 deletions pyproject.toml

This file was deleted.

38 changes: 22 additions & 16 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import os
import sys

# Ensure python-build directory exists
build_dir = 'python-build'
os.makedirs(build_dir, exist_ok=True)

class CMakeExtension(Extension):
def __init__(self, name, sourcedir=''):
Extension.__init__(self, name, sources=[])
Expand All @@ -14,39 +18,41 @@ def run(self):
try:
out = subprocess.check_output(['cmake', '--version'])
except OSError:
raise RuntimeError('CMake must be installed to build the following extensions: ' +
', '.join(e.name for e in self.extensions))
raise RuntimeError("CMake must be installed to build the following extensions: " +
", ".join(e.name for e in self.extensions))

for ext in self.extensions:
self.build_extension(ext)

def build_extension(self, ext):
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))

# Create a temporary build directory
build_temp = os.path.join('python-build', 'build', 'temp')
os.makedirs(build_temp, exist_ok=True)

vtk_dir = os.environ.get('VTK_DIR')
if not vtk_dir:
raise RuntimeError("Environment variable VTK_DIR is required")

jobs = os.environ.get('JOBS', os.cpu_count())

cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir,
'-DPYTHON_EXECUTABLE=' + sys.executable,
'-DVTK_DIR:PATH=~/Develop/vtk-build',
'-DCMAKE_OSX_DEPLOYMENT_TARGET:STRING=13.5']
'-DVTK_DIR:PATH=' + vtk_dir]

if sys.platform == "darwin":
import platform
macos_version = platform.mac_ver()[0]
cmake_args.append('-DCMAKE_OSX_DEPLOYMENT_TARGET:STRING=' + macos_version)

cfg = 'Debug' if self.debug else 'Release'
build_args = ['--config', cfg]

# Change to the build directory
os.chdir(build_temp)

# Run CMake and build commands
self.spawn(['cmake', ext.sourcedir] + cmake_args)
if not self.dry_run:
self.spawn(['cmake', '--build', '.', '--parallel', '-j8'] + build_args)

# Change back to the original directory
self.spawn(['cmake', '--build', '.', '--parallel', '-j' + str(jobs)] + build_args)
os.chdir(ext.sourcedir)


setup(
name='vttv',
version='0.0.1',
Expand All @@ -57,7 +63,7 @@ def build_extension(self, ext):
long_description='',
ext_modules=[CMakeExtension('vttv', sourcedir='.')],
cmdclass=dict(build_ext=CMakeBuild),
package_dir={'': 'python-build'},
packages=find_packages('python-build'),
package_dir={'': build_dir},
packages=find_packages(build_dir),
zip_safe=False,
)
)
24 changes: 24 additions & 0 deletions tests/testvttv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import json
import yaml
import vttv

import sys
import os

# source dir is the directory a level above this file
source_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

with open(f'{source_dir}/tests/unit/lb_test_data/data.0.json', 'r') as f:
data = json.load(f)

with open(f'{source_dir}/config/conf.yaml', 'r') as stream:
try:
params = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)


data_serialized = json.dumps(data)
params_serialized = yaml.dump(params)

vttv.tv_from_json(data_serialized, params_serialized)
19 changes: 0 additions & 19 deletions testvttv.py

This file was deleted.

0 comments on commit eea2cc7

Please sign in to comment.