diff --git a/tools/rosbash/CMakeLists.txt b/tools/rosbash/CMakeLists.txt index 647df533..1b70dd3d 100644 --- a/tools/rosbash/CMakeLists.txt +++ b/tools/rosbash/CMakeLists.txt @@ -8,6 +8,9 @@ install(FILES rosbash rosfish rostcsh roszsh if (WIN32) install(PROGRAMS scripts/rosrun.bat DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}) + + install(PROGRAMS scripts/rosfindpath.py scripts/roscd.bat scripts/rosls.bat + DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}) else() install(PROGRAMS scripts/rosrun DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}) diff --git a/tools/rosbash/scripts/roscd.bat b/tools/rosbash/scripts/roscd.bat new file mode 100644 index 00000000..c936d1ef --- /dev/null +++ b/tools/rosbash/scripts/roscd.bat @@ -0,0 +1,52 @@ +@echo off + +if "%1" equ "--help" goto :usage +if not "%2" equ "" goto :usage + +if "%1" equ "" ( + if not "%ROS_WORKSPACE%" equ "" ( + cd /d %ROS_WORKSPACE% + exit /b 0 + ) + + if not "%CMAKE_PREFIX_PATH%" equ "" ( + for %%a in ("%CMAKE_PREFIX_PATH:;=";"%") do ( + if exist %%a\.catkin ( + cd /d %%a + exit /b 0 + ) + ) + ) + + echo Neither ROS_WORKSPACE is set nor a catkin workspace is listed in CMAKE_PREFIX_PATH. Please set ROS_WORKSPACE or source a catkin workspace to use roscd with no arguments. + exit /b 1 +) + +for /f "tokens=*" %%g in ('call python %~dp0\findpath.py %1 forceeval') do ( + set target_path=%%~g +) + +if /i "%target_path:~0,7%"=="Error: " ( + echo roscd: %target_path:~7% + exit /b 1 +) + +if "%target_path%" equ "" ( + if not "%ROS_WORKSPACE%" equ "" ( + cd /d %ROS_WORKSPACE% + exit /b 0 + ) + + echo No ROS_WORKSPACE set. Please set ROS_WORKSPACE to use roscd with no arguments. + exit /b 1 +) + +cd /d "%target_path%" +exit /b 0 + +:usage +echo usage: roscd package +echo. +echo. +echo Jump to target package. +exit /b 0 diff --git a/tools/rosbash/scripts/rosfindpath.py b/tools/rosbash/scripts/rosfindpath.py new file mode 100644 index 00000000..59fb00aa --- /dev/null +++ b/tools/rosbash/scripts/rosfindpath.py @@ -0,0 +1,79 @@ +from __future__ import print_function + +import os +import subprocess +import sys + +ROS_CACHE_TIMEOUT_ENV_NAME = 'ROS_CACHE_TIMEOUT' +ROS_LOCATIONS_ENV_NAME = 'ROS_LOCATIONS' +ROS_LOCATION_SEP = ';' +ERROR_PREFIX = 'Error: ' + +def ros_location_find(package_name): + # process ROS_LOCATION and return if found + ros_location = os.environ.get(ROS_LOCATIONS_ENV_NAME) + if ros_location is not None: + locations = ros_location.split(ROS_LOCATION_SEP) + for loc in locations: + index = loc.find('=') + if index != -1: + if package_name == loc[:index]: + return 0, loc[index + 1:] + + if package_name == 'log': + p = subprocess.Popen('roslaunch-logs', stdout=subprocess.PIPE) + result_location = p.communicate()[0].strip() + result_code = p.returncode + return result_code, result_location if result_code == 0 else '' + + if package_name == 'test_results': + p = subprocess.Popen('rosrun.bat rosunit test_results_dir.py', stdout=subprocess.PIPE) + result_location = p.communicate()[0].strip() + result_code = p.returncode + return result_code, result_location if result_code == 0 else '' + + # process package_name and return + env = os.environ + env[ROS_CACHE_TIMEOUT_ENV_NAME] = '-1.0' + p = subprocess.Popen(['rospack', 'find', package_name], stdout=subprocess.PIPE) + result_location = p.communicate()[0].strip() + result_code = p.returncode + if result_code == 0: + return result_code, result_location + + p = subprocess.Popen(['rosstack', 'find', package_name], stdout=subprocess.PIPE) + result_location = p.communicate()[0].strip() + result_code = p.returncode + if result_code == 0: + return result_code, result_location + + # package not found + return result_code, '' + +# takes as argument either just a package-path or just a pkgname +# returns 0 for no argument or if package (+ path) exist, 1 else +# on success with arguments print result_path or Error: error message +def findpathmain(argv): + reldir = '' + parameters = os.path.normpath(argv[0]).split(os.path.sep) + package_name = parameters[0] + if len(parameters) > 1: + reldir = os.path.sep.join(parameters[1:]) + else: + if len(argv) < 2 or argv[1] != 'forceeval': + print(ERROR_PREFIX + '[' + package_name + '] is not a valid argument!', file=sys.stderr) + return 1 + + error_code, package_dir = ros_location_find(package_name) + if error_code != 0: + print(ERROR_PREFIX + '[' + package_name + '] not found!', file=sys.stderr) + return error_code + else: + rosdir = os.path.normpath(os.path.sep.join([package_dir, reldir])) + print(rosdir) + return 0 + +if __name__ == '__main__': + if len(sys.argv) < 2: + sys.exit(1) + sys.exit(findpathmain(sys.argv[1:])) diff --git a/tools/rosbash/scripts/rosls.bat b/tools/rosbash/scripts/rosls.bat new file mode 100644 index 00000000..a5b99c1f --- /dev/null +++ b/tools/rosbash/scripts/rosls.bat @@ -0,0 +1,21 @@ +@echo off + +if "%1" equ "--help" goto :usage +for /f "tokens=*" %%g in ('call python %~dp0\findpath.py %1 forceeval') do ( + set target_path=%%~g +) + +if /i "%target_path:~0,7%"=="Error: " ( + echo rosls: %target_path:~7% + exit /b 1 +) + +dir "%target_path%" +exit /b 0 + +:usage +echo usage: rosls [package] +echo. +echo. +echo Lists contents of a package directory. +exit /b 0