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