1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 """
36 ROS environment variables as well as routines for determining
37 configuration values that have environment overrides
38 (e.g. ROS_LOG_DIR, ROS_HOME, ROS_TEST_RESULTS_DIR).
39 """
40
41 import os
42 import sys
43
44 import roslib.exceptions
45
46
47 ROS_ROOT = "ROS_ROOT"
48 ROS_MASTER_URI = "ROS_MASTER_URI"
49 ROS_PACKAGE_PATH = "ROS_PACKAGE_PATH"
50 ROS_HOME = "ROS_HOME"
51
52
53 ROS_BINDEPS_PATH = "ROS_BINDEPS_PATH"
54 ROS_BOOST_ROOT = "ROS_BOOST_ROOT"
55
56
57
58 ROS_IP ="ROS_IP"
59 ROS_HOSTNAME ="ROS_HOSTNAME"
60 ROS_NAMESPACE ="ROS_NAMESPACE"
61
62 ROS_LOG_DIR ="ROS_LOG_DIR"
63
64 ROS_TEST_RESULTS_DIR = "ROS_TEST_RESULTS_DIR"
65
67 """Base class of roslib.rosenv errors."""
68 pass
69
71 """
72 @param required: (default True). If True, ROS_ROOT must be set and point to a valid directory.
73 @type required: bool
74 @param env: override environment dictionary
75 @type env: dict
76 @raise ROSEnvException: if required is True and ROS_ROOT is not
77 set validly
78 """
79 if env is None:
80 env = os.environ
81 p = None
82 try:
83 if ROS_ROOT not in env:
84 raise ROSEnvException("""
85 The %(ROS_ROOT)s environment variable has not been set.
86 Please set to the location of your ROS installation
87 before continuing.
88 """%globals())
89
90 p = env[ROS_ROOT]
91
92
93
94 if not os.path.exists(p):
95 raise ROSEnvException("""
96 The %s environment variable has not been set properly:
97 %s does not exist.
98 Please update your ROS installation before continuing.
99 """%(ROS_ROOT, p))
100 if not os.path.isdir(p):
101 raise ROSEnvException("""
102 The %s environment variable has not been set properly:
103 %s is not a directory.
104 Please update your ROS installation before continuing.
105 """%(ROS_ROOT, p))
106 return p
107 except Exception as e:
108 if required:
109 raise
110 return p
111
113 """
114 @param required: (default False) if True, ROS_PACKAGE_PATH must be
115 set and point to a valid directory.
116 @type required: bool
117 @raise ROSEnvException: if ROS_PACKAGE_PATH is not set and \a
118 required is True
119 """
120 if env is None:
121 env = os.environ
122 try:
123 return env[ROS_PACKAGE_PATH]
124 except KeyError as e:
125 if required:
126 raise ROSEnvException("%s has not been configured"%ROS_PACKAGE_PATH)
127
129 """
130 Get the ROS_MASTER_URI setting from the command-line args or
131 environment, command-line args takes precedence.
132 @param required: if True, enables exception raising
133 @type required: bool
134 @param env: override environment dictionary
135 @type env: dict
136 @param argv: override sys.argv
137 @type argv: [str]
138 @raise ROSEnvException: if ROS_MASTER_URI value is invalidly
139 specified or if required and ROS_MASTER_URI is not set
140 """
141 if env is None:
142 env = os.environ
143 if argv is None:
144 argv = sys.argv
145 try:
146 for arg in argv:
147 if arg.startswith('__master:='):
148 val = None
149 try:
150 _, val = arg.split(':=')
151 except:
152 pass
153
154
155
156
157 if not val:
158 raise ROSEnvException("__master remapping argument '%s' improperly specified"%arg)
159 return val
160 return env[ROS_MASTER_URI]
161 except KeyError as e:
162 if required:
163 raise ROSEnvException("%s has not been configured"%ROS_MASTER_URI)
164
166 """
167 @param path: path string
168 @type path: str
169 Catch-all utility routine for fixing ROS environment variables that
170 are a single path (e.g. ROS_ROOT). Currently this just expands
171 tildes to home directories, but in the future it may encode other
172 behaviors.
173 """
174 if p and p[0] == '~':
175 return os.path.expanduser(p)
176 return p
177
179 """
180 @param paths: path string with OS-defined separator (i.e. ':' for Linux)
181 @type paths: str
182 Catch-all utility routine for fixing ROS environment variables that
183 are paths (e.g. ROS_PACKAGE_PATH). Currently this just expands
184 tildes to home directories, but in the future it may encode other
185 behaviors.
186 """
187 splits = [p for p in paths.split(os.pathsep) if p]
188 return os.pathsep.join([resolve_path(p) for p in splits])
189
190
192 """
193 Get directory location of '.ros' directory (aka ROS home).
194 possible locations for this. The ROS_LOG_DIR environment variable
195 has priority. If that is not set, then ROS_HOME/log is used. If
196 ROS_HOME is not set, $HOME/.ros/log is used.
197
198 @param env: override os.environ dictionary
199 @type env: dict
200 @return: path to use use for log file directory
201 @rtype: str
202 """
203 if env is None:
204 env = os.environ
205 if ROS_HOME in env:
206 return env[ROS_HOME]
207 else:
208
209 return os.path.join(os.path.expanduser('~'), '.ros')
210
212 """
213 Get directory to use for writing log files. There are multiple
214 possible locations for this. The ROS_LOG_DIR environment variable
215 has priority. If that is not set, then ROS_HOME/log is used. If
216 ROS_HOME is not set, $HOME/.ros/log is used.
217
218 @param env: override os.environ dictionary
219 @type env: dict
220 @return: path to use use for log file directory
221 @rtype: str
222 """
223 if env is None:
224 env = os.environ
225 if ROS_LOG_DIR in env:
226 return env[ROS_LOG_DIR]
227 else:
228 return os.path.join(get_ros_home(env), 'log')
229
231 """
232 Get directory to use for writing test result files. There are multiple
233 possible locations for this. The ROS_TEST_RESULTS_DIR environment variable
234 has priority. If that is set, ROS_TEST_RESULTS_DIR is returned.
235 If ROS_TEST_RESULTS_DIR is not set, then ROS_HOME/test_results is used. If
236 ROS_HOME is not set, $HOME/.ros/test_results is used.
237
238 @param env: environment dictionary (defaults to os.environ)
239 @type env: dict
240 @return: path to use use for log file directory
241 @rtype: str
242 """
243 if env is None:
244 env = os.environ
245
246 if ROS_TEST_RESULTS_DIR in env:
247 return env[ROS_TEST_RESULTS_DIR]
248 else:
249 return os.path.join(get_ros_home(env), 'test_results')
250
251
252
254 """
255 Create the directory using the permissions of the nearest
256 (existing) parent directory. This is useful for logging, where a
257 root process sometimes has to log in the user's space.
258 @param p: directory to create
259 @type p: str
260 """
261 p = os.path.abspath(p)
262 parent = os.path.dirname(p)
263
264
265 if not os.path.exists(p) and p and parent != p:
266 makedirs_with_parent_perms(parent)
267 s = os.stat(parent)
268 os.mkdir(p)
269
270
271 s2 = os.stat(p)
272 if s.st_uid != s2.st_uid or s.st_gid != s2.st_gid:
273 os.chown(p, s.st_uid, s.st_gid)
274 if s.st_mode != s2.st_mode:
275 os.chmod(p, s.st_mode)
276
278 """
279 Check to see if filesystem path is on paths specified in ROS
280 environment (ROS_ROOT, ROS_PACKAGE_PATH).
281
282 New in ROS 1.2.
283
284 @param p: path
285 @type p: str
286 @return: True if p is on the ROS path (ROS_ROOT, ROS_PACKAGE_PATH)
287 """
288 pkg = os.path.realpath(roslib.rosenv.resolve_path(p))
289 paths = [p for p in roslib.packages.get_package_paths()]
290 paths = [os.path.realpath(roslib.rosenv.resolve_path(x)) for x in paths]
291 return bool([x for x in paths if pkg == x or pkg.startswith(x + os.sep)])
292