Package roshlaunch :: Module rlutil
[frames] | no frames]

Source Code for Module roshlaunch.rlutil

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2009, 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: rlutil.py 10921 2010-09-02 22:24:09Z kwc $ 
 34   
 35  """ 
 36  Uncategorized utility routines for roslaunch. 
 37   
 38  This API should not be considered stable. 
 39  """ 
 40   
 41  import os 
 42  import time 
 43   
 44  from roslib.names import SEP 
 45   
 46  import roslaunch.core 
 47  try: 
 48      from rosmaster import DEFAULT_MASTER_PORT 
 49  except: 
 50      # backwards compatibility with cturtle 
 51      DEFAULT_MASTER_PORT = 11311 
 52   
53 -def check_log_disk_usage():
54 """ 55 Check size of log directory. If high, print warning to user 56 """ 57 try: 58 import rosclean 59 import roslib.rosenv 60 d = roslib.rosenv.get_log_dir() 61 roslaunch.core.printlog("Checking log directory for disk usage. This may take awhile.\nPress Ctrl-C to interrupt") 62 disk_usage = rosclean.get_disk_usage(d) 63 # warn if over a gig 64 if disk_usage > 1073741824: 65 roslaunch.core.printerrlog("WARNING: disk usage in log directory [%s] is over 1GB.\nIt's recommended that you use the 'rosclean' command."%d) 66 else: 67 roslaunch.core.printlog("Done checking log file disk usage. Usage is <1GB.") 68 except: 69 pass
70
71 -def resolve_launch_arguments(args):
72 """ 73 Resolve command-line args to roslaunch filenames. 74 @return: resolved filenames 75 @rtype: [str] 76 """ 77 import roslib.packages 78 import roslib.scriptutil 79 80 # strip remapping args for processing 81 args = roslib.scriptutil.myargv(args) 82 83 # user can either specify: 84 # - filename + launch args 85 # - package + relative-filename + launch args 86 if not args: 87 return args 88 resolved_args = None 89 top = args[0] 90 if os.path.isfile(top): 91 resolved_args = [top] + args[1:] 92 elif len(args) == 1: 93 raise roslaunch.core.RLException("[%s] does not exist. please specify a package and launch file"%(top)) 94 else: 95 try: 96 resolved = roslib.packages.find_resource(top, args[1]) 97 if len(resolved) == 1: 98 resolved = resolved[0] 99 elif len(resolved) > 1: 100 raise roslaunch.core.RLException("multiple files named [%s] in package [%s].\nPlease specify full path instead"%(args[1], top)) 101 except roslib.packages.InvalidROSPkgException, e: 102 raise roslaunch.core.RLException("[%s] is not a package or launch file name"%top) 103 if not resolved: 104 raise roslaunch.core.RLException("cannot locate [%s] in package [%s]"%(args[1], top)) 105 else: 106 resolved_args = [resolved] + args[2:] 107 return resolved_args
108
109 -def _wait_for_master():
110 """ 111 Block until ROS Master is online 112 113 @raise RuntimeError: if unexpected error occurs 114 """ 115 m = roslaunch.core.Master() # get a handle to the default master 116 is_running = m.is_running() 117 if not is_running: 118 roslaunch.core.printlog("roscore/master is not yet running, will wait for it to start") 119 while not is_running: 120 time.sleep(0.1) 121 is_running = m.is_running() 122 if is_running: 123 roslaunch.core.printlog("master has started, initiating launch") 124 else: 125 raise RuntimeError("unknown error waiting for master to start")
126 127 _terminal_name = None 128
129 -def _set_terminal(s):
130 import platform 131 if platform.system() in ['FreeBSD', 'Linux', 'Darwin', 'Unix']: 132 try: 133 print '\033]2;%s\007'%(s) 134 except: 135 pass
136
137 -def update_terminal_name(ros_master_uri):
138 """ 139 append master URI to the terminal name 140 """ 141 if _terminal_name: 142 _set_terminal(_terminal_name + ' ' + ros_master_uri)
143
144 -def change_terminal_name(args, is_core):
145 """ 146 use echo (where available) to change the name of the terminal window 147 """ 148 global _terminal_name 149 _terminal_name = 'roscore' if is_core else ','.join(args) 150 _set_terminal(_terminal_name)
151
152 -def get_or_generate_uuid(options_runid, options_wait_for_master):
153 """ 154 @param options_runid: run_id value from command-line or None 155 @type options_runid: str 156 @param options_wait_for_master: the wait_for_master command 157 option. If this is True, it means that we must retrieve the 158 value from the parameter server and need to avoid any race 159 conditions with the roscore being initialized. 160 @type options_wait_for_master: bool 161 """ 162 import roslib.scriptutil 163 # Three possible sources of the run_id: 164 # 165 # - if we're a child process, we get it from options_runid 166 # - if there's already a roscore running, read from the param server 167 # - generate one if we're running the roscore 168 if options_runid: 169 return options_runid 170 171 # #773: Generate a run_id to use if we launch a master 172 # process. If a master is already running, we'll get the 173 # run_id from it instead 174 param_server = roslib.scriptutil.get_param_server() 175 val = None 176 while val is None: 177 try: 178 code, msg, val = param_server.getParam('/roslaunch', '/run_id') 179 if code == 1: 180 return val 181 else: 182 val = None 183 raise RuntimeError("unknown error communicating with Parameter Server: %s"%msg) 184 except: 185 if not options_wait_for_master: 186 val = roslaunch.core.generate_run_id() 187 return val
188
189 -def check_roslaunch(f):
190 """ 191 Check roslaunch file for errors, returning error message if check fails. This routine 192 is mainly to support rostest's roslaunch_check. 193 194 @param f: roslaunch file name 195 @type f: str 196 @return: error message or None 197 @rtype: str 198 """ 199 try: 200 import roslaunch.config 201 config = roslaunch.config.load_config_default([f], DEFAULT_MASTER_PORT, verbose=False) 202 except roslaunch.RLException, e: 203 return str(e) 204 205 errors = [] 206 # check for missing deps 207 import roslaunch.depends 208 base_pkg, file_deps, missing = roslaunch.depends.roslaunch_deps([f]) 209 for pkg, miss in missing.iteritems(): 210 if miss: 211 errors.append("Missing manifest dependencies: %s/manifest.xml: %s"%(pkg, ', '.join(miss))) 212 213 # load all node defs 214 nodes = [] 215 for filename, rldeps in file_deps.iteritems(): 216 nodes.extend(rldeps.nodes) 217 218 # check for missing packages 219 import roslib.packages 220 for pkg, node_type in nodes: 221 try: 222 roslib.packages.get_pkg_dir(pkg, required=True) 223 except: 224 errors.append("cannot find package [%s] for node [%s]"%(pkg, node_type)) 225 226 # check for missing nodes 227 for pkg, node_type in nodes: 228 try: 229 if not roslib.packages.find_node(pkg, node_type): 230 errors.append("cannot find node [%s] in package [%s]"%(node_type, pkg)) 231 except Exception, e: 232 errors.append("unable to find node [%s/%s]: %s"%(pkg, node_type, str(e))) 233 234 # Check for configuration errors, #2889 235 for err in config.config_errors: 236 errors.append('ROSLaunch config error: %s' % err) 237 238 if errors: 239 return '\n'.join(errors)
240 241
242 -def namespaces_of(name):
243 if name is None: 244 raise ValueError('name') 245 if not isinstance(name, basestring): 246 raise TypeError('name') 247 if not name: 248 return [SEP] 249 250 splits = [x for x in name.split(SEP) if x] 251 return ['/'] + ['/'+SEP.join(splits[:i]) for i in xrange(1, len(splits))]
252