Package roshlaunch
[frames] | no frames]

Source Code for Package roshlaunch

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