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$ 
 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  # Global, usually set in setup 
 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  # Build-related 
 52  ROS_BINDEPS_PATH = 'ROS_BINDEPS_PATH' 
 53  ROS_BOOST_ROOT = 'ROS_BOOST_ROOT' 
 54   
 55  # Per session 
 56  # hostname/address to bind XML-RPC services to. 
 57  ROS_IP = 'ROS_IP' 
 58  ROS_HOSTNAME = 'ROS_HOSTNAME' 
 59  ROS_NAMESPACE = 'ROS_NAMESPACE' 
 60  # directory in which log files are written 
 61  ROS_LOG_DIR = 'ROS_LOG_DIR' 
 62   
 63   
64 -class ROSEnvException(Exception):
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
72 -def get_ros_root(required=True, env=None):
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
98 -def get_ros_package_path(required=False, env=None):
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
115 -def get_master_uri(required=True, env=None, argv=None):
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 # we ignore required here because there really is no 142 # correct return value as the configuration is bad 143 # rather than unspecified 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
153 -def get_ros_home(env=None):
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 # slightly more robust than $HOME 171 return os.path.join(os.path.expanduser('~'), '.ros')
172 173
174 -def get_log_dir(env=None):
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
194 -def get_test_results_dir(env=None):
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 # this is a copy of the roslogging utility. it's been moved here as it is a common 209 # routine for programs using accessing ROS directories
210 -def makedirs_with_parent_perms(p):
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 # recurse upwards, checking to make sure we haven't reached the 221 # top 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 # if perms of new dir don't match, set anew 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