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 Warning: do not use this library. It is unstable and most of the routines
37 here have been superceded by other libraries (e.g. rospkg). These
38 routines will likely be *deleted* in future releases.
39 """
40
41 import os
42 import sys
43 import warnings
44
45
46 ROS_ROOT = 'ROS_ROOT'
47 ROS_MASTER_URI = 'ROS_MASTER_URI'
48 ROS_PACKAGE_PATH = 'ROS_PACKAGE_PATH'
49 ROS_HOME = 'ROS_HOME'
50
51
52 ROS_BINDEPS_PATH = 'ROS_BINDEPS_PATH'
53 ROS_BOOST_ROOT = 'ROS_BOOST_ROOT'
54
55
56
57 ROS_IP = 'ROS_IP'
58 ROS_HOSTNAME = 'ROS_HOSTNAME'
59 ROS_NAMESPACE = 'ROS_NAMESPACE'
60
61 ROS_LOG_DIR = 'ROS_LOG_DIR'
62
63
65 """Base class of roslib.rosenv errors."""
66 pass
67
68
69 warnings.warn('roslib.rosenv is deprecated, please use rospkg or rosgraph.rosenv', stacklevel=2)
70
71
73 """
74 @param required: (default True). If True, ROS_ROOT must be set and point to a valid directory.
75 @type required: bool
76 @param env: override environment dictionary
77 @type env: dict
78 @raise ROSEnvException: if required is True and ROS_ROOT is not set
79 """
80 if env is None:
81 env = os.environ
82 p = None
83 try:
84 if ROS_ROOT not in env:
85 raise ROSEnvException("""
86 The %(ROS_ROOT)s environment variable has not been set.
87 Please set to the location of your ROS installation
88 before continuing.
89 """ % globals())
90
91 return env[ROS_ROOT]
92 except Exception:
93 if required:
94 raise
95 return p
96
97
99 """
100 @param required: (default False) if True, ROS_PACKAGE_PATH must be
101 set and point to a valid directory.
102 @type required: bool
103 @raise ROSEnvException: if ROS_PACKAGE_PATH is not set and \a
104 required is True
105 """
106 if env is None:
107 env = os.environ
108 try:
109 return env[ROS_PACKAGE_PATH]
110 except KeyError:
111 if required:
112 raise ROSEnvException('%s has not been configured' % ROS_PACKAGE_PATH)
113
114
116 """
117 Get the ROS_MASTER_URI setting from the command-line args or
118 environment, command-line args takes precedence.
119 @param required: if True, enables exception raising
120 @type required: bool
121 @param env: override environment dictionary
122 @type env: dict
123 @param argv: override sys.argv
124 @type argv: [str]
125 @raise ROSEnvException: if ROS_MASTER_URI value is invalidly
126 specified or if required and ROS_MASTER_URI is not set
127 """
128 if env is None:
129 env = os.environ
130 if argv is None:
131 argv = sys.argv
132 try:
133 for arg in argv:
134 if arg.startswith('__master:='):
135 val = None
136 try:
137 _, val = arg.split(':=')
138 except Exception:
139 pass
140
141
142
143
144 if not val:
145 raise ROSEnvException("__master remapping argument '%s' improperly specified" % arg)
146 return val
147 return env[ROS_MASTER_URI]
148 except KeyError:
149 if required:
150 raise ROSEnvException('%s has not been configured' % ROS_MASTER_URI)
151
152
154 """
155 Get directory location of '.ros' directory (aka ROS home).
156 possible locations for this. The ROS_LOG_DIR environment variable
157 has priority. If that is not set, then ROS_HOME/log is used. If
158 ROS_HOME is not set, $HOME/.ros/log is used.
159
160 @param env: override os.environ dictionary
161 @type env: dict
162 @return: path to use use for log file directory
163 @rtype: str
164 """
165 if env is None:
166 env = os.environ
167 if ROS_HOME in env:
168 return env[ROS_HOME]
169 else:
170
171 return os.path.join(os.path.expanduser('~'), '.ros')
172
173
175 """
176 Get directory to use for writing log files. There are multiple
177 possible locations for this. The ROS_LOG_DIR environment variable
178 has priority. If that is not set, then ROS_HOME/log is used. If
179 ROS_HOME is not set, $HOME/.ros/log is used.
180
181 @param env: override os.environ dictionary
182 @type env: dict
183 @return: path to use use for log file directory
184 @rtype: str
185 """
186 if env is None:
187 env = os.environ
188 if ROS_LOG_DIR in env:
189 return env[ROS_LOG_DIR]
190 else:
191 return os.path.join(get_ros_home(env), 'log')
192
193
195 """
196 Get directory to use for writing test result files. There are multiple
197 possible locations for this. If ROS_HOME is set ROS_HOME/test_results
198 is used. Otherwise $HOME/.ros/test_results is used.
199
200 @param env: environment dictionary (defaults to os.environ)
201 @type env: dict
202 @return: path to use use for log file directory
203 @rtype: str
204 """
205 return os.path.join(get_ros_home(env), 'test_results')
206
207
208
209
211 """
212 Create the directory using the permissions of the nearest
213 (existing) parent directory. This is useful for logging, where a
214 root process sometimes has to log in the user's space.
215 @param p: directory to create
216 @type p: str
217 """
218 p = os.path.abspath(p)
219 parent = os.path.dirname(p)
220
221
222 if not os.path.exists(p) and p and parent != p:
223 makedirs_with_parent_perms(parent)
224 s = os.stat(parent)
225 os.mkdir(p)
226
227
228 s2 = os.stat(p)
229 if s.st_uid != s2.st_uid or s.st_gid != s2.st_gid:
230 os.chown(p, s.st_uid, s.st_gid)
231 if s.st_mode != s2.st_mode:
232 os.chmod(p, s.st_mode)
233