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