Skip to content

Commit

Permalink
Make python virtual environment installation optional
Browse files Browse the repository at this point in the history
  • Loading branch information
awnawab committed May 7, 2024
1 parent 06f9346 commit 7ebddc8
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 20 deletions.
25 changes: 21 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,29 @@ endif()
set( FCKIT_LIBRARIES fckit )

################################################################################################
# install python packages
include( fckit_install_venv )

# install python venv with rumael.yaml and fypp
include( fckit_install_venv )
set( install_permissions OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE )
fckit_install_venv()

# Discover only system install Python 3
set( Python3_FIND_VIRTUALENV STANDARD )
find_package( Python3 COMPONENTS Interpreter REQUIRED )

ecbuild_add_option( FEATURE FCKIT_VENV
DEFAULT OFF
DESCRIPTION "Install Python virtual environment with fypp and a yaml parser"
CONDITION Python3_VERSION VERSION_GREATER_EQUAL 3.8 )

if( HAVE_FCKIT_VENV )
fckit_install_venv()
else()
# install fypp runner script only
set( FYPP ${CMAKE_CURRENT_SOURCE_DIR}/tools/fckit-eval.sh
${CMAKE_CURRENT_SOURCE_DIR}/contrib/fypp-3.2-b8dd58b-20230822/bin/fypp )

install( FILES contrib/fypp-3.2-b8dd58b-20230822/bin/fypp DESTINATION libexec RENAME fckit-fypp.py
PERMISSIONS ${install_permissions} )
endif()

################################################################################################
# sources
Expand Down
21 changes: 15 additions & 6 deletions cmake/fckit-import.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,25 @@

if( @PROJECT_NAME@_IS_BUILD_DIR_EXPORT )
set( FCTEST_GENERATOR @Python3_EXECUTABLE@ @CMAKE_CURRENT_SOURCE_DIR@/tools/fctest-generate-runner.py )
set( FCKIT_VENV_EXE @FCKIT_VENV_EXE@ )
set( _fckit_eval_script @CMAKE_CURRENT_SOURCE_DIR@/tools/fckit-eval.sh )
set( FYPP @FYPP@ )
if( @HAVE_FCKIT_VENV@ )
set( FCKIT_VENV_EXE @FCKIT_VENV_EXE@ )
endif()
else()
if( @HAVE_FCKIT_VENV@ AND NOT @ECBUILD_INSTALL_LIBRARY_HEADERS@ )
message( FATAL_ERROR "Installation of fckit python vritual environment was disabled" )
elseif( @HAVE_FCKIT_VENV@ )
set( _fckit_eval_script ${fckit_BASE_DIR}/libexec/fckit-eval.sh )
set( FCKIT_VENV_EXE ${fckit_BASE_DIR}/@rel_venv_exe_path@ )
set( FYPP ${_fckit_eval_script} ${FCKIT_VENV_EXE} -m fypp )
else()
set( FYPP ${fckit_BASE_DIR}/libexec/fckit-eval.sh
${fckit_BASE_DIR}/libexec/fckit-fypp.py )
endif()

set( FCTEST_GENERATOR @Python3_EXECUTABLE@ ${fckit_BASE_DIR}/libexec/fctest-generate-runner.py )
set( FCKIT_VENV_EXE ${fckit_BASE_DIR}/@rel_venv_exe_path@ )
set( _fckit_eval_script ${fckit_BASE_DIR}/libexec/fckit-eval.sh )
endif()

set( FYPP ${_fckit_eval_script} ${FCKIT_VENV_EXE} -m fypp )

@FCKIT_PREPROCESS_FYPP@

@ADD_FCTEST@
Expand Down
12 changes: 5 additions & 7 deletions cmake/fckit_install_venv.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@

macro( fckit_install_venv )

# Discover only system install Python 3
set( Python3_FIND_VIRTUALENV STANDARD )
find_package( Python3 COMPONENTS Interpreter REQUIRED )

# Create a virtualenv
set( VENV_PATH ${CMAKE_CURRENT_BINARY_DIR}/venv )
set( VENV_PATH ${CMAKE_CURRENT_BINARY_DIR}/fckit_venv )
message( STATUS "Create Python virtual environment ${VENV_PATH}" )
execute_process( COMMAND ${Python3_EXECUTABLE} -m venv --copies "${VENV_PATH}" )

Expand All @@ -26,10 +22,10 @@ macro( fckit_install_venv )

# Change the context of the search to only find the venv
set( Python3_FIND_VIRTUALENV ONLY )
set( Python3_EXECUTABLE_CACHE ${Python3_EXECUTABLE} )

# Unset Python3_EXECUTABLE because it is also an input variable
# (see documentation, Artifacts Specification section)
set( Python3_EXECUTABLE_CACHE ${Python3_EXECUTABLE} )
unset( Python3_EXECUTABLE )
# To allow cmake to discover the newly created venv if Python3_ROOT_DIR
# was passed as an argument at build-time
Expand All @@ -54,7 +50,9 @@ macro( fckit_install_venv )
message( STATUS "Install fypp in virtual environment ${VENV_PATH}" )
execute_process( COMMAND ${Python3_EXECUTABLE} -m pip install --disable-pip-version-check ${CMAKE_CURRENT_SOURCE_DIR}/contrib/fypp-3.2-b8dd58b-20230822 OUTPUT_QUIET )

install( DIRECTORY ${VENV_PATH} DESTINATION . PATTERN "bin/*" PERMISSIONS ${install_permissions} )
if( ECBUILD_INSTALL_LIBRARY_HEADERS )
install( DIRECTORY ${VENV_PATH} DESTINATION . PATTERN "bin/*" PERMISSIONS ${install_permissions} )
endif()

# add python interpreter of venv as executable target
set( FCKIT_VENV_EXE ${Python3_EXECUTABLE} )
Expand Down
6 changes: 4 additions & 2 deletions src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,7 @@ add_subdirectory( test_downstream_fypp )
add_subdirectory( test_downstream_fctest )

### Test fckit_yaml_reader
ecbuild_add_test( COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/fckit_run_pytest.sh
ARGS ${CMAKE_CURRENT_BINARY_DIR}/../.. ${CMAKE_CURRENT_SOURCE_DIR} )
if( HAVE_FCKIT_VENV )
ecbuild_add_test( COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/fckit_run_pytest.sh
ARGS ${CMAKE_CURRENT_BINARY_DIR}/../.. ${CMAKE_CURRENT_SOURCE_DIR} )
endif()
2 changes: 1 addition & 1 deletion src/tests/fckit_run_pytest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# granted to it by virtue of its status as an intergovernmental organisation nor
# does it submit to any jurisdiction.

source $1/venv/bin/activate
source $1/fckit_venv/bin/activate

pytest $2/test_yaml_reader.py
retval=$?
Expand Down

0 comments on commit 7ebddc8

Please sign in to comment.