allow decryption from std{in,out} via crypto.py
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Tue, 4 Apr 2017 06:54:31 +0000 (08:54 +0200)
committerPhilipp Gesang <philipp.gesang@intra2net.com>
Fri, 28 Apr 2017 12:40:14 +0000 (14:40 +0200)
Make it possible to invoke the script as

    $ ./crypto.py test1234 - - <bfull-2017-04-04-0856-001.tar.pdtcrypt >out.tar.gz

for extra convenience.

deltatar/crypto.py

index 8363ae7..838c681 100755 (executable)
@@ -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)