diff --git a/src/wormhole/scripts/cmd_send_blocking.py b/src/wormhole/scripts/cmd_send_blocking.py index a79f8f8..29349e5 100644 --- a/src/wormhole/scripts/cmd_send_blocking.py +++ b/src/wormhole/scripts/cmd_send_blocking.py @@ -2,10 +2,20 @@ from __future__ import print_function import json, binascii, six from ..errors import TransferError from .progress import ProgressPrinter +from ..blocking.transcribe import Wormhole, WrongPasswordError +from ..blocking.transit import TransitSender +from ..errors import handle_server_error +from .send_common import (APPID, handle_zero, build_other_command, + build_phase1_data) -def send_blocking(appid, args, phase1, fd_to_send): - from ..blocking.transcribe import Wormhole, WrongPasswordError - from ..blocking.transit import TransitSender +@handle_server_error +def send_blocking(args): + assert isinstance(args.relay_url, type(u"")) + handle_zero(args) + phase1, fd_to_send = build_phase1_data(args) + other_cmd = build_other_command(args) + print(u"On the other computer, please run: %s" % other_cmd, + file=args.stdout) transit_sender = TransitSender(args.transit_helper) transit_data = { @@ -14,7 +24,7 @@ def send_blocking(appid, args, phase1, fd_to_send): } phase1["transit"] = transit_data - with Wormhole(appid, args.relay_url) as w: + with Wormhole(APPID, args.relay_url) as w: if args.code: w.set_code(args.code) code = args.code @@ -44,7 +54,7 @@ def send_blocking(appid, args, phase1, fd_to_send): return 0 raise TransferError("error sending text: %r" % (them_phase1,)) - return _send_file_blocking(w, appid, them_phase1, fd_to_send, + return _send_file_blocking(w, them_phase1, fd_to_send, transit_sender, args.stdout, args.hide_progress) def _do_verify(w): @@ -59,7 +69,7 @@ def _do_verify(w): w.send_data(reject_data) raise TransferError("verification rejected, abandoning transfer") -def _send_file_blocking(w, appid, them_phase1, fd_to_send, transit_sender, +def _send_file_blocking(w, them_phase1, fd_to_send, transit_sender, stdout, hide_progress): # we're sending a file, if they accept it @@ -72,7 +82,7 @@ def _send_file_blocking(w, appid, them_phase1, fd_to_send, transit_sender, "transfer abandoned: %s" % (them_phase1,)) tdata = them_phase1["transit"] - transit_key = w.derive_key(appid+"/transit-key") + transit_key = w.derive_key(APPID+"/transit-key") transit_sender.set_transit_key(transit_key) transit_sender.add_their_direct_hints(tdata["direct_connection_hints"]) transit_sender.add_their_relay_hints(tdata["relay_connection_hints"]) diff --git a/src/wormhole/scripts/runner.py b/src/wormhole/scripts/runner.py index f131326..2c02169 100644 --- a/src/wormhole/scripts/runner.py +++ b/src/wormhole/scripts/runner.py @@ -20,8 +20,8 @@ def dispatch(args): from ..servers import cmd_usage return cmd_usage.tail_usage(args) if args.func == "send/send": - from . import cmd_send - return cmd_send.send(args) + from . import cmd_send_blocking + return cmd_send_blocking.send_blocking(args) if args.func == "receive/receive": from . import cmd_receive return cmd_receive.receive(args) diff --git a/src/wormhole/scripts/cmd_send.py b/src/wormhole/scripts/send_common.py similarity index 74% rename from src/wormhole/scripts/cmd_send.py rename to src/wormhole/scripts/send_common.py index e366822..260e201 100644 --- a/src/wormhole/scripts/cmd_send.py +++ b/src/wormhole/scripts/send_common.py @@ -1,13 +1,24 @@ from __future__ import print_function import os, sys, six, tempfile, zipfile -from ..errors import handle_server_error, TransferError +from ..errors import TransferError APPID = u"lothar.com/wormhole/text-or-file-xfer" -@handle_server_error -def send(args): - # we're sending text, or a file/directory - assert isinstance(args.relay_url, type(u"")) +def handle_zero(args): + if args.zeromode: + assert not args.code + args.code = u"0-" + +def build_other_command(args): + other_cmd = "wormhole receive" + if args.verify: + other_cmd = "wormhole --verify receive" + if args.zeromode: + other_cmd += " -0" + return other_cmd + +def build_phase1_data(args): + phase1 = {} text = args.text if text == "-": @@ -20,34 +31,16 @@ def send(args): print(u"Sending text message (%d bytes)" % len(text), file=args.stdout) phase1 = { "message": text } fd_to_send = None - else: - what = os.path.join(args.cwd, args.what) - if not os.path.exists(what): - raise TransferError("Cannot send: no file/directory named '%s'" % - args.what) - phase1, fd_to_send = _build_phase1_data(args) - # transit_sender will be built in twisted/blocking-specific function + return phase1, fd_to_send - if args.zeromode: - assert not args.code - args.code = u"0-" + what = os.path.join(args.cwd, args.what) + if not os.path.exists(what): + raise TransferError("Cannot send: no file/directory named '%s'" % + args.what) - other_cmd = "wormhole receive" - if args.verify: - other_cmd = "wormhole --verify receive" - if args.zeromode: - other_cmd += " -0" - print(u"On the other computer, please run: %s" % other_cmd, - file=args.stdout) - - from .cmd_send_blocking import send_blocking - rc = send_blocking(APPID, args, phase1, fd_to_send) - return rc - -def _build_phase1_data(args): - phase1 = {} what = os.path.join(args.cwd, args.what) basename = os.path.basename(what) + if os.path.isfile(what): # we're sending a file filesize = os.stat(what).st_size @@ -58,7 +51,9 @@ def _build_phase1_data(args): print(u"Sending %d byte file named '%s'" % (filesize, basename), file=args.stdout) fd_to_send = open(what, "rb") - elif os.path.isdir(what): + return phase1, fd_to_send + + if os.path.isdir(what): print(u"Building zipfile..", file=args.stdout) # We're sending a directory. Create a zipfile in a tempdir and # send that. @@ -91,6 +86,6 @@ def _build_phase1_data(args): } print(u"Sending directory (%d bytes compressed) named '%s'" % (filesize, basename), file=args.stdout) - else: - raise TypeError("'%s' is neither file nor directory" % what) - return phase1, fd_to_send + return phase1, fd_to_send + + raise TypeError("'%s' is neither file nor directory" % what) diff --git a/src/wormhole/test/test_scripts.py b/src/wormhole/test/test_scripts.py index 74eb755..3834c8f 100644 --- a/src/wormhole/test/test_scripts.py +++ b/src/wormhole/test/test_scripts.py @@ -6,7 +6,7 @@ from twisted.internet.defer import inlineCallbacks from twisted.internet.threads import deferToThread from .. import __version__ from .common import ServerBase -from ..scripts import runner, cmd_send, cmd_receive +from ..scripts import runner, cmd_send_blocking, cmd_receive class ScriptsBase: def find_executable(self): @@ -172,7 +172,7 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase): rargs.cwd = receive_dir rargs.stdout = io.StringIO() rargs.stderr = io.StringIO() - send_d = deferToThread(cmd_send.send, sargs) + send_d = deferToThread(cmd_send_blocking.send_blocking, sargs) receive_d = deferToThread(cmd_receive.receive, rargs) send_rc = yield send_d