Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix symlink install python when shebang line is rewritten #1100

Merged
merged 4 commits into from
Jul 13, 2020
Merged

Fix symlink install python when shebang line is rewritten #1100

merged 4 commits into from
Jul 13, 2020

Conversation

kenji-miyake
Copy link
Contributor

@kenji-miyake kenji-miyake commented Jun 29, 2020

Overview

This PR fixes catkin_install_python when CATKIN_SYMLINK_INSTALL=ON.

I'm sending this PR to kinetic-devel because I'm still using melodic, but I believe the same change can be applied to noetic-devel. -> Changed to noetic-devel.

Problem

In catkin_install_python.cmake, new (shebang-replaced) files which are generated by file(WRITE ...), which drops the permissions.

file(WRITE ${rewritten_file} "${data}")

This disables python-scripts execution with rosrun.

Supplemental

The permissions of symlink files (inside install/) are executable because of install(PROGRAMS ...)

install(PROGRAMS "${rewritten_file}" DESTINATION "${ARG_DESTINATION}" ${optional_flag})

However, the real files (inside catkin_generated/installspace) are not executable.

Solution

I just added file(COPY ...) before file(WRITE ...).
aead61d#diff-3cfcccb0a4b1968a8dae7de378653f99R41

I also added a variable installspace in order to avoid overwriting the variable, but I can drop this change from this PR.
8f23bf5

How to test (Melodic)

###################################
##### Launch docker container #####
###################################
$ docker run --rm -it osrf/ros:melodic-desktop-full

##################################################
##### Check result with this patch (working) #####
##################################################
# apt update && apt install -y python-catkin-tools
# mkdir -p ~/catkin_ws/src
# cd ~/catkin_ws/src
# git clone https://github.com/kenji-miyake/catkin.git --depth 1 -b fix-symlink-install-python-kinetic # Actually based on noetic-devel
# git clone https://github.com/ros/ros_tutorials.git --depth 1 -b melodic-devel
# cd ~/catkin_ws
# catkin config --install
# catkin build rospy_tutorials --cmake-args -DCATKIN_SYMLINK_INSTALL=ON

# ls -l ~/catkin_ws/install/share/rospy_tutorials/001_talker_listener
total 24
lrwxrwxrwx 1 root root 76 Jun 29 13:55 README -> /root/catkin_ws/src/ros_tutorials/rospy_tutorials/001_talker_listener/README
lrwxrwxrwx 1 root root 76 Jun 29 13:55 listener -> /root/catkin_ws/build/rospy_tutorials/catkin_generated/installspace/listener
lrwxrwxrwx 1 root root 79 Jun 29 13:55 listener.py -> /root/catkin_ws/build/rospy_tutorials/catkin_generated/installspace/listener.py
lrwxrwxrwx 1 root root 74 Jun 29 13:55 talker -> /root/catkin_ws/build/rospy_tutorials/catkin_generated/installspace/talker
lrwxrwxrwx 1 root root 77 Jun 29 13:55 talker.py -> /root/catkin_ws/build/rospy_tutorials/catkin_generated/installspace/talker.py
lrwxrwxrwx 1 root root 92 Jun 29 13:55 talker_listener.launch -> /root/catkin_ws/src/ros_tutorials/rospy_tutorials/001_talker_listener/talker_listener.launch

# ls -l /root/catkin_ws/build/rospy_tutorials/catkin_generated/installspace/listener.py
-rwxr-xr-x 1 root root 2407 Jun 29 13:55 /root/catkin_ws/build/rospy_tutorials/catkin_generated/installspace/listener.py

# /root/catkin_ws/build/rospy_tutorials/catkin_generated/installspace/listener.py
Unable to register with master node [http://localhost:11311]: master may not be running yet. Will keep trying.

#########################################################
##### Check result without this patch (not working) #####
#########################################################
# cd ~/catkin_ws
# catkin clean -y
# rm -rf ~/catkin_ws/src/catkin
# catkin build rospy_tutorials --cmake-args -DCATKIN_SYMLINK_INSTALL=ON

# ls -l ~/catkin_ws/install/share/rospy_tutorials/001_talker_listener
total 24
lrwxrwxrwx 1 root root 76 Jun 29 14:02 README -> /root/catkin_ws/src/ros_tutorials/rospy_tutorials/001_talker_listener/README
lrwxrwxrwx 1 root root 76 Jun 29 14:02 listener -> /root/catkin_ws/build/rospy_tutorials/catkin_generated/installspace/listener
lrwxrwxrwx 1 root root 79 Jun 29 14:02 listener.py -> /root/catkin_ws/build/rospy_tutorials/catkin_generated/installspace/listener.py
lrwxrwxrwx 1 root root 74 Jun 29 14:02 talker -> /root/catkin_ws/build/rospy_tutorials/catkin_generated/installspace/talker
lrwxrwxrwx 1 root root 77 Jun 29 14:02 talker.py -> /root/catkin_ws/build/rospy_tutorials/catkin_generated/installspace/talker.py
lrwxrwxrwx 1 root root 92 Jun 29 14:02 talker_listener.launch -> /root/catkin_ws/src/ros_tutorials/rospy_tutorials/001_talker_listener/talker_listener.launch

# ls -l /root/catkin_ws/build/rospy_tutorials/catkin_generated/installspace/listener.py
-rw-r--r-- 1 root root 2407 Jun 29 14:02 /root/catkin_ws/build/rospy_tutorials/catkin_generated/installspace/listener.py

# /root/catkin_ws/build/rospy_tutorials/catkin_generated/installspace/listener.py
bash: /root/catkin_ws/build/rospy_tutorials/catkin_generated/installspace/listener.py: Permission denied

How to test (Noetic)

###################################
##### Launch docker container #####
###################################
$ docker run --rm -it osrf/ros:noetic-desktop-full

##################################################
##### Check result with this patch (working) #####
##################################################
# apt update && apt install -y git python3-pip
# pip3 install -U "git+https://github.com/catkin/catkin_tools.git#egg=catkin_tools"
# mkdir -p ~/catkin_ws/src
# cd ~/catkin_ws/src
# git clone https://github.com/kenji-miyake/catkin.git --depth 1 -b fix-symlink-install-python-kinetic # Actually based on noetic-devel
# git clone https://github.com/ros/ros_tutorials.git --depth 1 -b melodic-devel
# cd ~/catkin_ws
# catkin config --install
# catkin build rospy_tutorials --cmake-args -DCATKIN_SYMLINK_INSTALL=ON

# ls -l ~/catkin_ws/install/share/rospy_tutorials/001_talker_listener
total 24
lrwxrwxrwx 1 root root 76 Jun 29 16:13 README -> /root/catkin_ws/src/ros_tutorials/rospy_tutorials/001_talker_listener/README
lrwxrwxrwx 1 root root 76 Jun 29 16:13 listener -> /root/catkin_ws/build/rospy_tutorials/catkin_generated/installspace/listener
lrwxrwxrwx 1 root root 79 Jun 29 16:13 listener.py -> /root/catkin_ws/build/rospy_tutorials/catkin_generated/installspace/listener.py
lrwxrwxrwx 1 root root 74 Jun 29 16:13 talker -> /root/catkin_ws/build/rospy_tutorials/catkin_generated/installspace/talker
lrwxrwxrwx 1 root root 77 Jun 29 16:13 talker.py -> /root/catkin_ws/build/rospy_tutorials/catkin_generated/installspace/talker.py
lrwxrwxrwx 1 root root 92 Jun 29 16:13 talker_listener.launch -> /root/catkin_ws/src/ros_tutorials/rospy_tutorials/001_talker_listener/talker_listener.launch

# ls -l /root/catkin_ws/build/rospy_tutorials/catkin_generated/installspace/listener.py
-rwxr-xr-x 1 root root 2407 Jun 29 16:13 /root/catkin_ws/build/rospy_tutorials/catkin_generated/installspace/listener.py

# /root/catkin_ws/build/rospy_tutorials/catkin_generated/installspace/listener.py
Unable to register with master node [http://localhost:11311]: master may not be running yet. Will keep trying.

#########################################################
##### Check result without this patch (not working) #####
#########################################################
# cd ~/catkin_ws
# catkin clean -y
# rm -rf ~/catkin_ws/src/catkin
# catkin build rospy_tutorials --cmake-args -DCATKIN_SYMLINK_INSTALL=ON

# ls -l ~/catkin_ws/install/share/rospy_tutorials/001_talker_listener
total 24
lrwxrwxrwx 1 root root 76 Jun 29 16:14 README -> /root/catkin_ws/src/ros_tutorials/rospy_tutorials/001_talker_listener/README
lrwxrwxrwx 1 root root 76 Jun 29 16:14 listener -> /root/catkin_ws/build/rospy_tutorials/catkin_generated/installspace/listener
lrwxrwxrwx 1 root root 79 Jun 29 16:14 listener.py -> /root/catkin_ws/build/rospy_tutorials/catkin_generated/installspace/listener.py
lrwxrwxrwx 1 root root 74 Jun 29 16:14 talker -> /root/catkin_ws/build/rospy_tutorials/catkin_generated/installspace/talker
lrwxrwxrwx 1 root root 77 Jun 29 16:14 talker.py -> /root/catkin_ws/build/rospy_tutorials/catkin_generated/installspace/talker.py
lrwxrwxrwx 1 root root 92 Jun 29 16:14 talker_listener.launch -> /root/catkin_ws/src/ros_tutorials/rospy_tutorials/001_talker_listener/talker_listener.launch

# ls -l /root/catkin_ws/build/rospy_tutorials/catkin_generated/installspace/listener.py
-rw-r--r-- 1 root root 2407 Jun 29 16:14 /root/catkin_ws/build/rospy_tutorials/catkin_generated/installspace/listener.py

# /root/catkin_ws/build/rospy_tutorials/catkin_generated/installspace/listener.py
bash: /root/catkin_ws/build/rospy_tutorials/catkin_generated/installspace/listener.py: Permission denied

Kenji Miyake added 2 commits June 29, 2020 12:57
Signed-off-by: Kenji Miyake <kenji.miyake@tier4.jp>
Signed-off-by: Kenji Miyake <kenji.miyake@tier4.jp>
@dirk-thomas
Copy link
Member

Please change the target branch to noetic-devel. Only after a patch has been reviewed, merged and released into Noetic it will be considered for backporting.

This seems to be related to the change in #1054. @sloretz Can you please take a look at this.

@sloretz sloretz self-assigned this Jun 29, 2020
@kenji-miyake kenji-miyake changed the base branch from kinetic-devel to noetic-devel June 29, 2020 16:04
@kenji-miyake
Copy link
Contributor Author

@dirk-thomas Thank you for your quick response! I've fixed my PR for noetic-devel.

Copy link
Contributor

@sloretz sloretz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

If I was going to nitpick, I would say this should set the permissions on the generated file to be executable instead of copying the source file permissions via USE_SOURCE_PERMISSIONS since that's what install(PROGRAMS) would do:

https://cmake.org/cmake/help/v3.5/command/install.html

The PROGRAMS form is identical to the FILES form except that the default permissions for the installed file also include OWNER_EXECUTE, GROUP_EXECUTE, and WORLD_EXECUTE.

@dirk-thomas
Copy link
Member

I would say this should set the permissions on the generated file to be executable instead of copying the source file permissions via USE_SOURCE_PERMISSIONS

@sloretz Do you have a suggestion how to do that? Afaik there is no way to set permissions on a file in CMake (beyond file(COPY ...) and install(...)).

@sloretz
Copy link
Contributor

sloretz commented Jul 11, 2020

Afaik there is no way to set permissions on a file in CMake (beyond file(COPY ...) and install(...)).

I think it could be done with file(COPY ... FILE_PERMISSIONS <permissions>...) which I think takes the same permission args as install(...) .

@dirk-thomas
Copy link
Member

I think it could be done with file(COPY ... FILE_PERMISSIONS <permissions>...) which I think takes the same permission args as install(...) .

@sloretz

The current patch already uses file(COPY ... USE_SOURCE_PERMISSIONS) and afterwards overwrites the content of the file which isn't pretty. I thought that was what your nitpick is about and should be improved (#1100 (review)).

I then asked if you can recommend a function to set the permissions without copying the file (#1100 (comment)).

@sloretz
Copy link
Contributor

sloretz commented Jul 13, 2020

The current patch already uses file(COPY ... USE_SOURCE_PERMISSIONS) and afterwards overwrites the content of the file which isn't pretty.

I'm proposing using FILE_PERMISSIONS, not USE_SOURCE_PERMISSIONS.

@dirk-thomas
Copy link
Member

Ok, the nitpick is not about the "hacky copy a file and then overwrite its content anyway" but about enforcing an executable bit - even if the source file doesn't have one.

In the case where the shebang line doesn't have to be rewritten the source file is symlinked and therefore must have the executable bit set already. I would rather not workaround a missing executable bit for the source file only in the case when the shebang line is rewritten.

Otherwise it would magically work when the executable bit is missing for the source file as long as the shebang line is rewritten but as soon as the shebang line is update to not require rewriting anymore the symlinked file wouldn't be executable anymore.

@dirk-thomas
Copy link
Member

I updated the patch to not use the variable name installspace for a path which is not in the install space: f7cb085. This also keep the diff much smaller.

I also added a comment to describe why the file(COPY ...) is done: 92216e0.

@dirk-thomas dirk-thomas changed the title Fix symlink install python Fix symlink install python when shebang line is rewritten Jul 13, 2020
@dirk-thomas
Copy link
Member

@kenji-miyake Thank you for the patch.

@dirk-thomas dirk-thomas merged commit 189598e into ros:noetic-devel Jul 13, 2020
@kenji-miyake
Copy link
Contributor Author

@dirk-thomas Thank you, too.

I updated the patch to not use the variable name installspace for a path which is not in the install space: f7cb085. This also keep the diff much smaller.

Yes, no problem. It's what I mentioned here. I also added a variable installspace in order to avoid overwriting the variable, but I can drop this change from this PR.

I also added a comment to describe why the file(COPY ...) is done: 92216e0.

Thank you, looks nice!

@kenji-miyake kenji-miyake deleted the fix-symlink-install-python-kinetic branch July 14, 2020 02:35
@kenji-miyake
Copy link
Contributor Author

@dirk-thomas By the way, will this be backported to melodic?

@dirk-thomas
Copy link
Member

By the way, will this be backported to melodic?

Yes, that is what the consider-backport label is used for.

dirk-thomas added a commit that referenced this pull request Jul 14, 2020
* Refactor catkin_install_python.cmake

Signed-off-by: Kenji Miyake <kenji.miyake@tier4.jp>

* Use source-permissions for rewritten_files

Signed-off-by: Kenji Miyake <kenji.miyake@tier4.jp>

* rename temp variable

* add comment

Co-authored-by: Dirk Thomas <dirk-thomas@users.noreply.github.com>
@dirk-thomas
Copy link
Member

Cherry-picked to kinetic-devel in 1cb2018.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants