1+ #! /usr/bin/env python3
2+
13import os
24import re
35import sys
68import subprocess
79
810from distutils .version import LooseVersion
9- from setuptools import setup , Extension
11+ from setuptools import setup , Extension , find_packages
1012from setuptools .command .build_ext import build_ext
1113from setuptools .command .test import test as TestCommand
14+ from shutil import copyfile , copymode
1215
1316
1417class CMakeExtension (Extension ):
@@ -65,30 +68,29 @@ def build_extension(self, ext):
6568 cwd = self .build_temp , env = env )
6669 subprocess .check_call (['cmake' , '--build' , '.' ] + build_args ,
6770 cwd = self .build_temp )
71+ # Copy *_test file to tests directory
72+ test_bin = os .path .join (self .build_temp , 'python_cpp_example_test' )
73+ self .copy_test_file (test_bin )
6874 print () # Add an empty line for cleaner output
6975
70- class CatchTestCommand (TestCommand ):
71- """
72- A custom test runner to execute both python unittest tests and C++ Catch-
73- lib tests.
74- """
75- def distutils_dir_name (self , dname ):
76- """Returns the name of a distutils build directory"""
77- dir_name = "{dirname}.{platform}-{version[0]}.{version[1]}"
78- return dir_name .format (dirname = dname ,
79- platform = sysconfig .get_platform (),
80- version = sys .version_info )
81-
82- def run (self ):
83- # Run python tests
84- super (CatchTestCommand , self ).run ()
85- print ("\n Python tests complete, now running C++ tests...\n " )
86- # Run catch tests
87- subprocess .call (['./*_test' ],
88- cwd = os .path .join ('build' ,
89- self .distutils_dir_name ('temp' )),
90- shell = True )
76+ def copy_test_file (self , src_file ):
77+ '''
78+ Copy ``src_file`` to ``dest_file`` ensuring parent directory exists.
79+ By default, message like `creating directory /path/to/package` and
80+ `copying directory /src/path/to/package -> path/to/package` are displayed on standard output. Adapted from scikit-build.
81+ '''
82+ # Create directory if needed
83+ dest_dir = os .path .join (os .path .dirname (
84+ os .path .abspath (__file__ )), 'tests' , 'bin' )
85+ if dest_dir != "" and not os .path .exists (dest_dir ):
86+ print ("creating directory {}" .format (dest_dir ))
87+ os .makedirs (dest_dir )
9188
89+ # Copy file
90+ dest_file = os .path .join (dest_dir , os .path .basename (src_file ))
91+ print ("copying {} -> {}" .format (src_file , dest_file ))
92+ copyfile (src_file , dest_file )
93+ copymode (src_file , dest_file )
9294
9395setup (
9496 name = 'python_cpp_example' ,
@@ -97,7 +99,10 @@ def run(self):
9799 author_email = 'benjamin.r.jack@gmail.com' ,
98100 description = 'A hybrid Python/C++ test project' ,
99101 long_description = '' ,
100- ext_modules = [CMakeExtension ('python_cpp_example' )],
101- cmdclass = dict (build_ext = CMakeBuild , test = CatchTestCommand ),
102+ packages = find_packages ('src' ),
103+ package_dir = {'' :'src' },
104+ ext_modules = [CMakeExtension ('python_cpp_example/python_cpp_example' )],
105+ cmdclass = dict (build_ext = CMakeBuild ),
106+ test_suite = 'tests' ,
102107 zip_safe = False ,
103108)
0 commit comments