Package roslib :: Module rosenv
[frames] | no frames]

Source Code for Module roslib.rosenv

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2008, Willow Garage, Inc. 
  4  # All rights reserved. 
  5  # 
  6  # Redistribution and use in source and binary forms, with or without 
  7  # modification, are permitted provided that the following conditions 
  8  # are met: 
  9  # 
 10  #  * Redistributions of source code must retain the above copyright 
 11  #    notice, this list of conditions and the following disclaimer. 
 12  #  * Redistributions in binary form must reproduce the above 
 13  #    copyright notice, this list of conditions and the following 
 14  #    disclaimer in the documentation and/or other materials provided 
 15  #    with the distribution. 
 16  #  * Neither the name of Willow Garage, Inc. nor the names of its 
 17  #    contributors may be used to endorse or promote products derived 
 18  #    from this software without specific prior written permission. 
 19  # 
 20  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 21  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 22  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 23  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 24  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 25  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 26  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 27  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 28  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 29  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 30  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  # POSSIBILITY OF SUCH DAMAGE. 
 32  # 
 33  # Revision $Id: rosenv.py 14291 2011-07-13 03:24:43Z kwc $ 
 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  # Global, usually set in setup 
 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  # Build-related 
 53  ROS_BINDEPS_PATH = "ROS_BINDEPS_PATH" 
 54  ROS_BOOST_ROOT = "ROS_BOOST_ROOT" 
 55   
 56  # Per session 
 57  ## hostname/address to bind XML-RPC services to.  
 58  ROS_IP           ="ROS_IP" 
 59  ROS_HOSTNAME     ="ROS_HOSTNAME" 
 60  ROS_NAMESPACE    ="ROS_NAMESPACE" 
 61  ## directory in which log files are written 
 62  ROS_LOG_DIR      ="ROS_LOG_DIR" 
 63  ## directory in which test result files are written 
 64  ROS_TEST_RESULTS_DIR = "ROS_TEST_RESULTS_DIR" 
 65   
66 -class ROSEnvException(roslib.exceptions.ROSLibException):
67 """Base class of roslib.rosenv errors.""" 68 pass
69
70 -def get_ros_root(required=True, env=None):
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 #Test: 92 # 1. Is a path 93 # 2. Is a directory 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
112 -def get_ros_package_path(required=False, env=None):
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
128 -def get_master_uri(required=True, env=None, argv=None):
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 # we ignore required here because there really is no 155 # correct return value as the configuration is bad 156 # rather than unspecified 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
165 -def resolve_path(p):
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
178 -def resolve_paths(paths):
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
191 -def get_ros_home(env=None):
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 #slightly more robust than $HOME 209 return os.path.join(os.path.expanduser('~'), '.ros')
210
211 -def get_log_dir(env=None):
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
230 -def get_test_results_dir(env=None):
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 # this is a copy of the roslogging utility. it's been moved here as it is a common 252 # routine for programs using accessing ROS directories
253 -def makedirs_with_parent_perms(p):
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 # recurse upwards, checking to make sure we haven't reached the 264 # top 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 # if perms of new dir don't match, set anew 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
277 -def on_ros_path(p):
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