Skip to content

Commit

Permalink
add test for setup command line (#1302)
Browse files Browse the repository at this point in the history
* fix pip

* test

* add nose

* not install pytraj from begining if testing setup

* try amberhome

* fix install

* test

* update MANIFEST.in

* git pull cpptraj

* Update test_setup_command.py

* clone cpptraj

* try more
  • Loading branch information
hainm authored Aug 31, 2016
2 parents f2c6596 + 096bd04 commit a872b70
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 76 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ matrix:
- { os: linux, env: PYTHON_VERSION=3.4 }
- { os: linux, env: PYTHON_VERSION=3.5 }
- { os: linux, env: PYTHON_VERSION=3.5 COMPILER=clang }
- { os: linux, env: PYTHON_VERSION=3.5 TEST_SETUP=true }
# - { os: osx, env: PYTHON_VERSION=3.5 }

sudo: false
Expand Down
2 changes: 0 additions & 2 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
graft scripts
graft pytraj
graft devtools
graft tests
graft examples
graft contributors
graft licenses
Expand Down
10 changes: 7 additions & 3 deletions devtools/travis-ci/install_pytraj.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then
python setup.py install --disable-openmp
else
if [ "$COMPILER" == "clang" ]; then
CC=clang CXX=clang++ python setup.py install --disable-openmp
if [[ "$TEST_SETUP" == 'true' ]]; then
echo "TEST_SETUP"
else
python setup.py install
if [ "$COMPILER" == "clang" ]; then
CC=clang CXX=clang++ python setup.py install --disable-openmp
else
python setup.py install
fi
fi
fi
79 changes: 42 additions & 37 deletions devtools/travis-ci/run_tests.sh
Original file line number Diff line number Diff line change
@@ -1,42 +1,47 @@
#!/bin/sh


sh devtools/travis-ci/pyflakes_check.sh || exit 1

PLATFORM=`python -c 'import sys; print(sys.platform)'`
echo "PLATFORM =" $PLATFORM

# set env
PYTRAJ_HOME=`pwd`
export CPPTRAJHOME=`pwd`"/cpptraj/"
cd tests/energies/fake_amberhome/
export AMBERHOME=`pwd`

# go back to tests folder
cd ../../

# print info
python run_simple_test.py || exit 1

# run tests
if [ "$PLATFORM" = "linux" ]; then
nosetests --with-coverage --cover-package pytraj -vs test_*.py */test_*.py || exit 1
unset CPPTRAJHOME # for pytraj.tools coverage
nosetests --with-coverage --cover-package pytraj -vs test_docs.py || exit 1
if [ "$TEST_SETUP" == 'true' ]; then
echo "Test setup command line"
nosetests -vs devtools/travis-ci/test_setup_command.py
else
# osx (window too?), minimal tests
nosetests -vs test_*.py */test_*.py || exit 1
sh devtools/travis-ci/pyflakes_check.sh || exit 1

PLATFORM=`python -c 'import sys; print(sys.platform)'`
echo "PLATFORM =" $PLATFORM

# set env
PYTRAJ_HOME=`pwd`
export CPPTRAJHOME=`pwd`"/cpptraj/"
cd tests/energies/fake_amberhome/
export AMBERHOME=`pwd`

# go back to tests folder
cd ../../

# print info
python run_simple_test.py || exit 1

# run tests
if [ "$PLATFORM" = "linux" ]; then
nosetests --with-coverage --cover-package pytraj -vs test_*.py */test_*.py || exit 1
unset CPPTRAJHOME # for pytraj.tools coverage
nosetests --with-coverage --cover-package pytraj -vs test_docs.py || exit 1
else
# osx (window too?), minimal tests
nosetests -vs test_*.py */test_*.py || exit 1
fi

# run examples
cd ../examples
python ./run_examples.py || exit 1
cd mpi_examples

# only do mpi test for linux
if [ "$PLATFORM" = "linux" ]; then
sh run_mpi_examples.sh 4 || exit 1
fi

# go back to pytraj home
cd $PYTRAJ_HOME
fi

# run examples
cd ../examples
python ./run_examples.py || exit 1
cd mpi_examples

# only do mpi test for linux
if [ "$PLATFORM" = "linux" ]; then
sh run_mpi_examples.sh 4 || exit 1
fi

# go back to pytraj home
cd $PYTRAJ_HOME
107 changes: 107 additions & 0 deletions devtools/travis-ci/test_setup_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/usr/bin/env python
import os
from contextlib import contextmanager
import subprocess
from nose import tools

# tested with py3.5

# utils
def clone_cpptraj():
os.system('git clone https://github.com/amber-md/cpptraj')

@contextmanager
def temporarily_rename_cpptraj_folder():
old_name = 'cpptraj'
new_name = 't_cpptraj'
if os.path.exists(old_name):
print(os.path.abspath(old_name))
subprocess.check_call('mv {} {}'.format(old_name, new_name).split())
yield
subprocess.check_call('mv {} {}'.format(new_name, old_name).split())
else:
print("does not have cpptraj, nothing done")
yield
print("nothing done")

def capture_stderr(command):
"""capture_stderr"""
try:
subprocess.check_output(command.split(), stderr=subprocess.STDOUT)
return None
except subprocess.CalledProcessError as e:
return e.output.decode()

def git_clean_folder(name):
"""git_clean_folder"""
command = 'git clean -fdx {}'.format(name)
print('clean: ', command)
subprocess.check_call(command.split())

def get_output(command):
return subprocess.check_output(command.split()).decode()

# do not need cpptraj folder
def test_setup_clean():
""" python setup.py clean """
command = 'python setup.py clean'
output = subprocess.check_output(command.split()).decode()
tools.assert_in('must_compile_c_extension = False', output)
tools.assert_in('use_pip = False', output)
tools.assert_in('running clean', output)
tools.assert_not_in('libcpptraj', output)
tools.assert_not_in('github', output)

def test_setup_help():
""" python setup.py --help """
for command in ['python setup.py --help', 'python setup.py -h']:
output = subprocess.check_output(command.split()).decode()
tools.assert_in('setup.py install will install the package', output)
tools.assert_not_in('libcpptraj', output)
tools.assert_not_in('github', output)

def test_pip_help():
""" pip install --help """
command = 'pip install --help'
output = subprocess.check_output(command.split()).decode()
tools.assert_in('Install Options', output)

def test_raise_if_using_pip_but_does_not_have_cpptraj_home():
""" test_raise_if_using_pip_but_does_not_have_cpptraj_home_or_not_cpptraj_in_this_folder
pip install -e .
pip install .
pip wheel .
"""
os.environ['CPPTRAJHOME'] = ''
with temporarily_rename_cpptraj_folder():
for command in ['pip install -e .', 'pip install .', 'pip wheel .']:
print(command)
output = capture_stderr(command)
tools.assert_in('using pip, must set CPPTRAJHOME', output)

def test_install_libcpptraj_if_having_cpptraj_folder_here():
clone_cpptraj()
git_clean_folder('./cpptraj/lib/libcpptraj*')
command = 'python setup.py build'
output = get_output(command)
tools.assert_in('cpptraj/lib/libcpptraj', output)
tools.assert_in('install libcpptraj from current', output)

def test_install_to_amberhome():
fn = './fake_amberhome'
subprocess.check_call('mkdir -p {}/lib/python3.5/site-packages/'.format(fn).split())
full_name = os.path.abspath(fn)
command = 'python setup.py install --prefix={} --no-setuptools'.format(full_name)
output = subprocess.check_output(command.split()).decode()
tools.assert_in('running install_egg_info', output)
tools.assert_in('running install_lib', output)
tools.assert_in('Writing', output)
git_clean_folder(full_name)

def test_default_raise_if_install_cpptraj_without_openmp():
""" test_default_raise_if_install_cpptraj_without_openmp """
subprocess.check_output('python ./scripts/install_libcpptraj.py'.split())
command = 'python setup.py install'
output = capture_stderr(command)
tools.assert_in('libcpptraj was NOT installed with openmp', output)
tools.assert_in('Turn off openmp in pytraj: python setup.py install --disable-openmp', output)
44 changes: 24 additions & 20 deletions scripts/base_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def get_version_info():
GIT_REVISION = "Unknown"

if not is_released:
FULLVERSION += '.dev0+' + GIT_REVISION[:7]
FULLVERSION += '.dev0'

return FULLVERSION, GIT_REVISION

Expand Down Expand Up @@ -221,17 +221,19 @@ def write_version_py(filename='pytraj/version.py'):
finally:
a.close()

def do_what(pytraj_home):
do_install = do_build = False
def do_what(pytraj_home, use_pip=False):
must_compile_c_extension = False
# this checking should be here, after checking openmp and other stuff
if '--help' in sys.argv or '-h' in sys.argv or '--help-commands' in sys.argv:
do_install = do_build = False
must_compile_c_extension = False
elif use_pip:
must_compile_c_extension = True
else:
if 'install' in sys.argv:
do_install = True
if 'build' in sys.argv or 'build_ext' in sys.argv:
do_build = True
return do_install, do_build
if ('install' in sys.argv or
'build' in sys.argv or
'build_ext' in sys.argv):
must_compile_c_extension = True
return must_compile_c_extension

def install_libcpptraj(openmp_flag, from_github=False, use_amberlib=True):
'''If AMBERHOME is set and amberlib is True, use -amberlib for libcpptraj
Expand All @@ -246,8 +248,7 @@ def install_libcpptraj(openmp_flag, from_github=False, use_amberlib=True):
subprocess.check_call(cmd, shell=True)

def try_updating_libcpptraj(cpptraj_home,
do_install,
do_build,
must_compile_c_extension,
cpptraj_included,
openmp_flag,
use_amberlib):
Expand All @@ -258,7 +259,8 @@ def try_updating_libcpptraj(cpptraj_home,
'1. unset CPPTRAJHOME and `python setup.py install` again. We will install libcpptraj for you. \n'
'2. Or you need to install libcpptraj in $CPPTRAJHOME/lib \n')
else:
if do_install or do_build:
if must_compile_c_extension:
print('must_compile_c_extension')
if cpptraj_included:
print(
'can not find libcpptraj but found ./cpptraj folder, trying to reinstall it to ./cpptraj/lib/ \n')
Expand Down Expand Up @@ -380,7 +382,7 @@ def get_cpptraj_info(rootname,
cpptraj_info.lib_dir = cpptraj_info.dir + "/lib/"
else:

if setup_task.do_install or setup_task.do_build:
if setup_task.must_compile_c_extension:
print(message_auto_install)
for i in range(0, 3):
sys.stdout.write('.')
Expand Down Expand Up @@ -474,14 +476,16 @@ def get_ext_modules(cpptraj_info,
define_macros=[],
use_pip=False,
tarfile=False):
if setup_task.do_help:
return

if not tarfile:
print('build = {0}, install = {1}'.format(setup_task.do_build, setup_task.do_install))
print('install = {}'.format(setup_task.must_compile_c_extension))
if not libcpptraj_files:
libcpptraj_files = try_updating_libcpptraj(cpptraj_info.home_env,
setup_task.do_install, setup_task.do_build, cpptraj_included, openmp_flag, use_amberlib)
libcpptraj_files = try_updating_libcpptraj(
cpptraj_home=cpptraj_info.home_env,
must_compile_c_extension=setup_task.must_compile_c_extension,
cpptraj_included=cpptraj_included,
openmp_flag=openmp_flag,
use_amberlib=use_amberlib
)

try:
output_openmp_check = subprocess.check_output(['nm', libcpptraj_files[0]]).decode().split('\n')
Expand Down Expand Up @@ -510,7 +514,7 @@ def get_ext_modules(cpptraj_info,

pyxfiles, pxdfiles = get_pyx_pxd()

if not (setup_task.do_help or setup_task.do_clean or is_released):
if not is_released:
from Cython.Build import cythonize
if sys.platform.startswith("win"):
cythonize(
Expand Down
34 changes: 20 additions & 14 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import os
import sys
from collections import namedtuple

print('sys.argv', sys.argv)
try:
# for amber
sys.argv.remove('--no-setuptools')
Expand Down Expand Up @@ -48,27 +50,30 @@
rootname = os.getcwd()
pytraj_src = rootname + "/pytraj/"
cpptraj_home = os.environ.get('CPPTRAJHOME', '')
use_pip = any('pip' in arg for arg in sys.argv)

# danger
use_pip = (any('egg_info' in arg for arg in sys.argv) or
any('pip' in arg for arg in sys.argv) or
any('--no-deps' in arg for arg in sys.argv))

print('use_pip = {}, cpptraj_home = {}'.format(use_pip, cpptraj_home))
if use_pip and not cpptraj_home:
if not os.path.exists('./cpptraj'):
print('Detect using pip, must set CPPTRAJHOME if there is no cpptraj in current folder')
sys.exit(1)

install_type = os.environ.get("INSTALLTYPE", "")
check_flag(install_type)
if install_type:
print('install_type', install_type)

if not cpptraj_home and use_pip:
# if pip, require to set CPPTRAJHOME
raise EnvironmentError(message_pip_need_cpptraj_home)

cpptraj_included = os.path.exists("./cpptraj/")
pytraj_home = os.path.abspath(os.path.dirname(__file__))

SetupTask = namedtuple('SetupTask', ['do_install', 'do_build', 'do_help', 'do_clean'])
do_install, do_build = do_what(pytraj_home)
do_help = '--help' in sys.argv or '-h' in sys.argv
do_clean = (len(sys.argv) == 2 and 'clean' in sys.argv)
setup_task = SetupTask(do_install=do_install,
do_build=do_build,
do_help=do_help,
do_clean=do_clean)
SetupTask = namedtuple('SetupTask', ['must_compile_c_extension'])
must_compile_c_extension = do_what(pytraj_home, use_pip=use_pip)
print('must_compile_c_extension = ', must_compile_c_extension)
setup_task = SetupTask(must_compile_c_extension=must_compile_c_extension)

cpptraj_info = get_cpptraj_info(rootname=rootname,
cpptraj_home=cpptraj_home,
Expand Down Expand Up @@ -109,7 +114,7 @@

setenv_cc_cxx(cpptraj_info.ambertools_distro, extra_compile_args, extra_link_args)

if not do_build and not do_install:
if not must_compile_c_extension:
ext_modules = []
else:
ext_modules = get_ext_modules(cpptraj_info=cpptraj_info,
Expand All @@ -129,6 +134,7 @@
use_pip=use_pip,
tarfile=tarfile)

print(ext_modules)
setup_args = {}
packages = [
'pytraj',
Expand Down

0 comments on commit a872b70

Please sign in to comment.