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   
 63   
64 -class ROSEnvException(Exception):
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
71 -def get_ros_root(required=True, env=None):
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
96 -def get_ros_package_path(required=False, env=None):
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
112 -def get_master_uri(required=True, env=None, argv=None):
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 # we ignore required here because there really is no 139 # correct return value as the configuration is bad 140 # rather than unspecified 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
149 -def get_ros_home(env=None):
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 #slightly more robust than $HOME 167 return os.path.join(os.path.expanduser('~'), '.ros')
168
169 -def get_log_dir(env=None):
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
188 -def get_test_results_dir(env=None):
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 # this is a copy of the roslogging utility. it's been moved here as it is a common 202 # routine for programs using accessing ROS directories
203 -def makedirs_with_parent_perms(p):
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 # recurse upwards, checking to make sure we haven't reached the 214 # top 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 # if perms of new dir don't match, set anew 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