Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update scripts to Python3 #245

Merged
merged 2 commits into from
Jul 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ addons:
packages:
- build-essential
- cmake
- python
- python3
- libboost-all-dev
- libgsl-dev
- libxerces-c-dev
Expand Down
13 changes: 6 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@
# Copyright © 2005-2013 Swiss Tropical Institute and Liverpool School Of Tropical Medicine
# Licence: GNU General Public Licence version 2 or later (see COPYING)

cmake_minimum_required (VERSION 2.4)
# We still (try to) support cmake 2.4, but cmake 2.8 policies also work:
if("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" GREATER 2.6)
cmake_policy(VERSION 2.8)
elseif("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" GREATER 2.4)
cmake_policy(VERSION 2.6)
endif("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" GREATER 2.6)
cmake_minimum_required (VERSION 3.0)

project (OpenMalaria CXX)

Expand Down Expand Up @@ -94,6 +88,11 @@ find_package (XSD)
find_package (XERCESC)
find_package (GSL)
find_package (Z)
find_package (Python)

if (NOT PYTHON_EXECUTABLE)
message (SEND_ERROR "Unable to find a Python interpreter (required).")
endif (NOT PYTHON_EXECUTABLE)


# Find Boost:
Expand Down
12 changes: 11 additions & 1 deletion FindPython.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@
#
# Defines PYTHON_EXECUTABLE. Doesn't abort if not found.

find_program (PYTHON_EXECUTABLE python
# We need Python 3.x which is now often the default version.
# On Windows, install paths may help
find_program (PYTHON_EXECUTABLE
NAMES python3 python3.exe python python.exe
PATHS
"C:\\Python37-x64" "C:\\Python37"
"C:\\Python36-x64" "C:\\Python36"
"C:\\Python35-x64" "C:\\Python35"
"C:\\Python34-x64" "C:\\Python34"
"C:\\Python33-x64" "C:\\Python33"
DOC "Path to python (required).")
message (STATUS "Found Python interpreter: ${PYTHON_EXECUTABLE}")

mark_as_advanced (PYTHON_EXECUTABLE)
2 changes: 0 additions & 2 deletions schema/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
# Copyright © 2005-2011 Swiss Tropical Institute and Liverpool School Of Tropical Medicine
# Licence: GNU General Public Licence version 2 or later (see COPYING)

find_package (Python)

set (SCHEMA_NAMES
scenario
demography
Expand Down
15 changes: 5 additions & 10 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
# Copyright © 2005-2009 Swiss Tropical Institute and Liverpool School Of Tropical Medicine
# Licence: GNU General Public Licence version 2 or later (see COPYING)

find_package (Python)

# Testing via run.sh or run.py script:
configure_file (
${CMAKE_CURRENT_SOURCE_DIR}/run.py
Expand Down Expand Up @@ -53,14 +51,11 @@ set (OM_BOXTEST_NAMES
# tests with broken checkpointing:
set (OM_BOXTEST_NC_NAMES)
# Disabled due to "in-progress" work: (none)

# Use python script.
if (PYTHON_EXECUTABLE)
foreach (TEST_NAME ${OM_BOXTEST_NAMES})
foreach (TEST_NAME ${OM_BOXTEST_NAMES})
add_test (${TEST_NAME} ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/run.py ${TEST_NAME} -- --checkpoint)
endforeach (TEST_NAME)
foreach (TEST_NAME ${OM_BOXTEST_NC_NAMES})
endforeach (TEST_NAME)
foreach (TEST_NAME ${OM_BOXTEST_NC_NAMES})
add_test (${TEST_NAME} ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/run.py -- ${TEST_NAME})
endforeach (TEST_NAME)
else (PYTHON_EXECUTABLE)
message(WARNING "Tests are disabled (Python is needed to run them)")
endif (PYTHON_EXECUTABLE)
endforeach (TEST_NAME)
34 changes: 17 additions & 17 deletions test/run.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# This file is part of OpenMalaria.
Expand Down Expand Up @@ -51,7 +51,7 @@ def __str__(self):
testSrcDir="@CMAKE_CURRENT_SOURCE_DIR@"
testBuildDir="@CMAKE_CURRENT_BINARY_DIR@"
if not os.path.isdir(testSrcDir) or not os.path.isdir(testBuildDir):
print "Don't run this script directly; configure CMake then use the version in the CMake build dir."
print("Don't run this script directly; configure CMake then use the version in the CMake build dir.")
sys.exit(-1)

# executable
Expand Down Expand Up @@ -106,7 +106,7 @@ def linkOrCopy (src, dest):
# This can still fail: a VirtualBox guest is not allowed to create
# links on a drive shared by the host
os.symlink(os.path.abspath(src), dest)
except OSError,e:
except OSError as e:
shutil.copy2(src, dest)
else:
shutil.copy2(src, dest)
Expand All @@ -133,13 +133,13 @@ def runScenario(options,omOptions,name):
cmd=["xmllint","--noout","--schema",scenarioSchema,scenarioSrc]
# alternative: ["xmlstarlet","val","-s",SCHEMA,scenarioSrc]
if options.logging:
print "\033[0;32m "+(" ".join(cmd))+"\033[0;00m"
print("\033[0;32m "+(" ".join(cmd))+"\033[0;00m")
return subprocess.call (cmd,cwd=testBuildDir)

cmd=options.wrapArgs+[openMalariaExec,"--deprecation-warnings","--resource-path",os.path.abspath(testSrcDir),"--scenario",scenarioSrc]+omOptions

if not options.run:
print "\033[0;32m "+(" ".join(cmd))+"\033[0;00m"
print("\033[0;32m "+(" ".join(cmd))+"\033[0;00m")
return 0

# Run from a temporary directory, so checkpoint files won't conflict
Expand All @@ -156,16 +156,16 @@ def runScenario(options,omOptions,name):
linkOrCopy (scenarioSchema, scenario_xsd)

if options.logging:
print time.strftime("\033[0;33m%a, %d %b %Y %H:%M:%S")+"\t\033[1;33m%s" % scenarioSrc
print(time.strftime("\033[0;33m%a, %d %b %Y %H:%M:%S")+"\t\033[1;33m%s" % scenarioSrc)

startTime=lastTime=time.time()
# While no output.txt file and cmd exits successfully:
while (not os.path.isfile(outputFile)):
if options.logging:
print "\033[0;32m "+(" ".join(cmd))+"\033[0;00m"
print("\033[0;32m "+(" ".join(cmd))+"\033[0;00m")
ret=subprocess.call (cmd, shell=False, cwd=simDir)
if ret != 0:
print "\033[1;31mNon-zero exit status: " + str(ret)
print("\033[1;31mNon-zero exit status: " + str(ret))
break

# check for output.txt.gz in place of output.txt and uncompress:
Expand Down Expand Up @@ -195,7 +195,7 @@ def runScenario(options,omOptions,name):
lastTime=checkTime

if ret == 0 and options.logging:
print "\033[0;33mDone in " + str(time.time()-startTime) + " seconds"
print("\033[0;33mDone in " + str(time.time()-startTime) + " seconds")

if options.cleanup:
os.remove(scenario_xsd)
Expand All @@ -218,7 +218,7 @@ def runScenario(options,omOptions,name):
ctsret,ctsident = compareCtsout.main (origCtsout, ctsoutFile)
else:
ctsret,ctsident = 3,False
print "\033[1;31mNo original ctsout.txt to compare with."
print("\033[1;31mNo original ctsout.txt to compare with.")
if ctsident and options.cleanup:
os.remove(ctsoutFile)
if os.path.isfile(newCtsout):
Expand All @@ -233,7 +233,7 @@ def runScenario(options,omOptions,name):
ret,ident = compareOutput.main (origOutput, outputFile, 0)
else:
ret,ident = 3,False
print "\033[1;31mNo original output.txt to compare with."
print("\033[1;31mNo original output.txt to compare with.")
if ident and options.cleanup:
os.remove(outputFile)
if os.path.isfile(newOutput):
Expand All @@ -243,12 +243,12 @@ def runScenario(options,omOptions,name):
ret,ident = 1,False
stderrFile=os.path.join(simDir,"stderr.txt")
if os.path.isfile (stderrFile):
print "\033[1;31mNo output 'output.txt'; error messages:"
print("\033[1;31mNo output 'output.txt'; error messages:")
se = open(stderrFile)
se.read()
se.close()
else:
print "\033[1;31mNo output 'output.txt'"
print("\033[1;31mNo output 'output.txt'")

ret=max(ret,ctsret)
ident=ident and ctsident
Expand All @@ -265,9 +265,9 @@ def runScenario(options,omOptions,name):
try:
os.rmdir(simDir)
except OSError:
print "\033[0;31mDirectory %s not empty, so not deleted!" % simDir
print("\033[0;31mDirectory %s not empty, so not deleted!" % simDir)

print "\033[0;00m"
print("\033[0;00m")
return ret

def setWrapArgs(option, opt_str, value, parser, *args, **kwargs):
Expand Down Expand Up @@ -345,8 +345,8 @@ def main(args):
retVal = r if retVal == 0 else retVal

return retVal
except RunError,e:
print str(e)
except RunError as e:
print(str(e))
return -1

if __name__ == "__main__":
Expand Down
10 changes: 5 additions & 5 deletions unittest/AgeGroupInterpolationSuite.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# This file is part of OpenMalaria.
Expand Down Expand Up @@ -52,7 +52,7 @@ def set_age_groups(self,age_lower_bounds,values):
self.v=[values[0]]
self.v.extend(values)
self.v.append(values[len(values)-1])
print "Lengths:",len(self.a),len(self.v)
print("Lengths:",len(self.a),len(self.v))

def interpolate(self,t):
i=0
Expand Down Expand Up @@ -141,9 +141,9 @@ def plot(self,subplot):
# Generate sample data for unittest:
x=[15.2,18.09,7.0, 2.5, 0.0,20.0,900.0,62.0]

print "Age points:",x
print "Piecewise constant:",[const.interpolate(t) for t in x]
print "Linear interpolation:",[linear.interpolate(t) for t in x]
print("Age points:",x)
print("Piecewise constant:",[const.interpolate(t) for t in x])
print("Linear interpolation:",[linear.interpolate(t) for t in x])


# Third plot: CMF data
Expand Down
13 changes: 3 additions & 10 deletions unittest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
# Copyright © 2005-2009 Swiss Tropical Institute and Liverpool School Of Tropical Medicine
# Licence: GNU General Public Licence version 2 or later (see COPYING)

find_package (Python)

set (OM_CXXTEST_INTERPRETER ${PYTHON_EXECUTABLE})
set (OM_CXXTEST_SCRIPT ${CMAKE_SOURCE_DIR}/contrib/cxxtest/bin/cxxtestgen)

Expand Down Expand Up @@ -50,18 +48,13 @@ set (OM_CXXTEST_HEADERS
PkPdComplianceSuite.h
)

#Appears to be problems with this on windows...
if (OM_CXXTEST_INTERPRETER)
add_custom_command (OUTPUT tests.cpp
COMMAND ${OM_CXXTEST_INTERPRETER} ${OM_CXXTEST_SCRIPT} ${OM_CXXTEST_OPTIONS} --runner=ParenPrinter -o ${CMAKE_CURRENT_BINARY_DIR}/tests.cpp ${OM_CXXTEST_HEADERS}
add_custom_command (OUTPUT tests.cpp
COMMAND ${PYTHON_EXECUTABLE} ${OM_CXXTEST_SCRIPT} ${OM_CXXTEST_OPTIONS} --runner=ParenPrinter -o ${CMAKE_CURRENT_BINARY_DIR}/tests.cpp ${OM_CXXTEST_HEADERS}
DEPENDS ${OM_CXXTEST_HEADERS}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "Generating unittest code with cxxtestgen"
VERBATIM
)
else (OM_CXXTEST_INTERPRETER)
message (SEND_ERROR "Unable to find python executable. Thus I can't build tests.cpp which is required to run the unittests.")
endif (OM_CXXTEST_INTERPRETER)
)

# These files are needed to run various infection model tests:
configure_file (${CMAKE_SOURCE_DIR}/test/autoRegressionParameters.csv ${CMAKE_CURRENT_BINARY_DIR} COPYONLY)
Expand Down
2 changes: 1 addition & 1 deletion unittest/DecayFunctionSuite.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# This file is part of OpenMalaria.
Expand Down
Loading