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
14
16 '''
17 Returns the ROS HOME depending on ROS distribution API.
18 @return: ROS HOME path
19 @rtype: C{str}
20 '''
21 try:
22 import rospkg.distro
23 distro = rospkg.distro.current_distro_codename()
24 if distro in ['electric', 'diamondback', 'cturtle']:
25 import roslib.rosenv
26 return roslib.rosenv.get_ros_home()
27 else:
28 from rospkg import get_ros_home
29 return get_ros_home()
30 except:
31 from roslib import rosenv
32 return rosenv.get_ros_home()
33
34
36 '''
37 Returns the master URI depending on ROS distribution API.
38 @return: ROS master URI
39 @rtype: C{str}
40 '''
41 try:
42 import rospkg.distro
43 distro = rospkg.distro.current_distro_codename()
44 if distro in ['electric', 'diamondback', 'cturtle']:
45 import roslib.rosenv
46 return roslib.rosenv.get_master_uri()
47 else:
48 import rosgraph
49 return rosgraph.rosenv.get_master_uri()
50 except:
51 return os.environ['ROS_MASTER_URI']
52
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
72
74 '''
75 Searches in text for $(find ...) statements and replaces it by the package path.
76 @return: text with replaced statements.
77 '''
78 result = text
79 startIndex = text.find('$(')
80 if startIndex > -1:
81 endIndex = text.find(')', startIndex + 2)
82 script = text[startIndex + 2: endIndex].split()
83 if len(script) == 2 and (script[0] == 'find'):
84 pkg = ''
85 try:
86 from rospkg import RosPack
87 rp = RosPack()
88 pkg = rp.get_path(script[1])
89 except:
90 import roslib
91 pkg = roslib.packages.get_pkg_dir(script[1])
92 return result.replace(text[startIndex: endIndex + 1], pkg)
93 return result
94
95
97 '''
98 Returns for given directory a tuple of package name and package path or None values.
99 The results are cached!
100 @rtype: C{(name, path)}
101 '''
102 if not (path is None) and path and path != os.path.sep and os.path.isdir(path):
103 if path in PACKAGE_CACHE:
104 return PACKAGE_CACHE[path]
105 package = os.path.basename(path)
106 fileList = os.listdir(path)
107 for f in fileList:
108 if f == MANIFEST_FILE:
109 PACKAGE_CACHE[path] = (package, path)
110 return (package, path)
111 if CATKIN_SUPPORTED and f == PACKAGE_FILE:
112 try:
113 pkg = parse_package(os.path.join(path, f))
114 PACKAGE_CACHE[path] = (pkg.name, path)
115 return (pkg.name, path)
116 except:
117 return (None, None)
118 PACKAGE_CACHE[path] = package_name(os.path.dirname(path))
119 return PACKAGE_CACHE[path]
120 return (None, None)
121
122
125