15 print "Please install colorama by pip install colorama" 17 from colorama
import Fore, Style
23 run rosbag and return Popen object 25 cmd =
'roslaunch jsk_data rosbag_always_run_rosbag.launch' 26 formatted_topics = [t
for t
in topics.split(
' ')
if t]
27 args = cmd.split(
' ') + [
"TOPICS:=" + topics +
""] + [
"SIZE:=" + size] + [
"OUTPUT:=" + save_dir +
'/rosbag']
29 return subprocess.Popen(args)
34 regexp =
'rosbag_(\d\d\d\d)-(\d\d)-(\d\d)-(\d\d)-(\d\d)-(\d\d)_([\d]+).bag' 35 result = re.match(regexp, bag)
39 return [result.group(f)
for f
in range(1, 8)]
48 directory = os.path.join(root_dir, YYYY, MM, DD, HH)
49 if not os.path.exists(directory):
50 os.makedirs(directory)
56 from_file = os.path.join(save_dir, bag)
57 to_file = os.path.join(move_dir, bag)
58 print 'moving file %s -> %s' % (from_file, to_file)
59 shutil.move(from_file, to_file)
62 files = os.listdir(save_dir)
63 target_bags = [f
for f
in files
64 if f.startswith(
'rosbag')
65 and f.endswith(
'.bag')]
71 "size unit is Mega bytes" 73 for dirpath, dirnames, filenames
in os.walk(start_path):
75 fp = os.path.join(dirpath, f)
77 total_size += os.path.getsize(fp)
80 return total_size / 1000.0 / 1000.0
84 parse[len(parse) - 1] = str(int(parse[len(parse) - 1])).zfill(4)
85 concatenated_string = reduce(
lambda x, y: x + y, parse)
86 return int(concatenated_string)
90 for dirpath, dirnames, filenames
in os.walk(save_dir):
92 if f.startswith(
'rosbag')
and (f.endswith(
'.bag')
or f.endswith(
'.active')):
93 bags.append(os.path.join(dirpath, f))
94 return sorted(bags, key=keyFuncToSortBag)
98 remove_size = current_size - max_size
100 the_size = os.path.getsize(f)
101 print Fore.GREEN +
'removing %s (%d)' % (f, the_size / 1000 / 1000) + Fore.RESET
104 subprocess.check_output([
'notify-send',
"Removed %s (%d)" % (f, the_size / 1000 / 1000)])
105 remove_size = remove_size - the_size / 1000.0 / 1000.0
115 g_rosbag_process =
False 118 global g_rosbag_process
119 print 'Running rosbag...' 120 g_rosbag_process =
runROSBag(topics, size, save_dir)
123 output = subprocess.check_output([
'ps',
'--ppid=' + str(ppid),
'--no-headers'])
124 for process_line
in output.split(
'\n'):
125 strip_process_line = process_line.strip()
126 if strip_process_line:
127 pid = strip_process_line.split(
' ')[0]
128 name = strip_process_line.split(
' ')[-1]
129 print 'killing %s' % (name)
130 os.kill(int(pid), signal.SIGINT)
133 global g_rosbag_process
135 print 'Killing rosbag ...' 136 rosbag_pid = g_rosbag_process.pid
139 g_rosbag_process.send_signal(subprocess.signal.SIGINT)
143 def main(topics, size, save_dir, max_size, rate = 1):
144 if not os.path.exists(save_dir):
145 os.makedirs(save_dir)
146 previous_master_state =
None 149 master_state = isMasterAlive()
150 if not master_state
and previous_master_state:
153 elif master_state
and not previous_master_state:
154 print "restart rosbag" 157 previous_master_state = master_state
158 time.sleep(1.0 / rate)
167 if __name__ ==
"__main__":
168 parser = argparse.ArgumentParser(description=
'rosbag record regardless of rosmaster status')
169 parser.add_argument(
'--topics', help=
"topics to record", required=
True)
170 parser.add_argument(
'--size', help=
"size of each rosbag", required=
True)
171 parser.add_argument(
'--save-dir', help=
"directory to store rosbag", required=
True)
172 parser.add_argument(
'--max-size', help=
"maximum size of rosbags in save_dir", required=
True, type=int)
173 args = parser.parse_args()
174 main(args.topics, args.size, args.save_dir, args.max_size)
def main(topics, size, save_dir, max_size, rate=1)
def killChildProcesses(ppid)
def listBagsSortedByDate(save_dir)
def checkDirectorySize(save_dir, max_size)
def removeOldFiles(save_dir, max_size, current_size)
def watchFileSystem(save_dir, max_size)
def moveBagFiles(save_dir, bags)
def restartROSBag(topics, size, save_dir)
def mkdirForBag(root_dir, bag)
def keyFuncToSortBag(bag)
def getDirectorySize(start_path='.')
def runROSBag(topics, size, save_dir)