$search
00001 #!/usr/bin/env python 00002 ''' 00003 bag_analyzer.py 00004 Mac Mason <mac@cs.duke.edu> 00005 00006 Given a list of bagfiles, compute for each their (a) duration and size (in 00007 bits). 00008 ''' 00009 00010 import sys 00011 import os 00012 import subprocess 00013 00014 if len(sys.argv) < 2: 00015 print "Usage: bag_analyzer.py <one or more bagfiles>" 00016 sys.exit(1) 00017 00018 def sum_tuple(a, b): 00019 ''' 00020 Our tuples are (duration (sec.), compressed size, uncompressed size (GB), 00021 count) 00022 ''' 00023 return (a[0] + b[0], a[1] + b[1], a[2] + b[2], a[3] + b[3]) 00024 00025 def tuplize(f): 00026 ''' 00027 Given a filename f, call rosbag info and parse out the three tuple 00028 values. 00029 ''' 00030 lines = subprocess.Popen(['rosbag', 'info', f], 00031 stdout=subprocess.PIPE, 00032 stderr=subprocess.STDOUT, 00033 env=os.environ).communicate()[0].split('\n') 00034 00035 def parse_duration(lines): 00036 ''' Find the duration in second, given the lines.''' 00037 try: 00038 line = (line for line in lines if 'duration:' in line).next() # OWW. 00039 except StopIteration: 00040 print lines 00041 return float(line.split()[-1].split('(')[1].split('s')[0]) 00042 00043 def parse_compressed(lines): 00044 ''' 00045 Get the compressed value. Note the check for '%' to make sure we don't 00046 actually parse the 'uncompressed' line. 00047 ''' 00048 line = (l for l in lines if 'compressed:' in l and '%' in l).next() 00049 x = float(line.split()[1]) 00050 if 'GB' not in line: 00051 x /= 1024.0 00052 return x 00053 00054 def parse_uncompressed(lines): 00055 ''' 00056 And the uncompressed size. 00057 ''' 00058 line = (line for line in lines if 'uncompressed:' in line).next() 00059 return float(line.split()[1]) 00060 00061 return (parse_duration(lines), 00062 parse_compressed(lines), 00063 parse_uncompressed(lines), 00064 1) 00065 00066 00067 00068 print "I have", len(sys.argv[1:]), "bagfiles." 00069 res = reduce(sum_tuple, (tuplize(x) for x in sys.argv[1:])) 00070 print "Duration: %f" % res[0] 00071 print "Compressed: %f GB" % res[1] 00072 print "Uncompressed: %f GB " % res[2] 00073 print "Count: %d" % res[3]