From 5a654fbb7918467e4f8338756d1535c6f9768a94 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Tue, 4 Apr 2017 08:54:31 +0200 Subject: [PATCH] allow decryption from std{in,out} via crypto.py Make it possible to invoke the script as $ ./crypto.py test1234 - - out.tar.gz for extra convenience. --- deltatar/crypto.py | 26 ++++++++++++++++++++++++-- 1 files changed, 24 insertions(+), 2 deletions(-) diff --git a/deltatar/crypto.py b/deltatar/crypto.py index 8363ae7..838c681 100755 --- a/deltatar/crypto.py +++ b/deltatar/crypto.py @@ -680,6 +680,28 @@ def depdtcrypt (pw, ins, outs, verbose): pt = decr.process (ct) out (pt) +PDT_SINK = 0 +PDT_SOURCE = 1 + +def deptdcrypt_mk_stream (kind, path, verbose=False): + """Create stream from file or stdio descriptor.""" + if kind == PDT_SINK: + if path == "-": + if verbose is True: noise ("PDT: sink: stdout") + return sys.stdout.buffer + else: + if verbose is True: noise ("PDT: sink: file %s" % path) + return io.FileIO (path, "w") + if kind == PDT_SOURCE: + if path == "-": + if verbose is True: noise ("PDT: source: stdin") + return sys.stdin.buffer + else: + if verbose is True: noise ("PDT: source: file %s" % path) + return io.FileIO (path, "r") + + raise ValueError ("bogus stream “%s” / %s" % (kind, path)) + def depdtcrypt_file (pw, spath, dpath, verbose=False): """ @@ -688,8 +710,8 @@ def depdtcrypt_file (pw, spath, dpath, verbose=False): """ if verbose is True: noise ("PDT: decrypt %s → %s" % (spath, dpath), file=sys.stderr) - with io.FileIO (spath, "r") as ins: - with io.FileIO (dpath, "w") as outs: + with deptdcrypt_mk_stream (PDT_SOURCE, spath) as ins: + with deptdcrypt_mk_stream (PDT_SINK, dpath) as outs: return depdtcrypt (pw, ins, outs, verbose) -- 1.7.1