rosfindpath.py
Go to the documentation of this file.
1 from __future__ import print_function
2 
3 import os
4 import subprocess
5 import sys
6 
7 ROS_CACHE_TIMEOUT_ENV_NAME = 'ROS_CACHE_TIMEOUT'
8 ROS_LOCATIONS_ENV_NAME = 'ROS_LOCATIONS'
9 ROS_LOCATION_SEP = ';'
10 ERROR_PREFIX = 'Error: '
11 
12 def ros_location_find(package_name):
13  # process ROS_LOCATION and return if found
14  ros_location = os.environ.get(ROS_LOCATIONS_ENV_NAME)
15  if ros_location is not None:
16  locations = ros_location.split(ROS_LOCATION_SEP)
17  for loc in locations:
18  index = loc.find('=')
19  if index != -1:
20  if package_name == loc[:index]:
21  return 0, loc[index + 1:]
22 
23  if package_name == 'log':
24  p = subprocess.Popen('roslaunch-logs', stdout=subprocess.PIPE)
25  result_location = p.communicate()[0].strip()
26  result_code = p.returncode
27  return result_code, result_location if result_code == 0 else ''
28 
29  if package_name == 'test_results':
30  p = subprocess.Popen('rosrun.bat rosunit test_results_dir.py', stdout=subprocess.PIPE)
31  result_location = p.communicate()[0].strip()
32  result_code = p.returncode
33  return result_code, result_location if result_code == 0 else ''
34 
35  # process package_name and return
36  env = os.environ
37  env[ROS_CACHE_TIMEOUT_ENV_NAME] = '-1.0'
38  p = subprocess.Popen(['rospack', 'find', package_name], stdout=subprocess.PIPE)
39  result_location = p.communicate()[0].strip()
40  result_code = p.returncode
41  if result_code == 0:
42  return result_code, result_location
43 
44  p = subprocess.Popen(['rosstack', 'find', package_name], stdout=subprocess.PIPE)
45  result_location = p.communicate()[0].strip()
46  result_code = p.returncode
47  if result_code == 0:
48  return result_code, result_location
49 
50  # package <package_name> not found
51  return result_code, ''
52 
53 # takes as argument either just a package-path or just a pkgname
54 # returns 0 for no argument or if package (+ path) exist, 1 else
55 # on success with arguments print result_path or Error: error message
56 def findpathmain(argv):
57  reldir = ''
58  parameters = os.path.normpath(argv[0]).split(os.path.sep)
59  package_name = parameters[0]
60  if len(parameters) > 1:
61  reldir = os.path.sep.join(parameters[1:])
62  else:
63  if len(argv) < 2 or argv[1] != 'forceeval':
64  print(ERROR_PREFIX + '[' + package_name + '] is not a valid argument!', file=sys.stderr)
65  return 1
66 
67  error_code, package_dir = ros_location_find(package_name)
68  if error_code != 0:
69  print(ERROR_PREFIX + '[' + package_name + '] not found!', file=sys.stderr)
70  return error_code
71  else:
72  rosdir = os.path.normpath(os.path.sep.join([package_dir, reldir]))
73  print(rosdir)
74  return 0
75 
76 if __name__ == '__main__':
77  if len(sys.argv) < 2:
78  sys.exit(1)
79  sys.exit(findpathmain(sys.argv[1:]))
def findpathmain(argv)
Definition: rosfindpath.py:56
def ros_location_find(package_name)
Definition: rosfindpath.py:12


rosbash
Author(s): Jeremy Leibs, Thibault Kruse
autogenerated on Tue Apr 2 2019 02:14:11