1 import os
2
3 MANIFEST_FILE = 'manifest.xml'
4 PACKAGE_FILE = 'package.xml'
5
6 try:
7 from catkin_pkg.package import parse_package
8 CATKIN_SUPPORTED = True
9 except ImportError:
10 CATKIN_SUPPORTED = False
11
12 PACKAGE_CACHE = {}
13
15 '''
16 Returns the ROS HOME depending on ROS distribution API.
17 @return: ROS HOME path
18 @rtype: C{str}
19 '''
20 try:
21 import rospkg.distro
22 distro = rospkg.distro.current_distro_codename()
23 if distro in ['electric', 'diamondback', 'cturtle']:
24 import roslib.rosenv
25 return roslib.rosenv.get_ros_home()
26 else:
27 from rospkg import get_ros_home
28 return get_ros_home()
29 except:
30
31
32 from roslib import rosenv
33 return rosenv.get_ros_home()
34
35
37 '''
38 Returns the master URI depending on ROS distribution API.
39 @return: ROS master URI
40 @rtype: C{str}
41 '''
42 try:
43 import rospkg.distro
44 distro = rospkg.distro.current_distro_codename()
45 if distro in ['electric', 'diamondback', 'cturtle']:
46 import roslib.rosenv
47 return roslib.rosenv.get_master_uri()
48 else:
49 import rosgraph
50 return rosgraph.rosenv.get_master_uri()
51 except:
52 return os.environ['ROS_MASTER_URI']
53
55 result = {}
56 if os.path.isdir(path):
57 fileList = os.listdir(path)
58 if MANIFEST_FILE in fileList:
59 return {os.path.basename(path) : path}
60 if CATKIN_SUPPORTED and PACKAGE_FILE in fileList:
61 try:
62 pkg = parse_package(path)
63 return {pkg.name : path}
64 except:
65 pass
66 return {}
67 for f in fileList:
68 ret = get_packages(os.path.join(path, f))
69 result = dict(ret.items() + result.items())
70 return result
71
73 '''
74 Searches in text for $(find ...) statements and replaces it by the package path.
75 @return: text with replaced statements.
76 '''
77 result = text
78 startIndex = text.find('$(')
79 if startIndex > -1:
80 endIndex = text.find(')', startIndex+2)
81 script = text[startIndex+2:endIndex].split()
82 if len(script) == 2 and (script[0] == 'find'):
83 pkg = ''
84 try:
85 from rospkg import RosPack
86 rp = RosPack()
87 pkg = rp.get_path(script[1])
88 except:
89 import roslib
90 pkg = roslib.packages.get_pkg_dir(script[1])
91 return result.replace(text[startIndex:endIndex+1], pkg)
92 return result
93
95 '''
96 Returns for given directory a tuple of package name and package path or None values.
97 The results are cached!
98 @rtype: C{(name, path)}
99 '''
100 if not (path is None) and path and path != os.path.sep and os.path.isdir(path):
101 if PACKAGE_CACHE.has_key(path):
102 return PACKAGE_CACHE[path]
103 package = os.path.basename(path)
104 fileList = os.listdir(path)
105 for f in fileList:
106 if f == MANIFEST_FILE:
107 PACKAGE_CACHE[path] = (package, path)
108 return (package, path)
109 if CATKIN_SUPPORTED and f == PACKAGE_FILE:
110 try:
111 pkg = parse_package(os.path.join(path, f))
112 PACKAGE_CACHE[path] = (pkg.name, path)
113 return (pkg.name, path)
114 except:
115 return (None,None)
116 PACKAGE_CACHE[path] = package_name(os.path.dirname(path))
117 return PACKAGE_CACHE[path]
118 return (None, None)
119
122