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

change how commands are executed #1628

Merged
merged 7 commits into from
Mar 16, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion tools/roslaunch/src/roslaunch/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

import errno
import os
import sys
from copy import deepcopy

import yaml
Expand Down Expand Up @@ -491,7 +492,41 @@ def param_value(self, verbose, name, ptype, value, textfile, binfile, command):
print("... executing command param [%s]" % command)
import subprocess, shlex #shlex rocks
try:
p = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE)
if os.name != 'nt':
command = shlex.split(command)
else:
# Python scripts in ROS tend to omit .py extension since they could become executable with shebang line
# special handle the use of Python scripts in Windows environment:
# 1. search for a wrapper executable (of the same name) under the same directory with stat.S_IXUSR flag
# 2. if no wrapper is present, prepend command with 'python' executable

cl = shlex.split(command, posix=False) # use non-posix method on Windows
if os.path.isabs(cl[0]):
# trying to launch an executable from a specific location(package), e.g. xacro
import stat
rx_flag = stat.S_IRUSR | stat.S_IXUSR
if not os.path.exists(cl[0]) or os.stat(cl[0]).st_mode & rx_flag != rx_flag:
d = os.path.dirname(cl[0])
files_of_same_name = [
os.path.join(d, f) for f in os.listdir(d)
if os.path.splitext(f)[0].lower() == os.path.splitext(os.path.basename(cl[0]))[0].lower()
] if os.path.exists(d) else []
executable_command = None
for f in files_of_same_name:
if os.stat(f).st_mode & rx_flag == rx_flag:
# found an executable wrapper of the desired Python script
executable_command = f

if not executable_command:
for f in files_of_same_name:
mode = os.stat(f).st_mode
if (mode & stat.S_IRUSR == stat.S_IRUSR) and (mode & stat.S_IXUSR != stat.S_IXUSR):
# when there is read permission but not execute permission, this is typically a Python script (in ROS)
if os.path.splitext(f)[1].lower() in ['.py', '']:
executable_command = ' '.join([sys.executable, f])
if executable_command:
command = command.replace(cl[0], executable_command, 1)
p = subprocess.Popen(command, stdout=subprocess.PIPE)
c_value = p.communicate()[0]
if not isinstance(c_value, str):
c_value = c_value.decode('utf-8')
Expand Down