9.5.3 : The setup.py file

Now, let's write the setup.py file :

First, we have to import tools to make the installation of our module :
1
2
3
4
5
6
7
8
9
from setuptools import setup
from setuptools import Extension
from setuptools import find_packages
import sys
import os
from platform import system
import subprocess

import numpy as np
Check is Cython package is installed. The Cython package makes the python extensions for wrappers.
1
2
3
4
5
6
7
8
try:
	from Cython.Distutils import build_ext
except ImportError:
	use_cython = False
	print("Cython not found")
	raise Exception('Please install Cython on your system')
else:
	use_cython = True
Some informations to be sure that we will not lose them is the file size increases :
1
2
3
4
5
6
7
NAME = 'barycentrepython'
VERSION = '1.0.0'
AUTHOR = 'Asterics developers'
AUTHOR_EMAIL = 'pierre.aubert@lapp.in2p3.fr'
URL = ''
DESCRIPTION = 'Asterics HPC barycentre python module'
LICENSE = 'Cecil-C'
Some C++ flags correction, because the python expects C sources and not C++ ones :
1
2
3
4
5
6
7
8
# Remove the "-Wstrict-prototypes" compiler option, which isn't valid for C++.
import distutils.sysconfig
cfg_vars = distutils.sysconfig.get_config_vars()
for key, value in cfg_vars.items():
	if type(value) == str:
		value = value.replace("-Wstrict-prototypes", "")
		value = value.replace("-DNDEBUG", "")
		cfg_vars[key] = value
Since, we are compiling a function which has to be fast, we have to add extra compilation arguments such as in the C++ version :
1
extra_compile_args = ['-Werror', '-march=native',  '-mtune=native', '-ftree-vectorize', '-mavx2', '-O3', '-DVECTOR_ALIGNEMENT=32', '-g']
Now, we create the extension with our file sources and some cmake macro to handle the files positions :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
packageName = 'barycentrepython'
ext_modules = [
	Extension(packageName, ['@CMAKE_CURRENT_SOURCE_DIR@/barycentrepython.cpp',
				'@CMAKE_CURRENT_SOURCE_DIR@/../barycentre_intrinsics.cpp',
				'@CMAKE_CURRENT_SOURCE_DIR@/barycentreWrapper.cpp'
	],
	libraries=[],
	library_dirs=[],
	runtime_library_dirs=[],
	extra_link_args=[],
	extra_compile_args=extra_compile_args,

	include_dirs=['@CMAKE_CURRENT_SOURCE_DIR@/',
			'@CMAKE_CURRENT_SOURCE_DIR@/../',
			np.get_include()]
	)
]
Finally, we create the module and cacth exeptions if it failed :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
try:
	setup(name = NAME,
		version=VERSION,
		ext_modules=ext_modules,
		description=DESCRIPTION,
		install_requires=['numpy', 'cython'],
		author=AUTHOR,
		author_email=AUTHOR_EMAIL,
		license=LICENSE,
		url=URL,
		classifiers=[
			'Intended Audience :: Science/Research',
			'License :: OSI Approved ::Cecil-C',
			'Operating System :: OS Independent',
			'Programming Language :: Python :: 3',
			'Topic :: Scientific/Engineering :: Astronomy',
			'Development Status :: 3 - Alpha'],
	)
except Exception as e:
	print(str(e))
	sys.exit(-1)
The full setup.py file :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'''
	Auteur : Pierre Aubert
	Mail : aubertp7@gmail.com
	Licence : CeCILL-C
'''

from setuptools import setup
from setuptools import Extension
from setuptools import find_packages
import sys
import os
from platform import system
import subprocess

import numpy as np

try:
	from Cython.Distutils import build_ext
except ImportError:
	use_cython = False
	print("Cython not found")
	raise Exception('Please install Cython on your system')
else:
	use_cython = True

NAME = 'barycentrepython'
VERSION = '1.0.0'
AUTHOR = 'Asterics developers'
AUTHOR_EMAIL = 'pierre.aubert@lapp.in2p3.fr'
URL = ''
DESCRIPTION = 'Asterics HPC barycentre python module'
LICENSE = 'Cecil-C'

# Remove the "-Wstrict-prototypes" compiler option, which isn't valid for C++.
import distutils.sysconfig
cfg_vars = distutils.sysconfig.get_config_vars()
for key, value in cfg_vars.items():
	if type(value) == str:
		value = value.replace("-Wstrict-prototypes", "")
		value = value.replace("-DNDEBUG", "")
		cfg_vars[key] = value

extra_compile_args = ['-Werror', '-march=native',  '-mtune=native', '-ftree-vectorize', '-mavx2', '-O3', '-DVECTOR_ALIGNEMENT=32', '-g']

packageName = 'barycentrepython'
ext_modules = [
	Extension(packageName, ['@CMAKE_CURRENT_SOURCE_DIR@/barycentrepython.cpp',
				'@CMAKE_CURRENT_SOURCE_DIR@/../barycentre_intrinsics.cpp',
				'@CMAKE_CURRENT_SOURCE_DIR@/barycentreWrapper.cpp'
	],
	libraries=[],
	library_dirs=[],
	runtime_library_dirs=[],
	extra_link_args=[],
	extra_compile_args=extra_compile_args,

	include_dirs=['@CMAKE_CURRENT_SOURCE_DIR@/',
			'@CMAKE_CURRENT_SOURCE_DIR@/../',
			np.get_include()]
	)
]

try:
	setup(name = NAME,
		version=VERSION,
		ext_modules=ext_modules,
		description=DESCRIPTION,
		install_requires=['numpy', 'cython'],
		author=AUTHOR,
		author_email=AUTHOR_EMAIL,
		license=LICENSE,
		url=URL,
		classifiers=[
			'Intended Audience :: Science/Research',
			'License :: OSI Approved ::Cecil-C',
			'Operating System :: OS Independent',
			'Programming Language :: Python :: 3',
			'Topic :: Scientific/Engineering :: Astronomy',
			'Development Status :: 3 - Alpha'],
	)
except Exception as e:
	print(str(e))
	sys.exit(-1)
You can download it here.