1
+ #! /usr/bin/env python3
2
+
1
3
import os
2
4
import re
3
5
import sys
6
8
import subprocess
7
9
8
10
from distutils .version import LooseVersion
9
- from setuptools import setup , Extension
11
+ from setuptools import setup , Extension , find_packages
10
12
from setuptools .command .build_ext import build_ext
11
13
from setuptools .command .test import test as TestCommand
14
+ from shutil import copyfile , copymode
12
15
13
16
14
17
class CMakeExtension (Extension ):
@@ -65,30 +68,29 @@ def build_extension(self, ext):
65
68
cwd = self .build_temp , env = env )
66
69
subprocess .check_call (['cmake' , '--build' , '.' ] + build_args ,
67
70
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 )
68
74
print () # Add an empty line for cleaner output
69
75
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 )
91
88
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 )
92
94
93
95
setup (
94
96
name = 'python_cpp_example' ,
@@ -97,7 +99,10 @@ def run(self):
97
99
author_email = 'benjamin.r.jack@gmail.com' ,
98
100
description = 'A hybrid Python/C++ test project' ,
99
101
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' ,
102
107
zip_safe = False ,
103
108
)
0 commit comments