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  """ 
 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  # 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  ## directory in which test result files are written 
 63  CATKIN_TEST_RESULTS_DIR = "CATKIN_TEST_RESULTS_DIR" 
 64   
65 -class ROSEnvException(Exception):
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
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 as e: 93 if required: 94 raise 95 return p
96
97 -def get_ros_package_path(required=False, env=None):
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
113 -def get_master_uri(required=True, env=None, argv=None):
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 # we ignore required here because there really is no 140 # correct return value as the configuration is bad 141 # rather than unspecified 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
150 -def get_ros_home(env=None):
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 #slightly more robust than $HOME 168 return os.path.join(os.path.expanduser('~'), '.ros')
169
170 -def get_log_dir(env=None):
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
189 -def get_test_results_dir(env=None):
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 # this is a copy of the roslogging utility. it's been moved here as it is a common 211 # routine for programs using accessing ROS directories
212 -def makedirs_with_parent_perms(p):
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 # recurse upwards, checking to make sure we haven't reached the 223 # top 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 # if perms of new dir don't match, set anew 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