Skip to content

Commit

Permalink
Install devel space wrapper for python scripts
Browse files Browse the repository at this point in the history
Signed-off-by: Shane Loretz <sloretz@osrfoundation.org>
  • Loading branch information
sloretz committed Jan 23, 2020
1 parent 733f06b commit 9a33f43
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions cmake/catkin_install_python.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ function(catkin_install_python signature)
if(NOT ARG_DESTINATION)
message(FATAL_ERROR "catkin_install_python() called without required DESTINATION argument.")
endif()
foreach(file ${ARG_UNPARSED_ARGUMENTS})
if(NOT IS_ABSOLUTE ${file})
set(file "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
foreach(source_file ${ARG_UNPARSED_ARGUMENTS})
if(NOT IS_ABSOLUTE ${source_file})
set(source_file "${CMAKE_CURRENT_SOURCE_DIR}/${source_file}")
endif()
if(EXISTS ${file})
stamp(${file})
if(EXISTS ${source_file})
stamp(${source_file})
# read file and check shebang line
file(READ ${file} data)
file(READ ${source_file} data)
set(regex "^#!/([^\r\n]+)/env python([\r\n])")
string(REGEX MATCH "${regex}" shebang_line "${data}")
string(LENGTH "${shebang_line}" length)
Expand All @@ -35,26 +35,47 @@ function(catkin_install_python signature)
# write modified file with modified shebang line
get_filename_component(python_name ${PYTHON_EXECUTABLE} NAME)
string(REGEX REPLACE "${regex}" "#!/\\1/env ${python_name}\\2" data "${data}")
get_filename_component(filename ${file} NAME)
set(file "${CMAKE_CURRENT_BINARY_DIR}/catkin_generated/installspace")
file(MAKE_DIRECTORY ${file})
set(file "${file}/${filename}")
file(WRITE ${file} "${data}")
get_filename_component(filename ${source_file} NAME)
set(rewritten_file "${CMAKE_CURRENT_BINARY_DIR}/catkin_generated/installspace")
file(MAKE_DIRECTORY ${rewritten_file})
set(rewritten_file "${rewritten_file}/${filename}")
file(WRITE ${rewritten_file} "${data}")
endif()
# install (modified) file to destination
set(optional_flag "")
if(ARG_OPTIONAL)
set(optional_flag "OPTIONAL")
endif()
install(PROGRAMS "${file}" DESTINATION "${ARG_DESTINATION}" ${optional_flag})
# Install copy of file with re-written shebang to install space
install(PROGRAMS "${rewritten_file}" DESTINATION "${ARG_DESTINATION}" ${optional_flag})

get_filename_component(name "${file}" NAME)
# Hook for a platform specific wrapper around the modified python script
get_filename_component(name "${rewritten_file}" NAME)
add_python_executable(SCRIPT_NAME ${name}
# prefix with project name to avoid collisions across packages
TARGET_NAME ${PROJECT_NAME}_${name}_exec_install_python
DESTINATION "${ARG_DESTINATION}")

# Create devel-space wrapper if the destination is relative to the install prefix
if(NOT IS_ABSOLUTE ${ARG_DESTINATION})
message(STATUS "Installing devel-space wrapper ${source_file} to ${CATKIN_DEVEL_PREFIX}/${ARG_DESTINATION}")
# Create wrapper in devel space that uses source_file with but with correct shebang
set(PYTHON_SCRIPT ${source_file})
atomic_configure_file(${catkin_EXTRAS_DIR}/templates/script.py.in
${CATKIN_DEVEL_PREFIX}/${ARG_DESTINATION}/${name}
@ONLY)

# Hook for a platform specific wrapper around the modified python script
get_filename_component(name "${source_file}" NAME)
add_python_executable(SCRIPT_NAME ${name}
# prefix with project name to avoid collisions across packages
# cip: avoid conflicting with targets created for scripts installed via setup.py
TARGET_NAME ${PROJECT_NAME}_${name}_exec_cip_devel_python
DESTINATION "${CATKIN_DEVEL_PREFIX}/${ARG_DESTINATION}")
endif()

elseif(NOT ARG_OPTIONAL)
message(FATAL_ERROR "catkin_install_python() called with non-existing file '${file}'.")
message(FATAL_ERROR "catkin_install_python() called with non-existing file '${source_file}'.")
endif()
endforeach()
endfunction()

0 comments on commit 9a33f43

Please sign in to comment.