Skip to content

Commit 9af1c31

Browse files
committed
restructured package
1 parent 566e223 commit 9af1c31

File tree

10 files changed

+58
-31
lines changed

10 files changed

+58
-31
lines changed

.eggs/README.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
This directory contains eggs that were downloaded by setuptools to build, test, and run plug-ins.
2+
3+
This directory caches those eggs to prevent repeated downloads.
4+
5+
However, it is safe to delete this directory.
6+

.eggs/tests-0.7-py3.6.egg

1.37 KB
Binary file not shown.

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 2.8.12)
22
project(python_cpp_example)
33

4-
SET(SOURCE_DIR "python_cpp_example")
4+
SET(SOURCE_DIR "src/python_cpp_example")
55
# Tell cmake that headers are in alse in source_dir
66
include_directories(${SOURCE_DIR})
77
SET(SOURCES "${SOURCE_DIR}/math.cpp")

setup.py

100644100755
+29-24
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#! /usr/bin/env python3
2+
13
import os
24
import re
35
import sys
@@ -6,9 +8,10 @@
68
import subprocess
79

810
from distutils.version import LooseVersion
9-
from setuptools import setup, Extension
11+
from setuptools import setup, Extension, find_packages
1012
from setuptools.command.build_ext import build_ext
1113
from setuptools.command.test import test as TestCommand
14+
from shutil import copyfile, copymode
1215

1316

1417
class 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("\nPython 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

9395
setup(
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
)

src/python_cpp_example/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .python_cpp_example import *

python_cpp_example/bindings.cpp renamed to src/python_cpp_example/bindings.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33

44
namespace py = pybind11;
55

6-
PYBIND11_PLUGIN(python_cpp_example)
7-
{
8-
py::module m("python_cpp_example", R"doc(
6+
PYBIND11_PLUGIN(python_cpp_example) {
7+
py::module m("python_cpp_example", R"doc(
98
Python module
109
-----------------------
1110
.. currentmodule:: python_cpp_example
@@ -16,17 +15,17 @@ PYBIND11_PLUGIN(python_cpp_example)
1615
subtract
1716
)doc");
1817

19-
m.def("add", &add, R"doc(
18+
m.def("add", &add, R"doc(
2019
Add two numbers
2120
2221
Some other information about the add function.
2322
)doc");
2423

25-
m.def("subtract", &subtract, R"doc(
24+
m.def("subtract", &subtract, R"doc(
2625
Subtract two numbers
2726
2827
Some other information about the subtract function.
2928
)doc");
3029

31-
return m.ptr();
30+
return m.ptr();
3231
}
File renamed without changes.
File renamed without changes.

tests/bin/python_cpp_example_test

489 KB
Binary file not shown.

tests/cpp_test.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import unittest
2+
import python_cpp_example
3+
import subprocess
4+
import os
5+
6+
7+
class MainTest(unittest.TestCase):
8+
def test_cpp(self):
9+
print("\n\nTesting C++ code...")
10+
subprocess.check_call(os.path.join(os.path.dirname(
11+
os.path.relpath(__file__)), 'bin', 'python_cpp_example_test'))
12+
print() # for prettier output
13+
14+
15+
if __name__ == '__main__':
16+
unittest.main()

0 commit comments

Comments
 (0)