Skip to content

Commit 6b87396

Browse files
author
Dilawar Singh
committed
Setup.py works with both python2 and python3.
1 parent ecdc1a8 commit 6b87396

File tree

2 files changed

+37
-36
lines changed

2 files changed

+37
-36
lines changed

CMakeLists.txt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -315,21 +315,13 @@ endif( WITH_BOOST )
315315
# See the cmake doc.
316316
# Till then, stay with the old macro.
317317
set(Python_ADDITIONAL_VERSIONS 2.7)
318-
<<<<<<< HEAD
319318
find_package(PythonInterp 3.5)
320319
if(NOT PYTHONINTERP_FOUND)
321320
find_package(PythonInterp 2.7)
322321
endif()
323322

324323
# This target is built by pymoose/CMakeLists.txt file.
325324
add_subdirectory(pymoose)
326-
=======
327-
find_package(PythonInterp 3.4)
328-
329-
# This target is built by pymoose/CMakeLists.txt file.
330-
add_subdirectory(pymoose)
331-
332-
>>>>>>> bhallalab/master
333325

334326
# always override debian default installation directory. It will be installed in
335327
# site-packages instead of dist-packages.

setup.py

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,53 @@
11
# -*- coding: utf-8 -*-
22
# This script can also be called directly to build and install the pymoose
33
# module.
4+
#
45
# Alternatively, you can use cmake build system which provides finer control
56
# over the build. This script is called by cmake to install the python module.
7+
#
8+
# This script is compatible with python2.7 and python3+. Therefore use of
9+
# super() is commented out.
10+
#
11+
# NOTES:
12+
# * Python2
13+
# - Update setuptools using `python2 -m pip install setuptools --upgrade --user'.
614

715
__author__ = "Dilawar Singh"
816
__copyright__ = "Copyright 2019-, Dilawar Singh"
917
__maintainer__ = "Dilawar Singh"
10-
__email__ = "[email protected]"
18+
__email__ = "[email protected]"
1119

1220
import os
1321
import sys
1422
import subprocess
1523
import datetime
1624

17-
if sys.version_info[0] < 3:
18-
print("[ERROR] You must use python3.5 or higher.\n"
19-
"You used '%s'" % sys.version + " which is not supported.")
20-
quit(-1)
21-
22-
## TEST IF REQUIRED TOOLS EXISTS.
2325
try:
2426
cmakeVersion = subprocess.check_output(["cmake", "--version"])
2527
except Exception as e:
2628
print("[ERROR] cmake is not found. Please install cmake.")
2729
quit(-1)
2830

29-
3031
# See https://docs.python.org/3/library/distutils.html
3132
# setuptools is preferred over distutils. And we are supporting python3 only.
3233
from setuptools import setup, Extension, Command
3334
from setuptools.command.build_ext import build_ext as _build_ext
3435
import subprocess
35-
from pathlib import Path
3636

3737
# Global variables.
38-
39-
sdir_ = Path(__file__).parent.absolute()
38+
sdir_ = os.path.dirname(os.path.realpath(__file__))
4039
stamp = datetime.datetime.now().strftime('%Y%m%d')
41-
builddir_ = sdir_ / ('_build_%s' % stamp)
42-
builddir_.mkdir(parents=True, exist_ok=True)
40+
builddir_ = os.path.join(sdir_, '%s_build_%s' % (sys.version_info[0], stamp))
4341

44-
numCores_ = os.cpu_count() - 1
42+
if not os.path.exists(builddir_):
43+
os.makedirs(builddir_)
44+
45+
numCores_ = 4
46+
try:
47+
# Python3 only.
48+
numCores_ = os.cpu_count() - 1
49+
except Exception:
50+
pass
4551

4652
version_ = '3.2.dev%s' % stamp
4753

@@ -51,8 +57,7 @@
5157
class CMakeExtension(Extension):
5258
def __init__(self, name):
5359
# don't invoke the original build_ext for this special extension
54-
super().__init__(name, sources=[])
55-
60+
Extension.__init__(self, name, sources=[])
5661

5762
class TestCommand(Command):
5863
user_options = []
@@ -65,8 +70,9 @@ def finalize_options(self):
6570

6671
def run(self):
6772
print("[INFO ] Running tests... ")
68-
return subprocess.run(["ctest", "--output-on-failure", '-j2']
69-
, cwd=builddir_)
73+
os.chdir(builddir_)
74+
self.spawn(["ctest", "--output-on-failure", '-j2'])
75+
os.chdir(sdir_)
7076

7177
class build_ext(_build_ext):
7278
user_options = [('with-boost', None, 'Use Boost Libraries (OFF)')
@@ -82,12 +88,14 @@ def initialize_options(self):
8288
self.debug = 0
8389
self.no_build = 0
8490
self.cmake_options = {}
85-
super().initialize_options()
91+
# super().initialize_options()
92+
_build_ext.initialize_options(self)
8693

8794
def finalize_options(self):
8895
# Finalize options.
89-
super().finalize_options()
90-
self.cmake_options['PYTHON_EXECUTABLE'] = Path(sys.executable).resolve()
96+
# super().finalize_options()
97+
_build_ext.finalize_options(self)
98+
self.cmake_options['PYTHON_EXECUTABLE'] = os.path.realpath(sys.executable)
9199
if self.with_boost:
92100
self.cmake_options['WITH_BOOST'] = 'ON'
93101
self.cmake_options['WITH_GSL'] = 'OFF'
@@ -101,7 +109,8 @@ def run(self):
101109
return
102110
for ext in self.extensions:
103111
self.build_cmake(ext)
104-
super().run()
112+
# super().run()
113+
_build_ext.run(self)
105114

106115
def build_cmake(self, ext):
107116
global numCores_
@@ -117,10 +126,9 @@ def build_cmake(self, ext):
117126
self.spawn(['make', '-j%d'%numCores_])
118127
os.chdir(str(sdir_))
119128

120-
with open(Path(__file__).parent / "README.md") as f:
129+
with open(os.path.join(sdir_, "README.md")) as f:
121130
readme = f.read()
122131

123-
124132
setup(
125133
name="pymoose",
126134
version=version_,
@@ -139,13 +147,14 @@ def build_cmake(self, ext):
139147
# python2 specific version here as well.
140148
install_requires=['numpy'],
141149
package_dir={
142-
'moose': sdir_ / 'python' / 'moose',
143-
'rdesigneur': sdir_ / 'python' / 'rdesigneur'
150+
'moose': os.path.join(sdir_, 'python', 'moose'),
151+
'rdesigneur': os.path.join(sdir_, 'python', 'rdesigneur')
144152
},
145153
package_data={
146154
'moose': [
147-
'_moose.so', 'neuroml2/schema/NeuroMLCoreDimensions.xml',
148-
'chemUtil/rainbow2.pkl'
155+
'_moose.so'
156+
, os.path.join('neuroml2','schema','NeuroMLCoreDimensions.xml')
157+
, os.path.join('chemUtil', 'rainbow2.pkl')
149158
]
150159
},
151160
ext_modules=[CMakeExtension('pymoose')],

0 commit comments

Comments
 (0)