1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
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
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
82
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
90 if port is None:
91 port = DEFAULT_MASTER_PORT
92
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
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
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
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
143 parser.add_option("--dump-params", default=False, action="store_true",
144 dest="dump_params",
145 help="Dump parameters of all roslaunch files to stdout")
146 return parser
147
149
150 if options.child_name:
151 if not options.server_uri:
152 parser.error("--child option requires --server_uri to be set as well")
153 if not options.run_id:
154 parser.error("--child option requires --run_id to be set as well")
155 if options.port:
156 parser.error("port option cannot be used with roslaunch child mode")
157 if args:
158 parser.error("Input files are not allowed when run in child mode")
159 elif options.core:
160 if args:
161 parser.error("Input files are not allowed when launching core")
162 if options.run_id:
163 parser.error("--run_id should only be set for child roslaunches (-c)")
164
165
166
167
168 elif len(args) == 0:
169 parser.error("you must specify at least one input file")
170 elif [f for f in args if not os.path.exists(f)]:
171 parser.error("The following input files do not exist: %s"%f)
172
173 if len([x for x in [options.node_list, options.find_node, options.node_args] if x]) > 1:
174 parser.error("only one of [--nodes, --find-node, --args] may be specified")
175
176 -def main(argv=sys.argv):
177 options = None
178 try:
179 import roslaunch.rlutil
180 parser = _get_optparse()
181
182 (options, args) = parser.parse_args(argv[1:])
183 args = roslaunch.rlutil.resolve_launch_arguments(args)
184 _validate_args(parser, options, args)
185
186
187 if options.node_args or options.node_list or options.find_node or options.dump_params:
188 if options.node_args and not args:
189 parser.error("please specify a launch file")
190 import roslaunch.node_args
191 if options.node_args:
192 roslaunch.node_args.print_node_args(options.node_args, args)
193 elif options.find_node:
194 roslaunch.node_args.print_node_filename(options.find_node, args)
195
196 elif options.dump_params:
197 roslaunch.param_dump.dump_params(args)
198 else:
199 roslaunch.node_args.print_node_list(args)
200 return
201
202
203 if options.wait_for_master:
204 if options.core:
205 parser.error("--wait cannot be used with roscore")
206 roslaunch.rlutil._wait_for_master()
207
208
209 write_pid_file(options.pid_fn, options.core, options.port)
210
211
212 uuid = roslaunch.rlutil.get_or_generate_uuid(options.run_id, options.wait_for_master)
213 configure_logging(uuid)
214
215
216 if not options.child_name:
217
218 roslaunch.rlutil.check_log_disk_usage()
219
220 logger = logging.getLogger('roslaunch')
221 logger.info("roslaunch starting with args %s"%str(argv))
222 logger.info("roslaunch env is %s"%os.environ)
223
224 if options.child_name:
225 logger.info('starting in child mode')
226
227
228
229
230 import roslaunch.child
231 c = roslaunch.child.ROSLaunchChild(uuid, options.child_name, options.server_uri)
232 c.run()
233 else:
234 logger.info('starting in server mode')
235
236
237 roslaunch.rlutil.change_terminal_name(args, options.core)
238
239
240
241 import roslaunch.parent
242 try:
243
244 if options.core:
245 options.port = options.port or DEFAULT_MASTER_PORT
246 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)
247 p.start()
248 p.spin()
249 finally:
250
251 if options.pid_fn:
252 try: os.unlink(options.pid_fn)
253 except os.error, reason: pass
254
255 except RLException, e:
256 roslaunch.core.printerrlog(str(e))
257 sys.exit(1)
258 except ValueError, e:
259
260 roslaunch.core.printerrlog(str(e))
261 sys.exit(1)
262 except roslib.packages.InvalidROSPkgException, e:
263 roslaunch.core.printerrlog(str(e))
264 sys.exit(1)
265 except Exception, e:
266 import traceback
267 traceback.print_exc()
268 sys.exit(1)
269
270 if __name__ == '__main__':
271 main()
272