Package rosbag :: Module rosbag_main
[frames] | no frames]

Source Code for Module rosbag.rosbag_main

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2010, 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  from __future__ import print_function 
 34   
 35  import optparse 
 36  import os 
 37  import shutil 
 38  import signal 
 39  import subprocess 
 40  import sys 
 41  import time 
 42  try: 
 43      from UserDict import UserDict  # Python 2.x 
 44  except ImportError: 
 45      from collections import UserDict  # Python 3.x 
 46   
 47  import roslib.message 
 48  import roslib.packages 
 49   
 50  from .bag import Bag, Compression, ROSBagException, ROSBagFormatException, ROSBagUnindexedException 
 51  from .migration import MessageMigrator, fixbag2, checkbag 
 61   
62 -def handle_split(option, opt_str, value, parser):
63 parser.values.split = True 64 if len(parser.rargs) > 0 and parser.rargs[0].isdigit(): 65 print("Use of \"--split <MAX_SIZE>\" has been deprecated. Please use --split --size <MAX_SIZE> or --split --duration <MAX_DURATION>", file=sys.stderr) 66 parser.values.size = int(parser.rargs.pop(0))
67
68 69 -def _stop_process(signum, frame, old_handler, process):
70 process.terminate() 71 if old_handler: 72 old_handler(signum, frame)
73
74 75 -def record_cmd(argv):
76 parser = optparse.OptionParser(usage="rosbag record TOPIC1 [TOPIC2 TOPIC3 ...]", 77 description="Record a bag file with the contents of specified topics.", 78 formatter=optparse.IndentedHelpFormatter()) 79 80 parser.add_option("-a", "--all", dest="all", default=False, action="store_true", help="record all topics") 81 parser.add_option("-e", "--regex", dest="regex", default=False, action="store_true", help="match topics using regular expressions") 82 parser.add_option("-x", "--exclude", dest="exclude_regex", default="", action="store", help="exclude topics matching the follow regular expression (subtracts from -a or regex)") 83 parser.add_option("-q", "--quiet", dest="quiet", default=False, action="store_true", help="suppress console output") 84 parser.add_option("-o", "--output-prefix", dest="prefix", default=None, action="store", help="prepend PREFIX to beginning of bag name (name will always end with date stamp)") 85 parser.add_option("-O", "--output-name", dest="name", default=None, action="store", help="record to bag with name NAME.bag") 86 parser.add_option( "--split", dest="split", default=False, callback=handle_split, action="callback", help="split the bag when maximum size or duration is reached") 87 parser.add_option( "--size", dest="size", type='int', action="store", help="record a bag of maximum size SIZE MB. (Default: infinite)", metavar="SIZE") 88 parser.add_option( "--duration", dest="duration", type='string',action="store", help="record a bag of maximum duration DURATION in seconds, unless 'm', or 'h' is appended.", metavar="DURATION") 89 parser.add_option("-b", "--buffsize", dest="buffsize", default=256, type='int', action="store", help="use an internal buffer of SIZE MB (Default: %default, 0 = infinite)", metavar="SIZE") 90 parser.add_option("--chunksize", dest="chunksize", default=768, type='int', action="store", help="Advanced. Record to chunks of SIZE KB (Default: %default)", metavar="SIZE") 91 parser.add_option("-l", "--limit", dest="num", default=0, type='int', action="store", help="only record NUM messages on each topic") 92 parser.add_option( "--node", dest="node", default=None, type='string',action="store", help="record all topics subscribed to by a specific node") 93 parser.add_option("-j", "--bz2", dest="compression", default=None, action="store_const", const='bz2', help="use BZ2 compression") 94 parser.add_option("--lz4", dest="compression", action="store_const", const='lz4', help="use LZ4 compression") 95 96 (options, args) = parser.parse_args(argv) 97 98 if len(args) == 0 and not options.all and not options.node: 99 parser.error("You must specify a topic name or else use the '-a' option.") 100 101 if options.prefix is not None and options.name is not None: 102 parser.error("Can't set both prefix and name.") 103 104 recordpath = roslib.packages.find_node('rosbag', 'record') 105 if not recordpath: 106 parser.error("Cannot find rosbag/record executable") 107 cmd = [recordpath[0]] 108 109 cmd.extend(['--buffsize', str(options.buffsize)]) 110 cmd.extend(['--chunksize', str(options.chunksize)]) 111 112 if options.num != 0: cmd.extend(['--limit', str(options.num)]) 113 if options.quiet: cmd.extend(["--quiet"]) 114 if options.prefix: cmd.extend(["-o", options.prefix]) 115 if options.name: cmd.extend(["-O", options.name]) 116 if options.exclude_regex: cmd.extend(["--exclude", options.exclude_regex]) 117 if options.all: cmd.extend(["--all"]) 118 if options.regex: cmd.extend(["--regex"]) 119 if options.compression: cmd.extend(["--%s" % options.compression]) 120 if options.split: 121 if not options.duration and not options.size: 122 parser.error("Split specified without giving a maximum duration or size") 123 cmd.extend(["--split"]) 124 if options.duration: cmd.extend(["--duration", options.duration]) 125 if options.size: cmd.extend(["--size", str(options.size)]) 126 if options.node: 127 cmd.extend(["--node", options.node]) 128 129 cmd.extend(args) 130 131 old_handler = signal.signal( 132 signal.SIGTERM, 133 lambda signum, frame: _stop_process(signum, frame, old_handler, process) 134 ) 135 # Better way of handling it than os.execv 136 # This makes sure stdin handles are passed to the process. 137 process = subprocess.Popen(cmd) 138 process.wait()
139
140 141 -def info_cmd(argv):
142 parser = optparse.OptionParser(usage='rosbag info [options] BAGFILE1 [BAGFILE2 BAGFILE3 ...]', 143 description='Summarize the contents of one or more bag files.') 144 parser.add_option('-y', '--yaml', dest='yaml', default=False, action='store_true', help='print information in YAML format') 145 parser.add_option('-k', '--key', dest='key', default=None, action='store', help='print information on the given key') 146 parser.add_option( '--freq', dest='freq', default=False, action='store_true', help='display topic message frequency statistics') 147 (options, args) = parser.parse_args(argv) 148 149 if len(args) == 0: 150 parser.error('You must specify at least 1 bag file.') 151 if options.key and not options.yaml: 152 parser.error('You can only specify key when printing in YAML format.') 153 154 for i, arg in enumerate(args): 155 try: 156 b = Bag(arg, 'r', skip_index=not options.freq) 157 if options.yaml: 158 info = b._get_yaml_info(key=options.key) 159 if info is not None: 160 print(info) 161 else: 162 print(b) 163 b.close() 164 if i < len(args) - 1: 165 print('---') 166 167 except ROSBagUnindexedException as ex: 168 print('ERROR bag unindexed: %s. Run rosbag reindex.' % arg, 169 file=sys.stderr) 170 except ROSBagException as ex: 171 print('ERROR reading %s: %s' % (arg, str(ex)), file=sys.stderr) 172 except IOError as ex: 173 print('ERROR reading %s: %s' % (arg, str(ex)), file=sys.stderr)
174
175 176 -def handle_topics(option, opt_str, value, parser):
177 topics = [] 178 for arg in parser.rargs: 179 if arg[:2] == "--" and len(arg) > 2: 180 break 181 if arg[:1] == "-" and len(arg) > 1: 182 break 183 topics.append(arg) 184 parser.values.topics.extend(topics) 185 del parser.rargs[:len(topics)]
186
187 188 -def handle_pause_topics(option, opt_str, value, parser):
189 pause_topics = [] 190 for arg in parser.rargs: 191 if arg[:2] == "--" and len(arg) > 2: 192 break 193 if arg[:1] == "-" and len(arg) > 1: 194 break 195 pause_topics.append(arg) 196 parser.values.pause_topics.extend(pause_topics) 197 del parser.rargs[:len(pause_topics)]
198
199 200 -def play_cmd(argv):
201 parser = optparse.OptionParser(usage="rosbag play BAGFILE1 [BAGFILE2 BAGFILE3 ...]", 202 description="Play back the contents of one or more bag files in a time-synchronized fashion.") 203 parser.add_option("-p", "--prefix", dest="prefix", default='', type='str', help="prefix all output topics") 204 parser.add_option("-q", "--quiet", dest="quiet", default=False, action="store_true", help="suppress console output") 205 parser.add_option("-i", "--immediate", dest="immediate", default=False, action="store_true", help="play back all messages without waiting") 206 parser.add_option("--pause", dest="pause", default=False, action="store_true", help="start in paused mode") 207 parser.add_option("--queue", dest="queue", default=100, type='int', action="store", help="use an outgoing queue of size SIZE (defaults to %default)", metavar="SIZE") 208 parser.add_option("--clock", dest="clock", default=False, action="store_true", help="publish the clock time") 209 parser.add_option("--hz", dest="freq", default=100, type='float', action="store", help="use a frequency of HZ when publishing clock time (default: %default)", metavar="HZ") 210 parser.add_option("-d", "--delay", dest="delay", default=0.2, type='float', action="store", help="sleep SEC seconds after every advertise call (to allow subscribers to connect)", metavar="SEC") 211 parser.add_option("-r", "--rate", dest="rate", default=1.0, type='float', action="store", help="multiply the publish rate by FACTOR", metavar="FACTOR") 212 parser.add_option("-s", "--start", dest="start", default=0.0, type='float', action="store", help="start SEC seconds into the bag files", metavar="SEC") 213 parser.add_option("-u", "--duration", dest="duration", default=None, type='float', action="store", help="play only SEC seconds from the bag files", metavar="SEC") 214 parser.add_option("--skip-empty", dest="skip_empty", default=None, type='float', action="store", help="skip regions in the bag with no messages for more than SEC seconds", metavar="SEC") 215 parser.add_option("-l", "--loop", dest="loop", default=False, action="store_true", help="loop playback") 216 parser.add_option("-k", "--keep-alive", dest="keep_alive", default=False, action="store_true", help="keep alive past end of bag (useful for publishing latched topics)") 217 parser.add_option("--try-future-version", dest="try_future", default=False, action="store_true", help="still try to open a bag file, even if the version number is not known to the player") 218 parser.add_option("--topics", dest="topics", default=[], callback=handle_topics, action="callback", help="topics to play back") 219 parser.add_option("--pause-topics", dest="pause_topics", default=[], callback=handle_pause_topics, action="callback", help="topics to pause on during playback") 220 parser.add_option("--bags", help="bags files to play back from") 221 222 (options, args) = parser.parse_args(argv) 223 224 if options.bags: 225 args.append(options.bags) 226 227 if len(args) == 0: 228 parser.error('You must specify at least 1 bag file to play back.') 229 230 playpath = roslib.packages.find_node('rosbag', 'play') 231 if not playpath: 232 parser.error("Cannot find rosbag/play executable") 233 cmd = [playpath[0]] 234 235 if options.prefix: 236 cmd.extend(["--prefix", str(options.prefix)]) 237 238 if options.quiet: cmd.extend(["--quiet"]) 239 if options.pause: cmd.extend(["--pause"]) 240 if options.immediate: cmd.extend(["--immediate"]) 241 if options.loop: cmd.extend(["--loop"]) 242 if options.keep_alive: cmd.extend(["--keep-alive"]) 243 if options.try_future: cmd.extend(["--try-future-version"]) 244 245 if options.clock: 246 cmd.extend(["--clock", "--hz", str(options.freq)]) 247 248 cmd.extend(['--queue', str(options.queue)]) 249 cmd.extend(['--rate', str(options.rate)]) 250 cmd.extend(['--delay', str(options.delay)]) 251 cmd.extend(['--start', str(options.start)]) 252 if options.duration: 253 cmd.extend(['--duration', str(options.duration)]) 254 if options.skip_empty: 255 cmd.extend(['--skip-empty', str(options.skip_empty)]) 256 257 if options.topics: 258 cmd.extend(['--topics'] + options.topics) 259 260 if options.pause_topics: 261 cmd.extend(['--pause-topics'] + options.pause_topics) 262 263 # prevent bag files to be passed as --topics or --pause-topics 264 if options.topics or options.pause_topics: 265 cmd.extend(['--bags']) 266 267 cmd.extend(args) 268 269 old_handler = signal.signal( 270 signal.SIGTERM, 271 lambda signum, frame: _stop_process(signum, frame, old_handler, process) 272 ) 273 # Better way of handling it than os.execv 274 # This makes sure stdin handles are passed to the process. 275 process = subprocess.Popen(cmd) 276 process.wait()
277
278 279 -def filter_cmd(argv):
280 def expr_eval(expr): 281 def eval_fn(topic, m, t): 282 return eval(expr)
283 return eval_fn 284 285 parser = optparse.OptionParser(usage="""rosbag filter [options] INBAG OUTBAG EXPRESSION 286 287 EXPRESSION can be any Python-legal expression. 288 289 The following variables are available: 290 * topic: name of topic 291 * m: message 292 * t: time of message (t.secs, t.nsecs)""", 293 description='Filter the contents of the bag.') 294 parser.add_option('-p', '--print', action='store', dest='verbose_pattern', default=None, metavar='PRINT-EXPRESSION', help='Python expression to print for verbose debugging. Uses same variables as filter-expression') 295 296 options, args = parser.parse_args(argv) 297 if len(args) == 0: 298 parser.error('You must specify an in bag, an out bag, and an expression.') 299 if len(args) == 1: 300 parser.error('You must specify an out bag and an expression.') 301 if len(args) == 2: 302 parser.error("You must specify an expression.") 303 if len(args) > 3: 304 parser.error("Too many arguments.") 305 306 inbag_filename, outbag_filename, expr = args 307 308 if not os.path.isfile(inbag_filename): 309 print('Cannot locate input bag file [%s]' % inbag_filename, file=sys.stderr) 310 sys.exit(2) 311 312 if os.path.realpath(inbag_filename) == os.path.realpath(outbag_filename): 313 print('Cannot use same file as input and output [%s]' % inbag_filename, file=sys.stderr) 314 sys.exit(3) 315 316 filter_fn = expr_eval(expr) 317 318 outbag = Bag(outbag_filename, 'w') 319 320 try: 321 inbag = Bag(inbag_filename) 322 except ROSBagUnindexedException as ex: 323 print('ERROR bag unindexed: %s. Run rosbag reindex.' % inbag_filename, file=sys.stderr) 324 return 325 326 try: 327 meter = ProgressMeter(outbag_filename, inbag._uncompressed_size) 328 total_bytes = 0 329 330 if options.verbose_pattern: 331 verbose_pattern = expr_eval(options.verbose_pattern) 332 333 for topic, raw_msg, t in inbag.read_messages(raw=True): 334 msg_type, serialized_bytes, md5sum, pos, pytype = raw_msg 335 msg = pytype() 336 msg.deserialize(serialized_bytes) 337 338 if filter_fn(topic, msg, t): 339 print('MATCH', verbose_pattern(topic, msg, t)) 340 outbag.write(topic, msg, t) 341 else: 342 print('NO MATCH', verbose_pattern(topic, msg, t)) 343 344 total_bytes += len(serialized_bytes) 345 meter.step(total_bytes) 346 else: 347 for topic, raw_msg, t in inbag.read_messages(raw=True): 348 msg_type, serialized_bytes, md5sum, pos, pytype = raw_msg 349 msg = pytype() 350 msg.deserialize(serialized_bytes) 351 352 if filter_fn(topic, msg, t): 353 outbag.write(topic, msg, t) 354 355 total_bytes += len(serialized_bytes) 356 meter.step(total_bytes) 357 358 meter.finish() 359 360 finally: 361 inbag.close() 362 outbag.close() 363
364 -def fix_cmd(argv):
365 parser = optparse.OptionParser(usage='rosbag fix INBAG OUTBAG [EXTRARULES1 EXTRARULES2 ...]', description='Repair the messages in a bag file so that it can be played in the current system.') 366 parser.add_option('-n', '--noplugins', action='store_true', dest='noplugins', help='do not load rulefiles via plugins') 367 parser.add_option('--force', action='store_true', dest='force', help='proceed with migrations, even if not all rules defined') 368 369 (options, args) = parser.parse_args(argv) 370 371 if len(args) < 1: 372 parser.error('You must pass input and output bag files.') 373 if len(args) < 2: 374 parser.error('You must pass an output bag file.') 375 376 inbag_filename = args[0] 377 outbag_filename = args[1] 378 rules = args[2:] 379 380 ext = os.path.splitext(outbag_filename)[1] 381 if ext == '.bmr': 382 parser.error('Input file should be a bag file, not a rule file.') 383 if ext != '.bag': 384 parser.error('Output file must be a bag file.') 385 386 outname = outbag_filename + '.tmp' 387 388 if os.path.exists(outbag_filename): 389 if not os.access(outbag_filename, os.W_OK): 390 print('Don\'t have permissions to access %s' % outbag_filename, file=sys.stderr) 391 sys.exit(1) 392 else: 393 try: 394 file = open(outbag_filename, 'w') 395 file.close() 396 except IOError as e: 397 print('Cannot open %s for writing' % outbag_filename, file=sys.stderr) 398 sys.exit(1) 399 400 if os.path.exists(outname): 401 if not os.access(outname, os.W_OK): 402 print('Don\'t have permissions to access %s' % outname, file=sys.stderr) 403 sys.exit(1) 404 else: 405 try: 406 file = open(outname, 'w') 407 file.close() 408 except IOError as e: 409 print('Cannot open %s for writing' % outname, file=sys.stderr) 410 sys.exit(1) 411 412 if options.noplugins is None: 413 options.noplugins = False 414 415 migrator = MessageMigrator(rules, plugins=not options.noplugins) 416 417 try: 418 migrations = fixbag2(migrator, inbag_filename, outname, options.force) 419 except ROSBagUnindexedException as ex: 420 print('ERROR bag unindexed: %s. Run rosbag reindex.' % inbag_filename, 421 file=sys.stderr) 422 return 423 424 if len(migrations) == 0: 425 os.rename(outname, outbag_filename) 426 print('Bag migrated successfully.') 427 else: 428 print('Bag could not be migrated. The following migrations could not be performed:') 429 for m in migrations: 430 print_trans(m[0][0].old_class, m[0][-1].new_class, 0) 431 432 if len(m[1]) > 0: 433 print(' %d rules missing:' % len(m[1])) 434 for r in m[1]: 435 print_trans(r.old_class, r.new_class,1) 436 437 print('Try running \'rosbag check\' to create the necessary rule files or run \'rosbag fix\' with the \'--force\' option.') 438 os.remove(outname)
439
440 -def check_cmd(argv):
441 parser = optparse.OptionParser(usage='rosbag check BAG [-g RULEFILE] [EXTRARULES1 EXTRARULES2 ...]', description='Determine whether a bag is playable in the current system, or if it can be migrated.') 442 parser.add_option('-g', '--genrules', action='store', dest='rulefile', default=None, help='generate a rulefile named RULEFILE') 443 parser.add_option('-a', '--append', action='store_true', dest='append', help='append to the end of an existing rulefile after loading it') 444 parser.add_option('-n', '--noplugins', action='store_true', dest='noplugins', help='do not load rulefiles via plugins') 445 (options, args) = parser.parse_args(argv) 446 447 if len(args) == 0: 448 parser.error('You must specify a bag file to check.') 449 if options.append and options.rulefile is None: 450 parser.error('Cannot specify -a without also specifying -g.') 451 if options.rulefile is not None: 452 rulefile_exists = os.path.isfile(options.rulefile) 453 if rulefile_exists and not options.append: 454 parser.error('The file %s already exists. Include -a if you intend to append.' % options.rulefile) 455 if not rulefile_exists and options.append: 456 parser.error('The file %s does not exist, and so -a is invalid.' % options.rulefile) 457 458 if options.append: 459 append_rule = [options.rulefile] 460 else: 461 append_rule = [] 462 463 # First check that the bag is not unindexed 464 try: 465 Bag(args[0]) 466 except ROSBagUnindexedException as ex: 467 print('ERROR bag unindexed: %s. Run rosbag reindex.' % args[0], file=sys.stderr) 468 return 469 470 mm = MessageMigrator(args[1:] + append_rule, not options.noplugins) 471 472 migrations = checkbag(mm, args[0]) 473 474 if len(migrations) == 0: 475 print('Bag file is up to date.') 476 exit(0) 477 478 print('The following migrations need to occur:') 479 all_rules = [] 480 for m in migrations: 481 all_rules.extend(m[1]) 482 483 print_trans(m[0][0].old_class, m[0][-1].new_class, 0) 484 if len(m[1]) > 0: 485 print(" %d rules missing:" % len(m[1])) 486 for r in m[1]: 487 print_trans(r.old_class, r.new_class, 1) 488 489 if options.rulefile is None: 490 if all_rules == []: 491 print("\nAll rules defined. Bag is ready to be migrated") 492 else: 493 print("\nTo generate rules, please run with -g <rulefile>") 494 exit(0) 495 496 output = '' 497 rules_left = mm.filter_rules_unique(all_rules) 498 499 if rules_left == []: 500 print("\nNo additional rule files needed to be generated. %s not created."%(options.rulefile)) 501 exit(0) 502 503 while len(rules_left) > 0: 504 extra_rules = [] 505 for r in rules_left: 506 if r.new_class is None: 507 print('The message type %s appears to have moved. Please enter the type to migrate it to.' % r.old_class._type) 508 new_type = raw_input('>') 509 new_class = roslib.message.get_message_class(new_type) 510 while new_class is None: 511 print("\'%s\' could not be found in your system. Please make sure it is built." % new_type) 512 new_type = raw_input('>') 513 new_class = roslib.message.get_message_class(new_type) 514 new_rule = mm.make_update_rule(r.old_class, new_class) 515 R = new_rule(mm, 'GENERATED.' + new_rule.__name__) 516 R.find_sub_paths() 517 new_rules = [r for r in mm.expand_rules(R.sub_rules) if r.valid == False] 518 extra_rules.extend(new_rules) 519 print('Creating the migration rule for %s requires additional missing rules:' % new_type) 520 for nr in new_rules: 521 print_trans(nr.old_class, nr.new_class,1) 522 output += R.get_class_def() 523 else: 524 output += r.get_class_def() 525 rules_left = mm.filter_rules_unique(extra_rules) 526 f = open(options.rulefile, 'a') 527 f.write(output) 528 f.close() 529 530 print('\nThe necessary rule files have been written to: %s' % options.rulefile)
531
532 -def compress_cmd(argv):
533 parser = optparse.OptionParser(usage='rosbag compress [options] BAGFILE1 [BAGFILE2 ...]', 534 description='Compress one or more bag files.') 535 parser.add_option( '--output-dir', action='store', dest='output_dir', help='write to directory DIR', metavar='DIR') 536 parser.add_option('-f', '--force', action='store_true', dest='force', help='force overwriting of backup file if it exists') 537 parser.add_option('-q', '--quiet', action='store_true', dest='quiet', help='suppress noncritical messages') 538 parser.add_option('-j', '--bz2', action='store_const', dest='compression', help='use BZ2 compression', const=Compression.BZ2, default=Compression.BZ2) 539 parser.add_option( '--lz4', action='store_const', dest='compression', help='use lz4 compression', const=Compression.LZ4) 540 (options, args) = parser.parse_args(argv) 541 542 if len(args) < 1: 543 parser.error('You must specify at least one bag file.') 544 545 op = lambda inbag, outbag, quiet: change_compression_op(inbag, outbag, options.compression, options.quiet) 546 547 bag_op(args, False, lambda b: False, op, options.output_dir, options.force, options.quiet)
548
549 -def decompress_cmd(argv):
550 parser = optparse.OptionParser(usage='rosbag decompress [options] BAGFILE1 [BAGFILE2 ...]', 551 description='Decompress one or more bag files.') 552 parser.add_option( '--output-dir', action='store', dest='output_dir', help='write to directory DIR', metavar='DIR') 553 parser.add_option('-f', '--force', action='store_true', dest='force', help='force overwriting of backup file if it exists') 554 parser.add_option('-q', '--quiet', action='store_true', dest='quiet', help='suppress noncritical messages') 555 556 (options, args) = parser.parse_args(argv) 557 558 if len(args) < 1: 559 parser.error('You must specify at least one bag file.') 560 561 op = lambda inbag, outbag, quiet: change_compression_op(inbag, outbag, Compression.NONE, options.quiet) 562 563 bag_op(args, False, lambda b: False, op, options.output_dir, options.force, options.quiet)
564
565 -def reindex_cmd(argv):
566 parser = optparse.OptionParser(usage='rosbag reindex [options] BAGFILE1 [BAGFILE2 ...]', 567 description='Reindexes one or more bag files.') 568 parser.add_option( '--output-dir', action='store', dest='output_dir', help='write to directory DIR', metavar='DIR') 569 parser.add_option('-f', '--force', action='store_true', dest='force', help='force overwriting of backup file if it exists') 570 parser.add_option('-q', '--quiet', action='store_true', dest='quiet', help='suppress noncritical messages') 571 572 (options, args) = parser.parse_args(argv) 573 574 if len(args) < 1: 575 parser.error('You must specify at least one bag file.') 576 577 op = lambda inbag, outbag, quiet: reindex_op(inbag, outbag, options.quiet) 578 579 bag_op(args, True, lambda b: b.version > 102, op, options.output_dir, options.force, options.quiet)
580
581 -def bag_op(inbag_filenames, allow_unindexed, copy_fn, op, output_dir=None, force=False, quiet=False):
582 for inbag_filename in inbag_filenames: 583 # Check we can read the file 584 try: 585 inbag = Bag(inbag_filename, 'r', allow_unindexed=allow_unindexed) 586 except ROSBagUnindexedException: 587 print('ERROR bag unindexed: %s. Run rosbag reindex.' % inbag_filename, file=sys.stderr) 588 continue 589 except (ROSBagException, IOError) as ex: 590 print('ERROR reading %s: %s' % (inbag_filename, str(ex)), file=sys.stderr) 591 continue 592 593 # Determine whether we should copy the bag 594 copy = copy_fn(inbag) 595 596 inbag.close() 597 598 # Determine filename for output bag 599 if output_dir is None: 600 outbag_filename = inbag_filename 601 else: 602 outbag_filename = os.path.join(output_dir, os.path.split(inbag_filename)[1]) 603 604 backup_filename = None 605 if outbag_filename == inbag_filename: 606 # Rename the input bag to ###.orig.###, and open for reading 607 backup_filename = '%s.orig%s' % os.path.splitext(inbag_filename) 608 609 if not force and os.path.exists(backup_filename): 610 if not quiet: 611 print('Skipping %s. Backup path %s already exists.' % (inbag_filename, backup_filename), file=sys.stderr) 612 continue 613 614 try: 615 if copy: 616 shutil.copy(inbag_filename, backup_filename) 617 else: 618 os.rename(inbag_filename, backup_filename) 619 except OSError as ex: 620 print('ERROR %s %s to %s: %s' % ('copying' if copy else 'moving', inbag_filename, backup_filename, str(ex)), file=sys.stderr) 621 continue 622 623 source_filename = backup_filename 624 else: 625 if copy: 626 shutil.copy(inbag_filename, outbag_filename) 627 source_filename = outbag_filename 628 else: 629 source_filename = inbag_filename 630 631 try: 632 inbag = Bag(source_filename, 'r', allow_unindexed=allow_unindexed) 633 634 # Open the output bag file for writing 635 try: 636 if copy: 637 outbag = Bag(outbag_filename, 'a', allow_unindexed=allow_unindexed) 638 else: 639 outbag = Bag(outbag_filename, 'w') 640 except (ROSBagException, IOError) as ex: 641 print('ERROR writing to %s: %s' % (outbag_filename, str(ex)), file=sys.stderr) 642 inbag.close() 643 continue 644 645 # Perform the operation 646 try: 647 op(inbag, outbag, quiet=quiet) 648 except ROSBagException as ex: 649 print('\nERROR operating on %s: %s' % (source_filename, str(ex)), file=sys.stderr) 650 inbag.close() 651 outbag.close() 652 continue 653 654 outbag.close() 655 inbag.close() 656 657 except KeyboardInterrupt: 658 if backup_filename is not None: 659 try: 660 if copy: 661 os.remove(backup_filename) 662 else: 663 os.rename(backup_filename, inbag_filename) 664 except OSError as ex: 665 print('ERROR %s %s to %s: %s', ('removing' if copy else 'moving', backup_filename, inbag_filename, str(ex)), file=sys.stderr) 666 break 667 668 except (ROSBagException, IOError) as ex: 669 print('ERROR operating on %s: %s' % (inbag_filename, str(ex)), file=sys.stderr)
670
671 -def change_compression_op(inbag, outbag, compression, quiet):
672 outbag.compression = compression 673 674 if quiet: 675 for topic, msg, t in inbag.read_messages(raw=True): 676 outbag.write(topic, msg, t, raw=True) 677 else: 678 meter = ProgressMeter(outbag.filename, inbag._uncompressed_size) 679 680 total_bytes = 0 681 for topic, msg, t in inbag.read_messages(raw=True): 682 msg_type, serialized_bytes, md5sum, pos, pytype = msg 683 684 outbag.write(topic, msg, t, raw=True) 685 686 total_bytes += len(serialized_bytes) 687 meter.step(total_bytes) 688 689 meter.finish()
690
691 -def reindex_op(inbag, outbag, quiet):
692 if inbag.version == 102: 693 if quiet: 694 try: 695 for offset in inbag.reindex(): 696 pass 697 except: 698 pass 699 700 for (topic, msg, t) in inbag.read_messages(): 701 outbag.write(topic, msg, t) 702 else: 703 meter = ProgressMeter(outbag.filename, inbag.size) 704 try: 705 for offset in inbag.reindex(): 706 meter.step(offset) 707 except: 708 pass 709 meter.finish() 710 711 meter = ProgressMeter(outbag.filename, inbag.size) 712 for (topic, msg, t) in inbag.read_messages(): 713 outbag.write(topic, msg, t) 714 meter.step(inbag._file.tell()) 715 meter.finish() 716 else: 717 if quiet: 718 try: 719 for offset in outbag.reindex(): 720 pass 721 except: 722 pass 723 else: 724 meter = ProgressMeter(outbag.filename, outbag.size) 725 try: 726 for offset in outbag.reindex(): 727 meter.step(offset) 728 except: 729 pass 730 meter.finish()
731
732 -class RosbagCmds(UserDict):
733 - def __init__(self):
734 UserDict.__init__(self) 735 self._description = {} 736 self['help'] = self.help_cmd
737
738 - def add_cmd(self, name, function, description):
739 self[name] = function 740 self._description[name] = description
741
742 - def get_valid_cmds(self):
743 str = "Available subcommands:\n" 744 for k in sorted(self.keys()): 745 str += " %s " % k 746 if k in self._description.keys(): 747 str +="\t%s" % self._description[k] 748 str += "\n" 749 return str
750
751 - def help_cmd(self,argv):
752 argv = [a for a in argv if a != '-h' and a != '--help'] 753 754 if len(argv) == 0: 755 print('Usage: rosbag <subcommand> [options] [args]') 756 print() 757 print("A bag is a file format in ROS for storing ROS message data. The rosbag command can record, replay and manipulate bags.") 758 print() 759 print(self.get_valid_cmds()) 760 print('For additional information, see http://wiki.ros.org/rosbag') 761 print() 762 return 763 764 cmd = argv[0] 765 if cmd in self: 766 self[cmd](['-h']) 767 else: 768 print("Unknown command: '%s'" % cmd, file=sys.stderr) 769 print(self.get_valid_cmds(), file=sys.stderr)
770
771 -class ProgressMeter(object):
772 - def __init__(self, path, bytes_total, refresh_rate=1.0):
773 self.path = path 774 self.bytes_total = bytes_total 775 self.refresh_rate = refresh_rate 776 777 self.elapsed = 0.0 778 self.update_elapsed = 0.0 779 self.bytes_read = 0.0 780 781 self.start_time = time.time() 782 783 self._update_progress()
784
785 - def step(self, bytes_read, force_update=False):
786 self.bytes_read = bytes_read 787 self.elapsed = time.time() - self.start_time 788 789 if force_update or self.elapsed - self.update_elapsed > self.refresh_rate: 790 self._update_progress() 791 self.update_elapsed = self.elapsed
792
793 - def _update_progress(self):
794 max_path_len = self.terminal_width() - 37 795 path = self.path 796 if len(path) > max_path_len: 797 path = '...' + self.path[-max_path_len + 3:] 798 799 bytes_read_str = self._human_readable_size(float(self.bytes_read)) 800 bytes_total_str = self._human_readable_size(float(self.bytes_total)) 801 802 if self.bytes_read < self.bytes_total: 803 complete_fraction = float(self.bytes_read) / self.bytes_total 804 pct_complete = int(100.0 * complete_fraction) 805 806 if complete_fraction > 0.0: 807 eta = self.elapsed * (1.0 / complete_fraction - 1.0) 808 eta_min, eta_sec = eta / 60, eta % 60 809 if eta_min > 99: 810 eta_str = '--:--' 811 else: 812 eta_str = '%02d:%02d' % (eta_min, eta_sec) 813 else: 814 eta_str = '--:--' 815 816 progress = '%-*s %3d%% %8s / %8s %s ETA' % (max_path_len, path, pct_complete, bytes_read_str, bytes_total_str, eta_str) 817 else: 818 progress = '%-*s 100%% %19s %02d:%02d ' % (max_path_len, path, bytes_total_str, self.elapsed / 60, self.elapsed % 60) 819 820 print('\r', progress, end='') 821 sys.stdout.flush()
822
823 - def _human_readable_size(self, size):
824 multiple = 1024.0 825 for suffix in ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']: 826 size /= multiple 827 if size < multiple: 828 return '%.1f %s' % (size, suffix) 829 830 raise ValueError('number too large')
831
832 - def finish(self):
833 self.step(self.bytes_total, force_update=True) 834 print()
835 836 @staticmethod
837 - def terminal_width():
838 """Estimate the width of the terminal""" 839 width = 0 840 try: 841 import struct, fcntl, termios 842 s = struct.pack('HHHH', 0, 0, 0, 0) 843 x = fcntl.ioctl(1, termios.TIOCGWINSZ, s) 844 width = struct.unpack('HHHH', x)[1] 845 except (IOError, ImportError): 846 pass 847 if width <= 0: 848 try: 849 width = int(os.environ['COLUMNS']) 850 except: 851 pass 852 if width <= 0: 853 width = 80 854 855 return width
856
857 -def rosbagmain(argv=None):
858 cmds = RosbagCmds() 859 cmds.add_cmd('record', record_cmd, "Record a bag file with the contents of specified topics.") 860 cmds.add_cmd('info', info_cmd, 'Summarize the contents of one or more bag files.') 861 cmds.add_cmd('play', play_cmd, "Play back the contents of one or more bag files in a time-synchronized fashion.") 862 cmds.add_cmd('check', check_cmd, 'Determine whether a bag is playable in the current system, or if it can be migrated.') 863 cmds.add_cmd('fix', fix_cmd, 'Repair the messages in a bag file so that it can be played in the current system.') 864 cmds.add_cmd('filter', filter_cmd, 'Filter the contents of the bag.') 865 cmds.add_cmd('compress', compress_cmd, 'Compress one or more bag files.') 866 cmds.add_cmd('decompress', decompress_cmd, 'Decompress one or more bag files.') 867 cmds.add_cmd('reindex', reindex_cmd, 'Reindexes one or more bag files.') 868 869 if argv is None: 870 argv = sys.argv 871 872 if '-h' in argv or '--help' in argv: 873 argv = [a for a in argv if a != '-h' and a != '--help'] 874 argv.insert(1, 'help') 875 876 if len(argv) > 1: 877 cmd = argv[1] 878 else: 879 cmd = 'help' 880 881 try: 882 if cmd in cmds: 883 cmds[cmd](argv[2:]) 884 else: 885 cmds['help']([cmd]) 886 except KeyboardInterrupt: 887 pass
888