Skip to content

Commit

Permalink
Updated setup.py to use distutils. Now works with pip.
Browse files Browse the repository at this point in the history
  • Loading branch information
connorourke committed Aug 1, 2018
1 parent b3793e8 commit fccea0a
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 68 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ crystal_torture/ENV
tests/STRUCTURE_FILES/UNUSED
*coverage
crystal_torture.egg-info*
build*
dist*
MANIFEST.in
crystal_torture-*
build
MANIFEST
README.rst
README.txt
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ or download directly from [GitHub](http://github.com/connorourke/crystal_torture

```
cd crystal_torture
pip install -r requirements.txt
python setup.py install
```

Expand Down
2 changes: 1 addition & 1 deletion crystal_torture/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.15
1.0.40
1 change: 0 additions & 1 deletion docs/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
build
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ ddt==1.2.0
decorator==4.3.0
docutils==0.14
entrypoints==0.2.3
numpy==1.14.5
f90wrap==0.1.4
html5lib==1.0.1
idna==2.7
Expand All @@ -35,9 +34,10 @@ mpmath==1.0.0
nbconvert==5.3.1
nbformat==4.4.0
notebook==5.5.0
numpy==1.14.5
packaging==17.1
palettable==3.1.1
pandas==0.23.1
pandas==0.20.1
pandocfilters==1.4.2
parso==0.3.1
pexpect==4.6.0
Expand Down
178 changes: 115 additions & 63 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,81 +1,135 @@
def return_major_minor_python():

import subprocess
from distutils.core import setup, Command
import distutils.log
import os
import sys
from setuptools.command.install import install
from setuptools.command.develop import develop
from setuptools.command.egg_info import egg_info
import sys

return str(sys.version_info[0])+"."+str(sys.version_info[1])

def check_compiler_gnu():

result = subprocess.check_output('gfortran --version | grep GNU',shell=True)
return('GNU' in str(result))
def return_include_dir():
from distutils.util import get_platform
return get_platform()+'-'+return_major_minor_python()


def setup_tort_ext(args,parent_package='',top_path=''):
from numpy.distutils.misc_util import Configuration
from os.path import join
import sys

def custom_command():
"""Run command to compile & wrap"""
config = Configuration('',parent_package,top_path)
tort_src = [join('crystal_torture/','tort.f90')]

if check_compiler_gnu():
command = ['f2py -c --opt=\'-O3\' --f90flags=\'-fopenmp\' -lgomp -m dist dist.f90']
config.add_library('_tort', sources=tort_src,
extra_f90_compile_args = [ args["compile_args"]],
extra_link_args=[args["link_args"]])

sources = [join('crystal_torture','f90wrap_tort.f90')]

command1 = ['gfortran -c -O3 -fPIC tort.f90']
command2 = ['f2py-f90wrap -c --opt=\'-O3\' --f90flags=\'-fopenmp\' -lgomp -m _tort f90wrap_tort.f90 tort.o']

top_dir = os.getcwd()
src_dir = top_dir + '/crystal_torture'
os.chdir(src_dir)
subprocess.check_call(command1, shell=True)
subprocess.check_call(command2,shell=True)
subprocess.check_call(command,shell=True)
os.chdir(top_dir)
else:
sys.exit("Error: f2py is using a compiler other than gfortran. Please install gfortran.")
config.add_extension(name='_tort',
sources=sources,
extra_f90_compile_args = [ args["compile_args"]],
extra_link_args=[args["link_args"]],
libraries=['_tort'],
include_dirs=['build/temp.' + return_include_dir()])

class CustomInstallCommand(install):
def run(self):
install.run(self)
custom_command()
dist_src = [join('crystal_torture/','dist.f90')]
config.add_extension(name='dist',
sources=dist_src,
extra_f90_compile_args = [ args["compile_args"]],
extra_link_args=[args["link_args"]])

class CustomDevelopCommand(develop):
def run(self):
develop.run(self)
custom_command()

class CustomEggInfoCommand(egg_info):
def run(self):
egg_info.run(self)
custom_command()
return config


try:
from setuptools import setup
except ImportError:
from distutils.core import setup
def check_compiler_gnu():

result = subprocess.check_output('gfortran --version | grep GNU',shell=True)
return('GNU' in str(result))

def check_f2py_compiler():

result = subprocess.check_output('f2py -c --help-fcompiler | grep -A 1 \'Fortran compilers found\' ',shell=True)

if not check_compiler_gnu():
print(' GNU compiler not installed. Checking f2py comompiler - this is UNTESTED' )
print(' Speculatively setting flags - if compile fails, or OpenMP doesn\'t work install gfortran and retry')


if 'GNU' in str(result):
print('Found gnu compiler. Setting OpenMP flag to \'-fopenmp\'')
compile_args = '-fopenmp -lgomp -O3'
link_args = '-lgomp'
elif 'Intel' in str(result):
print('Found intel compiler. Setting OpenMP flag to \'-openmp\'')
compile_args = '-openmp -O3'
link_args = ''
elif 'Portland' in str(result):
print('Found portland compiler. Setting OpenMP flag to \'-mp\'')
compile_args = '-mp -O3'
link_args = ''
elif 'NAG' in str(result):
print('Found NAG compiler. Setting OpenMP flag to \'-openmp\'')
compile_args = '-openmp -O3'
link_args = ''
else:
print('Not sure what compiler f2py uses. Speculatively setting OpenMP flag to \'-openmp\'')
compile_args = '-openmp -O3'
link_args = ''

args = {'link_args':link_args,'compile_args':compile_args}

return args



def install_dependencies():
if '--user' in sys.argv:
cmd = ['pip install -r requirements.txt --user']
else:
cmd = ['pip install -r requirements.txt']
subprocess.call(cmd, shell=True)

def install_numpy():
if '--user' in sys.argv:
cmd = ['pip install numpy --user']
else:
cmd = ['pip install numpy']
subprocess.call(cmd, shell=True)


def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read()

if __name__ == '__main__':

import sys
import subprocess
import os

install_numpy()
install_dependencies()

from numpy.distutils.core import setup

version_file = open(os.getcwd()+'/crystal_torture/'+ 'VERSION')
__version__ = version_file.read().strip()

version_file = open(os.getcwd()+'/crystal_torture/'+ 'VERSION')
__version__ = version_file.read().strip()

args = check_f2py_compiler()

config = {'name':'CrystalTorture',
'version':__version__,
'project_description':'A Crystal Tortuosity Module',
'description':'A Crystal Tortuosity Module',
'long_description': read('README.md'),
'long_description_content_type':'text/markdown',
'author':'Conn O\'Rourke',
config = {'name':'CrystalTorture',
'version':__version__,
'project_description':'A Crystal Tortuosity Module',
'description':'A Crystal Tortuosity Module',
'long_description': open('README.txt').read(),#read('README.txt'),
'long_description_content_type':'text/markdown',
'author':'Conn O\'Rourke',
'author_email':'conn.orourke@gmail.com',
'url':'https://github.com/connorourke/crystaltorture',
'python_requires':'==3.3',
'python_requires':'>=3.3',
'packages':['crystal_torture'],
'package_dir':{'crystal_torture':'crystal_torture'},
'package_data':{'crystal_torture':['*so','*tort*','*dist*']},
'package_data':{'crystal_torture':['*so','*tort*','*dist*','*o*']},
'name': 'crystal_torture',
'license': 'MIT',
'install_requires': [ 'alabaster==0.7.11',
Expand All @@ -90,7 +144,6 @@ def read(fname):
'decorator==4.3.0',
'docutils==0.14',
'entrypoints==0.2.3',
'numpy==1.14.5',
'f90wrap==0.1.4',
'html5lib==1.0.1',
'idna==2.7',
Expand All @@ -115,6 +168,7 @@ def read(fname):
'nbconvert==5.3.1',
'nbformat==4.4.0',
'notebook==5.5.0',
'numpy==1.14.5',
'packaging==17.1',
'palettable==3.1.1',
'pandas==0.20.1',
Expand All @@ -125,7 +179,7 @@ def read(fname):
'prompt-toolkit==1.0.15',
'ptyprocess==0.6.0',
'PyDispatcher==2.0.5',
'Pygments==2.2.0',
'Pygments==2.2.0',
'pymatgen==2018.6.27',
'pyparsing==2.2.0',
'python-dateutil==2.7.3',
Expand Down Expand Up @@ -154,11 +208,9 @@ def read(fname):
'widgetsnbextension==3.2.1']
}

config_tort = setup_tort_ext(args,parent_package='crystal_torture',top_path='')
# config_tort.add_library(name='',sources=None,include_dirs=[return_include_dir()])
config2 = dict(config,**config_tort.todict())#**setup_tort_ext(args,parent_package='crystal_torture',top_path='').todict())

setup(**config2)

setup(
cmdclass={
'install': CustomInstallCommand,
'develop': CustomDevelopCommand,
'egg_info': CustomEggInfoCommand,
},**config
)

0 comments on commit fccea0a

Please sign in to comment.