Skip to content

Commit

Permalink
Add python testing for examples (#4628)
Browse files Browse the repository at this point in the history
  • Loading branch information
byrnHDF authored Jul 13, 2024
1 parent f7ec0c2 commit 033f2a8
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 99 deletions.
97 changes: 0 additions & 97 deletions HDF5Examples/C/TUTR/CMakeTests.cmake

This file was deleted.

8 changes: 8 additions & 0 deletions HDF5Examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ if (NOT EXAMPLES_EXTERNALLY_CONFIGURED)
#-----------------------------------------------------------------------------
HDF5_SUPPORT (TRUE)
APIVersion(${HDF5_VERSION} H5_LIBVER_DIR)

#-----------------------------------------------------------------------------
# Python support check
#-----------------------------------------------------------------------------
PYTHON_SUPPORT ()
endif ()
message (STATUS "HDF5 link libs: ${H5EX_HDF5_LINK_LIBS}")
message (STATUS "HDF5 H5_LIBVER_DIR: ${H5_LIBVER_DIR} HDF5_VERSION_MAJOR: ${HDF5_VERSION_MAJOR}")
Expand Down Expand Up @@ -230,4 +235,7 @@ endif ()
if (HDF_BUILD_CPP_LIB AND HDF5_BUILD_CPP_LIB)
add_subdirectory (CXX)
endif ()
if (HDF_BUILD_PYTHON)
add_subdirectory (PYTHON)
endif ()

72 changes: 72 additions & 0 deletions HDF5Examples/PYTHON/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
cmake_minimum_required (VERSION 3.18)
project (HDF5Examples_PYTHON)

#-----------------------------------------------------------------------------
# Define Sources
#-----------------------------------------------------------------------------
include (Py_sourcefiles.cmake)

if (H5EX_BUILD_TESTING)
set (${EXAMPLE_VARNAME}_PY_CLEANFILES
compound.h5
dset.h5
gzip.h5
hype.h5
links.h5
objref.h5
regref.h5
copy1.h5
copy2.h5
string.h5
unlim.h5
vlstring.h5
)

# Remove any output file left over from previous test run
add_test (
NAME ${EXAMPLE_VARNAME}_PY-clear-objects
COMMAND ${CMAKE_COMMAND} -E remove ${${EXAMPLE_VARNAME}_PY_CLEANFILES}
)
set_tests_properties (${EXAMPLE_VARNAME}_PY-clear-objects PROPERTIES
FIXTURES_SETUP clear_${EXAMPLE_VARNAME}_PY
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
)

add_custom_target(${EXAMPLE_VARNAME}_PY-copy-objects ALL COMMENT "Copying Python source files")
foreach (example_name ${examples})
add_custom_command (
TARGET ${EXAMPLE_VARNAME}_PY-copy-objects
POST_BUILD
COMMAND ${CMAKE_COMMAND}
ARGS -E copy_if_different ${PROJECT_SOURCE_DIR}/${example_name}.py ${PROJECT_BINARY_DIR}/${example_name}.py
)
endforeach ()

add_test (
NAME ${EXAMPLE_VARNAME}_PY-clean-objects
COMMAND ${CMAKE_COMMAND} -E remove ${${EXAMPLE_VARNAME}_PY_CLEANFILES}
)
set_tests_properties (${EXAMPLE_VARNAME}_PY-clean-objects PROPERTIES
FIXTURES_CLEANUP clear_${EXAMPLE_VARNAME}_PY
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
)

macro (ADD_H5_TEST testname)
if (NOT HDF5_USING_ANALYSIS_TOOL)
add_test (
NAME ${EXAMPLE_VARNAME}_PY_${testname}
COMMAND ${Python3_EXECUTABLE} ${testname}.py
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
)
set_tests_properties (${EXAMPLE_VARNAME}_PY_${testname} PROPERTIES FIXTURES_REQUIRED clear_${EXAMPLE_VARNAME}_PY)
if (last_test)
set_tests_properties (${EXAMPLE_VARNAME}_PY_${testname} PROPERTIES DEPENDS ${last_test})
endif ()
set (last_test "${EXAMPLE_VARNAME}_PY_${testname}")
endif ()
endmacro ()

foreach (example_name ${examples})
ADD_H5_TEST (${example_name})
endforeach ()
endif ()
20 changes: 20 additions & 0 deletions HDF5Examples/PYTHON/Py_sourcefiles.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#-----------------------------------------------------------------------------
# Define Sources, one file per application
#-----------------------------------------------------------------------------
set (examples
h5_compound
h5_crtdat
h5_gzip
h5_hype
h5_hypeb
h5_links
h5_objref
h5_readtofloat
h5_regref
h5_selecelem
h5_string
h5_unlim
h5_visit
h5_visita
h5_vlstring
)
4 changes: 2 additions & 2 deletions HDF5Examples/PYTHON/h5_vlstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# Create a dataset under the Root group using variable-length string type.
#
str_type = h5py.new_vlen(str)
str_type = h5py.special_dtype(vlen=str)
dataset = file.create_dataset("DSvariable",(4,), dtype=str_type)
data = ("Parting", " is such", " sweet", " sorrow...")
dataset[...] = data
Expand All @@ -22,7 +22,7 @@
dataset = file['DSvariable']
data_out = dataset[...]
for i in range(4):
print("DSvariable[",i,"]", "'"+data_out[i]+"'", "has length", len(data_out[i]))
print("DSvariable[",i,"]", "'"+data_out[i].decode('utf-8')+"'", "has length", len(data_out[i]))

print(data_out)
file.close()
23 changes: 23 additions & 0 deletions HDF5Examples/config/cmake/HDFExampleMacros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,29 @@ macro (BASIC_SETTINGS varname)
)
endmacro ()

macro (PYTHON_SUPPORT)
option (HDF_BUILD_PYTHON "Test Python3 support" OFF)
if (HDF_BUILD_PYTHON)
find_package (Python3 COMPONENTS Interpreter Development NumPy)
if (Python3_FOUND AND Python3_NumPy_FOUND)
include (ExternalProject)
EXTERNALPROJECT_ADD (h5py
GIT_REPOSITORY https://github.com/h5py/h5py.git
GIT_TAG master
UPDATE_COMMAND ""
PATCH_COMMAND ""
CONFIGURE_COMMAND ""
BUILD_COMMAND "${CMAKE_COMMAND}" -E env HDF5_DIR=${HDF5_ROOT} "${Python3_EXECUTABLE}" setup.py build
BUILD_IN_SOURCE 1
INSTALL_COMMAND python3 -m pip --no-cache-dir install -v .
)
else ()
set (HDF_BUILD_PYTHON OFF CACHE BOOL "Test Python3 support" FORCE)
message (STATUS "Python3:${Python3_FOUND} or numpy:${Python3_NumPy_FOUND} not found - disable test of Python examples")
endif ()
endif ()
endmacro ()

macro (HDF5_SUPPORT)
set (CMAKE_MODULE_PATH ${H5EX_RESOURCES_DIR} ${CMAKE_MODULE_PATH})
option (USE_SHARED_LIBS "Use Shared Libraries" ON)
Expand Down

0 comments on commit 033f2a8

Please sign in to comment.