Package roslaunch
[frames] | no frames]

Source Code for Package roslaunch

  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  from __future__ import with_statement 
 36   
 37  import os 
 38  import logging 
 39  import sys 
 40   
 41  import roslib.packages 
 42  import roslaunch.core 
 43  import roslaunch.param_dump 
 44   
 45  # symbol exports 
 46  from roslaunch.core import Node, Test, Master, RLException 
 47  from roslaunch.config import ROSLaunchConfig 
 48  from roslaunch.launch import ROSLaunchRunner 
 49  from roslaunch.xmlloader import XmlLoader, XmlParseException 
 50   
 51   
 52  # script api 
 53  from roslaunch.scriptapi import ROSLaunch 
 54  from roslaunch.pmon import Process 
 55   
 56  try: 
 57      from rosmaster import DEFAULT_MASTER_PORT 
 58  except: 
 59      DEFAULT_MASTER_PORT = 11311 
 60       
 61  NAME = 'roslaunch' 
 62   
63 -def configure_logging(uuid):
64 """ 65 scripts using roslaunch MUST call configure_logging 66 """ 67 try: 68 import socket 69 import roslib.roslogging 70 logfile_basename = os.path.join(uuid, '%s-%s-%s.log'%(NAME, socket.gethostname(), os.getpid())) 71 # additional: names of python packages we depend on that may also be logging 72 logfile_name = roslib.roslogging.configure_logging(NAME, filename=logfile_basename) 73 if logfile_name: 74 print "... logging to %s"%logfile_name 75 76 # add logger to internal roslaunch logging infrastructure 77 logger = logging.getLogger('roslaunch') 78 roslaunch.core.add_printlog_handler(logger.info) 79 roslaunch.core.add_printerrlog_handler(logger.error) 80 except: 81 print >> sys.stderr, "WARNING: unable to configure logging. No log files will be generated"
82
83 -def write_pid_file(options_pid_fn, options_core, port):
84 from roslib.rosenv import get_ros_home 85 if options_pid_fn or options_core: 86 if options_pid_fn: 87 pid_fn = options_pid_fn 88 else: 89 # NOTE: this assumption is not 100% valid until work on #3097 is complete 90 if port is None: 91 port = DEFAULT_MASTER_PORT 92 #2987 93 pid_fn = os.path.join(get_ros_home(), 'roscore-%s.pid'%(port)) 94 with open(pid_fn, "w") as f: 95 f.write(str(os.getpid()))
96
97 -def _get_optparse():
98 from optparse import OptionParser 99 100 parser = OptionParser(usage="usage: %prog [options] [package] <filename> [arg_name:=value...]", prog=NAME) 101 parser.add_option("--files", 102 dest="file_list", default=False, action="store_true", 103 help="Print list files loaded by launch file, including launch file itself") 104 parser.add_option("--args", 105 dest="node_args", default=None, 106 help="Print command-line arguments for node", metavar="NODE_NAME") 107 parser.add_option("--nodes", 108 dest="node_list", default=False, action="store_true", 109 help="Print list of node names in launch file") 110 parser.add_option("--find-node", 111 dest="find_node", default=None, 112 help="Find launch file that node is defined in", metavar="NODE_NAME") 113 parser.add_option("-c", "--child", 114 dest="child_name", default=None, 115 help="Run as child service 'NAME'. Required with -u", metavar="NAME") 116 parser.add_option("--local", 117 dest="local_only", default=False, action="store_true", 118 help="Do not launch remote nodes") 119 # #2370 120 parser.add_option("--screen", 121 dest="force_screen", default=False, action="store_true", 122 help="Force output of all local nodes to screen") 123 parser.add_option("-u", "--server_uri", 124 dest="server_uri", default=None, 125 help="URI of server. Required with -c", metavar="URI") 126 parser.add_option("--run_id", 127 dest="run_id", default=None, 128 help="run_id of session. Required with -c", metavar="RUN_ID") 129 # #1254: wait until master comes online before starting 130 parser.add_option("--wait", action="store_true", 131 dest="wait_for_master", default=False, 132 help="wait for master to start before launching") 133 parser.add_option("-p", "--port", 134 dest="port", default=None, 135 help="master port. Only valid if master is launched", metavar="PORT") 136 parser.add_option("--core", action="store_true", 137 dest="core", default=False, 138 help="Launch core services only") 139 parser.add_option("--pid", 140 dest="pid_fn", default="", 141 help="write the roslaunch pid to filename") 142 parser.add_option("-v", action="store_true", 143 dest="verbose", default=False, 144 help="verbose printing") 145 # 2685 - Dump parameters of launch files 146 parser.add_option("--dump-params", default=False, action="store_true", 147 dest="dump_params", 148 help="Dump parameters of all roslaunch files to stdout") 149 return parser
150
151 -def _validate_args(parser, options, args):
152 # validate args first so we don't spin up any resources 153 if options.child_name: 154 if not options.server_uri: 155 parser.error("--child option requires --server_uri to be set as well") 156 if not options.run_id: 157 parser.error("--child option requires --run_id to be set as well") 158 if options.port: 159 parser.error("port option cannot be used with roslaunch child mode") 160 if args: 161 parser.error("Input files are not allowed when run in child mode") 162 elif options.core: 163 if args: 164 parser.error("Input files are not allowed when launching core") 165 if options.run_id: 166 parser.error("--run_id should only be set for child roslaunches (-c)") 167 168 # we don't actually do anything special for core as the roscore.xml file 169 # is an implicit include for any roslaunch 170 171 elif len(args) == 0: 172 parser.error("you must specify at least one input file") 173 elif [f for f in args if not os.path.exists(f)]: 174 parser.error("The following input files do not exist: %s"%f) 175 176 if len([x for x in [options.node_list, options.find_node, options.node_args] if x]) > 1: 177 parser.error("only one of [--nodes, --find-node, --args] may be specified")
178
179 -def main(argv=sys.argv):
180 options = None 181 try: 182 import roslaunch.rlutil 183 parser = _get_optparse() 184 185 (options, args) = parser.parse_args(argv[1:]) 186 args = roslaunch.rlutil.resolve_launch_arguments(args) 187 _validate_args(parser, options, args) 188 189 # node args doesn't require any roslaunch infrastructure, so process it first 190 if any([options.node_args, options.node_list, options.find_node, options.dump_params, options.file_list]): 191 if options.node_args and not args: 192 parser.error("please specify a launch file") 193 import roslaunch.node_args 194 if options.node_args: 195 roslaunch.node_args.print_node_args(options.node_args, args) 196 elif options.find_node: 197 roslaunch.node_args.print_node_filename(options.find_node, args) 198 # Dump parameters, #2685 199 elif options.dump_params: 200 roslaunch.param_dump.dump_params(args) 201 elif options.file_list: 202 roslaunch.rlutil.print_file_list(args) 203 else: 204 roslaunch.node_args.print_node_list(args) 205 return 206 207 # we have to wait for the master here because we don't have the run_id yet 208 if options.wait_for_master: 209 if options.core: 210 parser.error("--wait cannot be used with roscore") 211 roslaunch.rlutil._wait_for_master() 212 213 # write the pid to a file 214 write_pid_file(options.pid_fn, options.core, options.port) 215 216 # spin up the logging infrastructure. have to wait until we can read options.run_id 217 uuid = roslaunch.rlutil.get_or_generate_uuid(options.run_id, options.wait_for_master) 218 configure_logging(uuid) 219 220 # #3088: don't check disk usage on remote machines 221 if not options.child_name: 222 # #2761 223 roslaunch.rlutil.check_log_disk_usage() 224 225 logger = logging.getLogger('roslaunch') 226 logger.info("roslaunch starting with args %s"%str(argv)) 227 logger.info("roslaunch env is %s"%os.environ) 228 229 if options.child_name: 230 logger.info('starting in child mode') 231 232 # This is a roslaunch child, spin up client server. 233 # client spins up an XML-RPC server that waits for 234 # commands and configuration from the server. 235 import roslaunch.child 236 c = roslaunch.child.ROSLaunchChild(uuid, options.child_name, options.server_uri) 237 c.run() 238 else: 239 logger.info('starting in server mode') 240 241 # #1491 change terminal name 242 roslaunch.rlutil.change_terminal_name(args, options.core) 243 244 # This is a roslaunch parent, spin up parent server and launch processes. 245 # args are the roslaunch files to load 246 import roslaunch.parent 247 try: 248 # force a port binding spec if we are running a core 249 if options.core: 250 options.port = options.port or DEFAULT_MASTER_PORT 251 p = roslaunch.parent.ROSLaunchParent(uuid, args, is_core=options.core, port=options.port, local_only=options.local_only, verbose=options.verbose, force_screen=options.force_screen) 252 p.start() 253 p.spin() 254 finally: 255 # remove the pid file 256 if options.pid_fn: 257 try: os.unlink(options.pid_fn) 258 except os.error, reason: pass 259 260 except RLException, e: 261 roslaunch.core.printerrlog(str(e)) 262 sys.exit(1) 263 except ValueError, e: 264 # TODO: need to trap better than this high-level trap 265 roslaunch.core.printerrlog(str(e)) 266 sys.exit(1) 267 except roslib.packages.InvalidROSPkgException, e: 268 roslaunch.core.printerrlog(str(e)) 269 sys.exit(1) 270 except Exception, e: 271 import traceback 272 traceback.print_exc() 273 sys.exit(1)
274 275 if __name__ == '__main__': 276 main() 277