From 033f2a8cb5162a55e8771727ff0b57604d009ad0 Mon Sep 17 00:00:00 2001 From: Allen Byrne <50328838+byrnHDF@users.noreply.github.com> Date: Fri, 12 Jul 2024 23:28:38 -0500 Subject: [PATCH] Add python testing for examples (#4628) --- HDF5Examples/C/TUTR/CMakeTests.cmake | 97 ------------------- HDF5Examples/CMakeLists.txt | 8 ++ HDF5Examples/PYTHON/CMakeLists.txt | 72 ++++++++++++++ HDF5Examples/PYTHON/Py_sourcefiles.cmake | 20 ++++ HDF5Examples/PYTHON/h5_vlstring.py | 4 +- .../config/cmake/HDFExampleMacros.cmake | 23 +++++ 6 files changed, 125 insertions(+), 99 deletions(-) delete mode 100644 HDF5Examples/C/TUTR/CMakeTests.cmake create mode 100644 HDF5Examples/PYTHON/CMakeLists.txt create mode 100644 HDF5Examples/PYTHON/Py_sourcefiles.cmake diff --git a/HDF5Examples/C/TUTR/CMakeTests.cmake b/HDF5Examples/C/TUTR/CMakeTests.cmake deleted file mode 100644 index 96578b3db65..00000000000 --- a/HDF5Examples/C/TUTR/CMakeTests.cmake +++ /dev/null @@ -1,97 +0,0 @@ -# -# Copyright by The HDF Group. -# All rights reserved. -# -# This file is part of HDF5. The full HDF5 copyright notice, including -# terms governing use, modification, and redistribution, is contained in -# the COPYING file, which can be found at the root of the source code -# distribution tree, or in https://www.hdfgroup.org/licenses. -# If you do not have access to either file, you may request a copy from -# help@hdfgroup.org. -# - -############################################################################## -############################################################################## -### T E S T I N G ### -############################################################################## -############################################################################## -file (MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/red ${PROJECT_BINARY_DIR}/blue ${PROJECT_BINARY_DIR}/u2w) - -set (test_ex_CLEANFILES - Attributes.h5 - btrees_file.h5 - cmprss.h5 - default_file.h5 - dset.h5 - extend.h5 - extlink_prefix_source.h5 - extlink_source.h5 - extlink_target.h5 - group.h5 - groups.h5 - hard_link.h5 - mount1.h5 - mount2.h5 - one_index_file.h5 - only_dspaces_and_attrs_file.h5 - only_huge_mesgs_file.h5 - REF_REG.h5 - refere.h5 - refer_deprec.h5 - refer_extern1.h5 - refer_extern2.h5 - SDS.h5 - SDScompound.h5 - SDSextendible.h5 - Select.h5 - separate_indexes_file.h5 - small_lists_file.h5 - soft_link.h5 - subset.h5 - unix2win.h5 - blue/prefix_target.h5 - red/prefix_target.h5 - u2w/u2w_target.h5 -) - -if (HDF5_TEST_SERIAL) - # Remove any output file left over from previous test run - add_test ( - NAME EXAMPLES-clear-objects - COMMAND ${CMAKE_COMMAND} -E remove ${test_ex_CLEANFILES} - ) - set_tests_properties (EXAMPLES-clear-objects PROPERTIES - FIXTURES_SETUP clear_EXAMPLES - WORKING_DIRECTORY ${PROJECT_BINARY_DIR} - ) - add_test ( - NAME EXAMPLES-clean-objects - COMMAND ${CMAKE_COMMAND} -E remove ${test_ex_CLEANFILES} - ) - set_tests_properties (EXAMPLES-clean-objects PROPERTIES - FIXTURES_CLEANUP clear_EXAMPLES - WORKING_DIRECTORY ${PROJECT_BINARY_DIR} - ) - - foreach (example ${examples}) - if (HDF5_USING_ANALYSIS_TOOL) - add_test (NAME EXAMPLES-${example} COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR} $) - else () - add_test (NAME EXAMPLES-${example} COMMAND "${CMAKE_COMMAND}" - -D "TEST_EMULATOR=${CMAKE_CROSSCOMPILING_EMULATOR}" - -D "TEST_PROGRAM=$" - -D "TEST_ARGS:STRING=" - -D "TEST_EXPECT=0" - -D "TEST_SKIP_COMPARE=TRUE" - -D "TEST_OUTPUT=${example}.txt" - -D "TEST_FOLDER=${PROJECT_BINARY_DIR}" - -P "${HDF_RESOURCES_DIR}/runTest.cmake" - ) - endif () - set_tests_properties (EXAMPLES-${example} PROPERTIES FIXTURES_REQUIRED clear_EXAMPLES) - if (last_test) - set_tests_properties (EXAMPLES-${example} PROPERTIES DEPENDS ${last_test}) - endif () - set (last_test "EXAMPLES-${example}") - endforeach () -endif () diff --git a/HDF5Examples/CMakeLists.txt b/HDF5Examples/CMakeLists.txt index eca67915d29..7f823abb1e7 100644 --- a/HDF5Examples/CMakeLists.txt +++ b/HDF5Examples/CMakeLists.txt @@ -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}") @@ -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 () diff --git a/HDF5Examples/PYTHON/CMakeLists.txt b/HDF5Examples/PYTHON/CMakeLists.txt new file mode 100644 index 00000000000..e7e2951e731 --- /dev/null +++ b/HDF5Examples/PYTHON/CMakeLists.txt @@ -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 () diff --git a/HDF5Examples/PYTHON/Py_sourcefiles.cmake b/HDF5Examples/PYTHON/Py_sourcefiles.cmake new file mode 100644 index 00000000000..5f811532103 --- /dev/null +++ b/HDF5Examples/PYTHON/Py_sourcefiles.cmake @@ -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 +) diff --git a/HDF5Examples/PYTHON/h5_vlstring.py b/HDF5Examples/PYTHON/h5_vlstring.py index ccb7c44ed8d..62f652b75e5 100644 --- a/HDF5Examples/PYTHON/h5_vlstring.py +++ b/HDF5Examples/PYTHON/h5_vlstring.py @@ -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 @@ -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() diff --git a/HDF5Examples/config/cmake/HDFExampleMacros.cmake b/HDF5Examples/config/cmake/HDFExampleMacros.cmake index 811bd2c7b9c..c540918b9f1 100644 --- a/HDF5Examples/config/cmake/HDFExampleMacros.cmake +++ b/HDF5Examples/config/cmake/HDFExampleMacros.cmake @@ -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)