From 56a0a1f584e15c3a21ddae1f2031ea57444c3e11 Mon Sep 17 00:00:00 2001 From: Shannon Mulloy Date: Sat, 4 Feb 2017 19:27:09 -0800 Subject: [PATCH 1/3] rx: allow overwrite with --output-file= #73 --- src/wormhole/cli/cmd_receive.py | 7 +++++-- src/wormhole/test/test_scripts.py | 14 +++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/wormhole/cli/cmd_receive.py b/src/wormhole/cli/cmd_receive.py index fb43412..537582a 100644 --- a/src/wormhole/cli/cmd_receive.py +++ b/src/wormhole/cli/cmd_receive.py @@ -257,8 +257,11 @@ class TwistedReceiver: # get confirmation from the user before writing to the local directory if os.path.exists(abs_destname): - self._msg(u"Error: refusing to overwrite existing '%s'" % destname) - raise TransferRejectedError() + if self.args.output_file: # overwrite is intentional + self._msg(u"Overwriting '%s'" % destname) + else: + self._msg(u"Error: refusing to overwrite existing '%s'" % destname) + raise TransferRejectedError() return abs_destname def _ask_permission(self): diff --git a/src/wormhole/test/test_scripts.py b/src/wormhole/test/test_scripts.py index c5e2ede..a354b62 100644 --- a/src/wormhole/test/test_scripts.py +++ b/src/wormhole/test/test_scripts.py @@ -238,7 +238,7 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase): @inlineCallbacks def _do_test(self, as_subprocess=False, mode="text", addslash=False, override_filename=False, - fake_tor=False): + fake_tor=False, overwrite=False): assert mode in ("text", "file", "empty-file", "directory", "slow-text") if fake_tor: assert not as_subprocess @@ -275,6 +275,11 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase): recv_cfg.accept_file = True if override_filename: recv_cfg.output_file = receive_filename = "outfile" + if overwrite: + recv_cfg.output_file = receive_filename + existing_file = os.path.join(receive_dir, receive_filename) + with open(existing_file, 'w') as f: + f.write('pls overwrite me') elif mode == "directory": # $send_dir/ @@ -308,6 +313,9 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase): recv_cfg.accept_file = True if override_filename: recv_cfg.output_file = receive_dirname = "outdir" + if overwrite: + recv_cfg.output_file = receive_dirname + os.mkdir(os.path.join(receive_dir, receive_dirname)) if as_subprocess: wormhole_bin = self.find_executable() @@ -503,6 +511,8 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase): return self._do_test(mode="file") def test_file_override(self): return self._do_test(mode="file", override_filename=True) + def test_file_overwrite(self): + return self._do_test(mode="file", overwrite=True) def test_file_tor(self): return self._do_test(mode="file", fake_tor=True) def test_empty_file(self): @@ -514,6 +524,8 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase): return self._do_test(mode="directory", addslash=True) def test_directory_override(self): return self._do_test(mode="directory", override_filename=True) + def test_directory_overwrite(self): + return self._do_test(mode="directory", overwrite=True) def test_slow_text(self): return self._do_test(mode="slow-text") From 33526b21803e15f46cc8d76c95c4bd7ea9b4c226 Mon Sep 17 00:00:00 2001 From: Shannon Mulloy Date: Sun, 5 Feb 2017 13:29:20 -0800 Subject: [PATCH 2/3] rx: _remove_existing after accept --- src/wormhole/cli/cmd_receive.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/wormhole/cli/cmd_receive.py b/src/wormhole/cli/cmd_receive.py index 537582a..c6dacbc 100644 --- a/src/wormhole/cli/cmd_receive.py +++ b/src/wormhole/cli/cmd_receive.py @@ -1,5 +1,5 @@ from __future__ import print_function -import os, sys, six, tempfile, zipfile, hashlib +import os, sys, six, tempfile, zipfile, hashlib, shutil from tqdm import tqdm from humanize import naturalsize from twisted.internet import reactor @@ -259,16 +259,26 @@ class TwistedReceiver: if os.path.exists(abs_destname): if self.args.output_file: # overwrite is intentional self._msg(u"Overwriting '%s'" % destname) + if self.args.accept_file: + self._remove_existing(abs_destname) else: self._msg(u"Error: refusing to overwrite existing '%s'" % destname) raise TransferRejectedError() return abs_destname + def _remove_existing(self, path): + if os.path.isfile(path): + os.remove(path) + elif os.path.isdir(path): + shutil.rmtree(path) + def _ask_permission(self): with self.args.timing.add("permission", waiting="user") as t: while True and not self.args.accept_file: ok = six.moves.input("ok? (y/n): ") if ok.lower().startswith("y"): + if os.path.exists(self.abs_destname): + self._remove_existing(self.abs_destname) break print(u"transfer rejected", file=sys.stderr) t.detail(answer="no") From ffefb5ebdc91578755b5c323ff44d7e9cb666b9f Mon Sep 17 00:00:00 2001 From: Shannon Mulloy Date: Sun, 5 Feb 2017 15:42:49 -0800 Subject: [PATCH 3/3] improve coverage with input mock --- src/wormhole/cli/cmd_receive.py | 6 ++---- src/wormhole/test/test_scripts.py | 13 ++++++++++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/wormhole/cli/cmd_receive.py b/src/wormhole/cli/cmd_receive.py index c6dacbc..c262b9a 100644 --- a/src/wormhole/cli/cmd_receive.py +++ b/src/wormhole/cli/cmd_receive.py @@ -267,10 +267,8 @@ class TwistedReceiver: return abs_destname def _remove_existing(self, path): - if os.path.isfile(path): - os.remove(path) - elif os.path.isdir(path): - shutil.rmtree(path) + if os.path.isfile(path): os.remove(path) + if os.path.isdir(path): shutil.rmtree(path) def _ask_permission(self): with self.args.timing.add("permission", waiting="user") as t: diff --git a/src/wormhole/test/test_scripts.py b/src/wormhole/test/test_scripts.py index a354b62..49562e0 100644 --- a/src/wormhole/test/test_scripts.py +++ b/src/wormhole/test/test_scripts.py @@ -238,7 +238,7 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase): @inlineCallbacks def _do_test(self, as_subprocess=False, mode="text", addslash=False, override_filename=False, - fake_tor=False, overwrite=False): + fake_tor=False, overwrite=False, mock_accept=False): assert mode in ("text", "file", "empty-file", "directory", "slow-text") if fake_tor: assert not as_subprocess @@ -272,7 +272,7 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase): send_cfg.what = send_filename receive_filename = send_filename - recv_cfg.accept_file = True + recv_cfg.accept_file = False if mock_accept else True if override_filename: recv_cfg.output_file = receive_filename = "outfile" if overwrite: @@ -310,7 +310,7 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase): send_cfg.what = send_dirname_arg receive_dirname = send_dirname - recv_cfg.accept_file = True + recv_cfg.accept_file = False if mock_accept else True if override_filename: recv_cfg.output_file = receive_dirname = "outdir" if overwrite: @@ -395,6 +395,9 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase): with mock.patch.object(cmd_send, "VERIFY_TIMER", 0), \ mock.patch.object(cmd_receive, "VERIFY_TIMER", 0): yield gatherResults([send_d, receive_d], True) + elif mock_accept: + with mock.patch.object(cmd_receive.six.moves, 'input', return_value='y'): + yield gatherResults([send_d, receive_d], True) else: yield gatherResults([send_d, receive_d], True) @@ -513,6 +516,8 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase): return self._do_test(mode="file", override_filename=True) def test_file_overwrite(self): return self._do_test(mode="file", overwrite=True) + def test_file_overwrite_mock_accept(self): + return self._do_test(mode="file", overwrite=True, mock_accept=True) def test_file_tor(self): return self._do_test(mode="file", fake_tor=True) def test_empty_file(self): @@ -526,6 +531,8 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase): return self._do_test(mode="directory", override_filename=True) def test_directory_overwrite(self): return self._do_test(mode="directory", overwrite=True) + def test_directory_overwrite_mock_accept(self): + return self._do_test(mode="directory", overwrite=True, mock_accept=True) def test_slow_text(self): return self._do_test(mode="slow-text")