Ask Your Question
0

Convert lines endings of python scripts when colcon build

asked 2021-09-02 08:21:19 -0500

PatoInso gravatar image

Hello,

I have a C++ package created with ament_cmake, that also contains some python nodes. I work in a mixed environment shared between Linux and Windows, where I edit the files on Windows and run ROS2 on Linux. So the end-of-lines of my python files often get replaced by CRLF, and so my python nodes failed to run on my Linux install of ROS2.

How can I convert automatically the EOL when doing colcon build ?

I tried to execute dos2unix in the CMakeList.txt but this actually does not convert the .py files, even while there is no dos2unix error:

# Install Python executables
install(PROGRAMS
    scripts/laserscan_adapter.py
    DESTINATION lib/${PROJECT_NAME}
)

ament_package()

# This runs without error message but the file is not actually changed
execute_process(COMMAND dos2unix /abs/path/to/workspace/install/leg_detector/lib/laserscan_processor/laserscan_adapter.py)

# This fails with dos2unix: No such file or directory, Skipping: not a regular file
execute_process(COMMAND dos2unix /abs/path/to/workspace/install/leg_detector/lib/laserscan_processor/*.py)

However the same commands copy-pasted in the terminal works as expected. I note that ament_python packages works no matter the lines ending.

How can I convert the end of lines or call dos2unix automatically when doing colcon build ? Is there a better approach for this conversion ?

Thanks,

edit retag flag offensive close merge delete

Comments

Colcon is not involved here, it's CMake. So don't restrict yourself to looking for solutions "with Colcon".

A CMakeLists.txt is not executed sequentially, it's a tree of dependencies visited in the appropriate order (ie: dependencies first, then dependents).

Your two execute_process(..) statements don't specify any dependencies, nor are there any dependents, so I'm not sure they are ever executed (they could be, but I wouldn't know when).

You'll want to lookup documentation on CMake's execute_process(..), specifically the parts about configuring the targets they are attached to.

gvdhoorn gravatar image gvdhoorn  ( 2021-09-02 09:41:21 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-09-02 10:20:49 -0500

PatoInso gravatar image

updated 2021-09-02 10:52:54 -0500

@gvdhoorn put me on the right track: indeed there are several steps in the execution of a CMakeLists.txt and it is not necessary sequential: from the CMake doc:

The execute_process() command [...] run while CMake is processing the project prior to build system generation.

A trick is impired by this SO question is to use the CODE variant of the install command, that is run last (or at least after the whole build process):

install(CODE "execute_process(COMMAND dos2unix laserscan_processor.py WORKING_DIRECTORY ${CMAKE_INSTALL_PREFIX}/lib/${PROJECT_NAME})")

The EOL are effectively converted to the Unix format.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2021-09-02 08:21:19 -0500

Seen: 41 times

Last updated: Sep 02 '21