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

Source Code for Module roslaunch.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  """ 
 34  Uncategorized utility routines for roslaunch. 
 35   
 36  This API should not be considered stable. 
 37  """ 
 38   
 39  from __future__ import print_function 
 40   
 41  import os 
 42  import sys 
 43  import time 
 44   
 45  import roslib.packages 
 46   
 47  import rosclean 
 48  import rospkg 
 49  import rosgraph 
 50   
 51  import roslaunch.core 
 52  import roslaunch.config 
 53  import roslaunch.depends 
 54  from rosmaster import DEFAULT_MASTER_PORT 
 55   
56 -def check_log_disk_usage():
57 """ 58 Check size of log directory. If high, print warning to user 59 """ 60 try: 61 d = rospkg.get_log_dir() 62 roslaunch.core.printlog("Checking log directory for disk usage. This may take awhile.\nPress Ctrl-C to interrupt") 63 disk_usage = rosclean.get_disk_usage(d) 64 # warn if over a gig 65 if disk_usage > 1073741824: 66 roslaunch.core.printerrlog("WARNING: disk usage in log directory [%s] is over 1GB.\nIt's recommended that you use the 'rosclean' command."%d) 67 else: 68 roslaunch.core.printlog("Done checking log file disk usage. Usage is <1GB.") 69 except: 70 pass
71
72 -def resolve_launch_arguments(args):
73 """ 74 Resolve command-line args to roslaunch filenames. 75 76 :returns: resolved filenames, ``[str]`` 77 """ 78 79 # strip remapping args for processing 80 args = rosgraph.myargv(args) 81 82 # user can either specify: 83 # - filename + launch args 84 # - package + relative-filename + launch args 85 if not args: 86 return args 87 resolved_args = None 88 89 # try to resolve launch file in package first 90 if len(args) >= 2: 91 try: 92 resolved = roslib.packages.find_resource(args[0], args[1]) 93 if len(resolved) > 1: 94 raise roslaunch.core.RLException("multiple files named [%s] in package [%s]:%s\nPlease specify full path instead" % (args[1], args[0], ''.join(['\n- %s' % r for r in resolved]))) 95 if len(resolved) == 1: 96 resolved_args = [resolved[0]] + args[2:] 97 except rospkg.ResourceNotFound: 98 pass 99 # try to resolve launch file 100 if resolved_args is None and (args[0] == '-' or os.path.isfile(args[0])): 101 resolved_args = [args[0]] + args[1:] 102 # raise if unable to resolve 103 if resolved_args is None: 104 if len(args) >= 2: 105 raise roslaunch.core.RLException("[%s] is neither a launch file in package [%s] nor is [%s] a launch file name" % (args[1], args[0], args[0])) 106 else: 107 raise roslaunch.core.RLException("[%s] is not a launch file name" % args[0]) 108 return resolved_args
109
110 -def _wait_for_master():
111 """ 112 Block until ROS Master is online 113 114 :raise: :exc:`RuntimeError` If unexpected error occurs 115 """ 116 m = roslaunch.core.Master() # get a handle to the default master 117 is_running = m.is_running() 118 if not is_running: 119 roslaunch.core.printlog("roscore/master is not yet running, will wait for it to start") 120 while not is_running: 121 time.sleep(0.1) 122 is_running = m.is_running() 123 if is_running: 124 roslaunch.core.printlog("master has started, initiating launch") 125 else: 126 raise RuntimeError("unknown error waiting for master to start")
127 128 _terminal_name = None 129
130 -def _set_terminal(s):
131 import platform 132 if platform.system() in ['FreeBSD', 'Linux', 'Darwin', 'Unix']: 133 try: 134 print('\033]2;%s\007'%(s)) 135 except: 136 pass
137
138 -def update_terminal_name(ros_master_uri):
139 """ 140 append master URI to the terminal name 141 """ 142 if _terminal_name: 143 _set_terminal(_terminal_name + ' ' + ros_master_uri)
144
145 -def change_terminal_name(args, is_core):
146 """ 147 use echo (where available) to change the name of the terminal window 148 """ 149 global _terminal_name 150 _terminal_name = 'roscore' if is_core else ','.join(args) 151 _set_terminal(_terminal_name)
152
153 -def get_or_generate_uuid(options_runid, options_wait_for_master):
154 """ 155 :param options_runid: run_id value from command-line or ``None``, ``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. ``bool`` 160 """ 161 162 # Three possible sources of the run_id: 163 # 164 # - if we're a child process, we get it from options_runid 165 # - if there's already a roscore running, read from the param server 166 # - generate one if we're running the roscore 167 if options_runid: 168 return options_runid 169 170 # #773: Generate a run_id to use if we launch a master 171 # process. If a master is already running, we'll get the 172 # run_id from it instead 173 param_server = rosgraph.Master('/roslaunch') 174 val = None 175 while val is None: 176 try: 177 val = param_server.getParam('/run_id') 178 except: 179 if not options_wait_for_master: 180 val = roslaunch.core.generate_run_id() 181 return val
182
183 -def check_roslaunch(f, use_test_depends=False):
184 """ 185 Check roslaunch file for errors, returning error message if check fails. This routine 186 is mainly to support rostest's roslaunch_check. 187 188 :param f: roslaunch file name, ``str`` 189 :param use_test_depends: Consider test_depends, ``Bool`` 190 :returns: error message or ``None`` 191 """ 192 try: 193 rl_config = roslaunch.config.load_config_default([f], DEFAULT_MASTER_PORT, verbose=False) 194 except roslaunch.core.RLException as e: 195 return str(e) 196 197 rospack = rospkg.RosPack() 198 errors = [] 199 # check for missing deps 200 try: 201 base_pkg, file_deps, missing = roslaunch.depends.roslaunch_deps([f], use_test_depends=use_test_depends) 202 except rospkg.common.ResourceNotFound as r: 203 errors.append("Could not find package [%s] included from [%s]"%(str(r), f)) 204 missing = {} 205 file_deps = {} 206 except roslaunch.substitution_args.ArgException as e: 207 errors.append("Could not resolve arg [%s] in [%s]"%(str(e), f)) 208 missing = {} 209 file_deps = {} 210 for pkg, miss in missing.items(): 211 # even if the pkgs is not found in packges.xml, if other package.xml provdes that pkgs, then it will be ok 212 all_pkgs = [] 213 try: 214 for file_dep in file_deps.keys(): 215 file_pkg = rospkg.get_package_name(file_dep) 216 all_pkgs.extend(rospack.get_depends(file_pkg,implicit=False)) 217 miss_all = list(set(miss) - set(all_pkgs)) 218 except Exception as e: 219 print(e, file=sys.stderr) 220 miss_all = True 221 if miss_all: 222 print("Missing package dependencies: %s/package.xml: %s"%(pkg, ', '.join(miss)), file=sys.stderr) 223 errors.append("Missing package dependencies: %s/package.xml: %s"%(pkg, ', '.join(miss))) 224 elif miss: 225 print("Missing package dependencies: %s/package.xml: %s (notify upstream maintainer)"%(pkg, ', '.join(miss)), file=sys.stderr) 226 227 # load all node defs 228 nodes = [] 229 for filename, rldeps in file_deps.items(): 230 nodes.extend(rldeps.nodes) 231 232 # check for missing packages 233 for pkg, node_type in nodes: 234 try: 235 rospack.get_path(pkg) 236 except: 237 errors.append("cannot find package [%s] for node [%s]"%(pkg, node_type)) 238 239 # check for missing nodes 240 for pkg, node_type in nodes: 241 try: 242 if not roslib.packages.find_node(pkg, node_type, rospack=rospack): 243 errors.append("cannot find node [%s] in package [%s]"%(node_type, pkg)) 244 except Exception as e: 245 errors.append("unable to find node [%s/%s]: %s"%(pkg, node_type, str(e))) 246 247 # Check for configuration errors, #2889 248 for err in rl_config.config_errors: 249 errors.append('ROSLaunch config error: %s' % err) 250 251 if errors: 252 return '\n'.join(errors)
253 270