python_examples: Reformatted, self-checking, executable

* Moved body of each python example to main.  This allows for basic
      load module testing for CI
    * General cleanup of python modules (crlf/tabs/prints/etc)
    * Chmod'ed to 755 to allow running examples without specifying the
      python interpreter
    * Added ctest for loading python2/3 modules
    * Added jniclasscode pragma for java swig interface files.
    * Updated check_examplenames.py module to check all languages vs. a
      cxx example name
    * Added tests for checking python module and test loading
    * Added 'make test' to travis-ci run (run ctests)
    * Print a more meaningful message when not building cxx docs into
      python modules
    * Updated check_clean.py to only check java wrapper files
    * ENABLED ctests for UPM
    * Deleted using_carrays.py python example - this is covered by other
      examples

Signed-off-by: Noel Eck <noel.eck@intel.com>
This commit is contained in:
Noel Eck
2016-09-29 18:24:19 -07:00
parent 62718daf0b
commit 2f78d9f62b
203 changed files with 5890 additions and 5216 deletions

View File

@ -7,11 +7,42 @@ if (BUILDSWIGJAVA)
${CMAKE_CURRENT_SOURCE_DIR}/check_autoloadlibrary.py
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
add_test (NAME check_samplenames COMMAND ${PYTHON_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/check_samplenames.py
add_test (NAME check_examplenames_java COMMAND ${PYTHON_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/check_examplenames.py java
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
add_test (NAME check_clean COMMAND ${PYTHON_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/check_clean.py
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
endif ()
if (BUILDSWIGNODE)
add_test (NAME check_examplenames_js COMMAND ${PYTHON_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/check_examplenames.py js
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
endif (BUILDSWIGNODE)
if (BUILDSWIGPYTHON)
add_test (NAME check_examplenames_python COMMAND ${PYTHON_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/check_examplenames.py python
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
if (PYTHON2INTERP_FOUND)
add_test (NAME check_load_modules_python2 COMMAND ${PYTHON2_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/check_pythonload.py
${CMAKE_BINARY_DIR}/src/*/python${PYTHON2_VERSION_MAJOR}.${PYTHON2_VERSION_MINOR}/*.py
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/src/)
add_test (NAME check_load_examples_python2 COMMAND ${PYTHON2_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/check_pythonload.py
${CMAKE_SOURCE_DIR}/examples/python/*.py
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/src/)
endif (PYTHON2INTERP_FOUND)
if (PYTHON3INTERP_FOUND)
add_test (NAME check_load_modules_python3 COMMAND ${PYTHON3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/check_pythonload.py
${CMAKE_BINARY_DIR}/src/*/python${PYTHON3_VERSION_MAJOR}.${PYTHON3_VERSION_MINOR}/*.py
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/src/)
endif (PYTHON3INTERP_FOUND)
endif (BUILDSWIGPYTHON)

View File

@ -31,7 +31,8 @@ class AutoLoadLibrary(u.TestCase):
with open(os.path.join(subdir, fileName), "r") as f:
if not snippet in f.read():
broken_modules.append(moduleName)
broken_modules.append('%s: %s' % \
(moduleName, os.path.join(subdir, fileName)))
self.assertEqual( len(broken_modules), 0,
"\nThe following modules do not contain the standard auto load library code:\n" + \

View File

@ -21,6 +21,8 @@ class Clean(u.TestCase):
if not ok:
break
for fileName in files:
# Only look at JAVA wrappers
if not fileName.endswith('JAVA_wrap.cxx'): continue
if swigtypeStr in fileName:
unclean.append(dirName)
ok = False

75
tests/check_examplenames.py Executable file
View File

@ -0,0 +1,75 @@
#!/usr/bin/python
import unittest
import re, fnmatch, os, sys
# Example name map
sampleMappingFile = '../doxy/samples.mapping.txt'
# Example directories
sampledir = {}
sampledir['cxx'] = '../examples/c++/'
sampledir['java'] = '../examples/java/'
sampledir['js'] = '../examples/javascript/'
sampledir['py'] = '../examples/python/'
# Default to all targets
test_targets = ['java', 'js', 'py']
class SampleNames(unittest.TestCase):
def test_existing_samples(self):
# Dictionary for missing examples
missing = {}
# If test target/s were provided on the command line, use them
if len(self.argv) > 0:
test_targets = self.argv
# Iterate over the mapping file and check for per-language examples
with open (sampleMappingFile, "r") as f:
for line in f:
matched = {}
fullpaths = {}
# Split out the example names per target
for target in test_targets:
match = re.match('(.*.cxx).*\t(\S+.%s)' % target, line)
if match:
matched['cxx'] = match.group(1)
matched[target] = match.group(2)
# Need at least two target files to check
if len(matched) < 2: continue
# Fail if NO .cxx file to compare against
self.assertTrue('cxx' in matched)
# Update the full path to each sample file
for k,v in matched.items():
fullpaths[k] = os.path.join(sampledir[k], v)
# Use cxx base filename as testname
basename = os.path.splitext(matched['cxx'])[0]
missing[basename] = []
# Check for all example filenames
for target, filename in fullpaths.items():
if not os.path.exists(filename):
missing[basename] += [os.path.split(filename)[-1]]
# Prune out tests with no missing files
for k in missing.keys():
if len(missing[k]) == 0: del missing[k]
# Print sorted missing example filenames
sk_missing = missing.keys()
sk_missing.sort()
self.assertEqual(len(missing), 0,
'\n\nMissing examples:\n' +
'\n'.join(['%s: %s' % (k, ', '.join(missing[k])) for k in sk_missing]))
if __name__ == '__main__':
# Allow passing from argv
SampleNames.argv = []
for arg in sys.argv[1:]:
SampleNames.argv.append(sys.argv.pop())
unittest.main()

68
tests/check_pythonload.py Executable file
View File

@ -0,0 +1,68 @@
#!/usr/bin/python
import unittest
import imp
import glob
import os, sys
# Skip individual modules based on module name. For example,
# pyupm_ozw will skip pyupm_ozw.py.
blacklist = [
# OpenZwave-dependent modules
'pyupm_ozw', 'aeotecdw2e', 'tzemt400', 'aeotecdsb09104',
'aeotecss6', 'aeotecsdg2', 'ozwdump',
# Requires mraa python module
'curieimu',
# Requires modbus
'h803x', 'hwxpxx', 't3311',
# Requires bacnet
'e50hx', 'tb7300', 't8100',
# Requires PIL
'make_oled_pic'
]
class loadModule(unittest.TestCase):
''' The loadModule class loads all module which match a search string
provided via argv. If any of the target modules fails loading, this
class will assert and provide a list of failing modules.'''
def test_load_module(self):
# Python version provided on the command line
py_search_str = '../build/src/*/python2.7/*.py'
if len(self.argv) > 0:
py_search_str = self.argv[0]
# Make sure the search string has a full path
full_py_search_str = os.path.realpath(py_search_str)
# Get all python modules matching full_py_search_str
pyfiles = glob.glob(full_py_search_str)
# Fail if no modules to test
self.assertNotEqual(len(pyfiles), 0,
'Failed to find any %s modules in %s' % \
(py_search_str, full_py_search_str));
# Test load each module
failures = {}
for pyfile in pyfiles:
module = os.path.splitext(os.path.basename(pyfile))[0]
# Don't load blacklisted modules
if module in blacklist:
print('Skipping blacklisted %s ...' % pyfile)
continue
try:
pyfile = imp.load_source(module, pyfile)
except Exception as x:
failures[pyfile] = x
self.assertEqual(len(failures), 0,
'\n\nFailed to load %d modules:\n' % len(failures) +
'\n'.join(['%s: %s' % (k, ', '.join(failures[k])) for k in failures.keys()]))
if __name__ == '__main__':
# Allow passing from argv
loadModule.argv = []
for arg in sys.argv[1:]:
loadModule.argv.append(sys.argv.pop())
unittest.main()

View File

@ -1,53 +0,0 @@
#!/usr/bin/python
import unittest as u
import re, fnmatch, os, sys
sampleMappingFile = '../doxy/samples.mapping.txt'
cSamplesDir = '../examples/c++/'
javaSamplesDir = '../examples/java/'
class SampleNames(u.TestCase):
def test_existing_samples(self):
missing_c_files = []
missing_java_files = []
with open (sampleMappingFile, "r") as f:
for line in f:
sampleNames = line.split();
cSampleName = sampleNames[0]
if not cSampleName.endswith('.cxx'):
continue
javaSampleName = sampleNames[1]
if not javaSampleName.endswith('.java'):
continue
ok = False
for file in os.listdir(cSamplesDir):
if file == cSampleName:
ok = True
break
if not ok:
missing_c_files.append(cSampleName)
ok = False
for file in os.listdir(javaSamplesDir):
if file == javaSampleName:
ok = True
break
if not ok:
missing_java_files.append(javaSampleName)
self.assertEqual( len(missing_java_files) + len(missing_c_files), 0,
"\nThe following files are missing from samples:\n" + \
"\n".join(missing_c_files) + "\n" + "\n".join(missing_java_files))
if __name__ == '__main__':
u.main()