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


rosbash
Author(s): Jeremy Leibs, Thibault Kruse
autogenerated on Thu Apr 30 2020 06:30:12