-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy_bags.py
133 lines (106 loc) · 4.53 KB
/
copy_bags.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python
import multiprocessing
import os
import subprocess
from typing import List
import sys
import time
from aido_utils import get_device_list, show_status
OUTPUT_DIR = ""
starting_time = 0
ending_time = 0
def copy_bags_device(device):
global OUTPUT_DIR
global starting_time
global ending_time
print (device+": Starting clipping")
print(starting_time)
print(ending_time)
cmd = "docker -H %s.local rm -f clipper || echo bla && docker -H %s.local run --rm -dit --net host \
--name clipper -v /data/logs:/data/logs duckietown/rpi-duckiebot-base:master19 \
/bin/bash -c \"cd /data/logs; \
source /home/software/catkin_ws/devel/setup.bash; \
for file in *; do if [[ \$file != *\"clipped\"* && \$file != *\".orig.\"* ]]; then \
if [ ! -f clipped_\$file ]; then rosbag reindex \$file; fi; fi; done; \
for file in *; do if [[ \$file != *\"clipped\"* && \$file != *\".orig.\"* ]]; then \
rosbag info \$file || sudo unlink *.orig.* || echo bla && rosbag info \$file || rosbag reindex \$file ; fi; done; \
for file in *; do if [[ \$file != *\"clipped\"* && \$file != *\".orig.\"* ]]; then \
rosbag info \$file || sudo unlink *.orig.* || echo bla && rosbag info \$file || rosbag reindex \$file ; fi; done; \
for file in *; do if [[ \$file != *\"clipped\"* && \$file != *\".orig.\"* ]]; then \
sudo mv \$file \${file%%.active} || echo bla && sudo unlink *.orig.* || echo blabla; fi; done; \
for file in *; do if [[ \$file != *\"clipped\"* && \$file != *\".orig.\"* ]]; then \
rosbag info \$file && rosbag filter \$file clipped_\$file \' %f <= t.to_sec() <= %f \' && sudo rm -f \$file; fi; done \" " % (device, device, starting_time, ending_time)
try:
error = subprocess.check_output(
cmd, shell=True, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as error:
return "Error while attempting to clip bags: %s" % error.output.decode("utf-8")
time.sleep(2)
try:
print (device+": Inspecting clipping status")
while True:
cmd = 'docker -H %s.local inspect -f \'{{.State.Running}}\' clipper' % device
res = subprocess.check_output(
cmd, shell=True, stderr=subprocess.STDOUT)
if res.decode("utf-8") == "false\n":
break
time.sleep(0.5)
except Exception as e:
res = e.output.decode("utf-8")
if "No such object" in res:
pass
else:
print(res)
print (device+": Starting copying")
cmd = 'rsync -avz --block-size=131072 --protocol=29 --partial-dir=.rsync-partial %s:/data/logs/ %s/' % (
device, OUTPUT_DIR)
print (cmd)
try:
subprocess.check_output(cmd, shell=True, stderr=subprocess.PIPE)
except subprocess.CalledProcessError as e:
return "Copy failed : %s" % e.output.decode("utf-8")
return "Success"
def list_files():
global OUTPUT_DIR
OUTPUTDIR = os.path.abspath(OUTPUT_DIR)
while True:
files = os.listdir(OUTPUTDIR)
string = "-------------------------------------------------------\n"
for f in sorted(files):
if (os.path.isfile(os.path.join(OUTPUTDIR, f))):
f_size = os.path.getsize(os.path.join(OUTPUTDIR, f))
string += "%s : %9.3f MBs\n" % (f,
float(f_size)/(1024.0*1024.0))
sys.stdout.write(string)
sys.stdout.flush()
time.sleep(1)
def copy_bags_all_devices(device_list):
global OUTPUT_DIR
global starting_time
global ending_time
p = multiprocessing.Process(target=list_files)
p.start()
pool = multiprocessing.Pool(processes=20)
results = pool.map(copy_bags_device, device_list)
pool.close()
pool.join()
time.sleep(5)
p.terminate()
p.join()
print()
show_status(device_list, results)
return results
def copy_bags_main_with_list(device_list, outputdir, starting_time_in, ending_time_in):
global OUTPUT_DIR
global starting_time
global ending_time
OUTPUT_DIR = outputdir
starting_time = starting_time_in
ending_time = ending_time_in
if OUTPUT_DIR == None:
OUTPUT_DIR = '/data/bags'
if not os.path.exists(OUTPUT_DIR):
os.makedirs(OUTPUT_DIR)
print('Copying bags:')
return device_list, copy_bags_all_devices(device_list)
test1, test2 = copy_bags_main_with_list({"watchtower22"}, None, 1564749866240, 1564749885589)