Browse code

Small bugfixes and cleanup

Lorenz Hüdepohl authored on 03/11/2017 14:17:55
Showing 1 changed files
... ...
@@ -6,7 +6,6 @@ import socket
6 6
 import argparse
7 7
 from shutil import which
8 8
 from subprocess import Popen, PIPE, run
9
-from contextlib import contextmanager
10 9
 from collections import namedtuple
11 10
 
12 11
 sys.tracebacklimit = 0
... ...
@@ -36,7 +35,7 @@ parser.add_argument('-c', '--cache-dir', metavar="DIR",
36 35
                          'then rsync that to the remote host. With that, one can resume '
37 36
                          'a partially sent file without starting over')
38 37
 
39
-parser.add_argument('-m', '--min-cache-size', default="1M",
38
+parser.add_argument('-m', '--min-cache-size', default="50M",
40 39
                     help='Minimum (estimated) size of stream to use the cache-dir funcitonality')
41 40
 
42 41
 parser.add_argument('-v', '--verbose', action="append_const", const=1,
... ...
@@ -138,7 +137,7 @@ def rsync(orig_url, dest_url):
138 137
         progress = ["--info=progress2"]
139 138
     else:
140 139
         progress = []
141
-    with echoPopen(["rsync", "--partial"] + progress + ["--inplace", orig_url, dest_url]) as rsync:
140
+    with echoPopen(["rsync", "--append"] + progress + ["--inplace", orig_url, dest_url]) as rsync:
142 141
         rsync.wait()
143 142
         check_returncode(rsync)
144 143
 
... ...
@@ -171,36 +170,40 @@ def touch(url):
171 170
     return command_on_url("touch", url, dry_run=args.dry_run, echo=args.dry_run or args.verbose)
172 171
 
173 172
 def rm(url):
174
-    return command_on_url("rm", url, dry_run=args.dry_run, echo=args.dry_run or args.verbose)
173
+    return command_on_url("rm", "-v", url, dry_run=args.dry_run, echo=args.dry_run or args.verbose)
175 174
 
176 175
 MockStdout = namedtuple("MockStdout", ["cmd_string"])
177
-MockPopen = namedtuple("MockPopen", ["stdout", "returncode", "wait"])
178
-def mockWait():
179
-    pass
180 176
 
181
-@contextmanager
182
-def echoPopen(commands, stdin=None, stdout=None, **kwargs):
183
-    if args.verbose or args.dry_run:
184
-        if stdin is not None:
185
-            in_pipe = stdin.cmd_string + " | "
186
-        else:
187
-            in_pipe = ""
188
-        if commands[0] == "ssh":
189
-            cmd = " ".join(commands[0:2]) + ' "' + " ".join(commands[2:]) + '"'
190
-        else:
191
-            cmd = " ".join(commands)
192
-        cmd_string = in_pipe + cmd
193
-        if stdout is None:
194
-            print(cmd_string)
177
+class echoPopen(Popen):
178
+    def __init__(self, commands, stdin=None, stdout=None, **kwargs):
179
+        if args.verbose or args.dry_run:
180
+            if stdin is not None:
181
+                in_pipe = stdin.cmd_string + " | "
182
+            else:
183
+                in_pipe = ""
184
+            if commands[0] == "ssh":
185
+                cmd = " ".join(commands[0:2]) + ' "' + " ".join(commands[2:]) + '"'
186
+            else:
187
+                cmd = " ".join(commands)
188
+            cmd_string = in_pipe + cmd
189
+            if stdout is None:
190
+                print(cmd_string)
195 191
 
196
-    if not args.dry_run:
197
-        with Popen(commands, stdin=stdin, stdout=stdout, **kwargs) as proc:
192
+        if not args.dry_run:
193
+            super().__init__(commands, stdin=stdin, stdout=stdout, **kwargs)
198 194
             if stdout is not None:
199
-                proc.stdout.cmd_string = cmd_string
200
-            yield proc
201
-    else:
202
-        yield MockPopen(MockStdout(cmd_string), 0, mockWait)
195
+                self.stdout.cmd_string = cmd_string
196
+        else:
197
+            self.stdout = MockStdout(cmd_string)
198
+            self.returncode = 0
199
+
200
+    def wait(self):
201
+        if not args.dry_run:
202
+            super().wait()
203 203
 
204
+    def __exit__(self, type, value, traceback):
205
+        if not args.dry_run:
206
+            return super().__exit__(type, value, traceback)
204 207
 
205 208
 for fs, snapname in origin_snapshots:
206 209
     if fs in destinations and snapname in destinations[fs]:
... ...
@@ -280,7 +283,7 @@ for fs, snapname in origin_snapshots:
280 283
     if use_cache_dir:
281 284
         pre_pipe = ["cat", dest_cache_filename, "|"] + pre_pipe
282 285
 
283
-    recv_cmd = ssh_dest + pre_pipe + ["zfs", "recv", "-u", destination + fs]
286
+    recv_cmd = ssh_dest + pre_pipe + ["zfs", "recv"] + verbose + ["-u", destination + fs]
284 287
 
285 288
     send_shell = not ssh_orig
286 289
     if send_shell:
... ...
@@ -306,7 +309,7 @@ for fs, snapname in origin_snapshots:
306 309
         recv_stdin = None
307 310
     else:
308 311
         # direct `zfs send | zfs recv` pipe
309
-        sender = echoPopen(send_cmd, shell=send_shell)
312
+        sender = echoPopen(send_cmd, stdout=PIPE, shell=send_shell)
310 313
         recv_stdin = sender.stdout
311 314
 
312 315
     # recv