From a96dede6329b738d36a70f16b4b8d44df31eb7c4 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Tue, 4 Jul 2017 02:37:00 +0200 Subject: [PATCH 1/2] dash_open: generalized and renamed open_file_or_stdin --- src/borg/crypto/keymanager.py | 4 ++-- src/borg/helpers.py | 9 ++++----- src/borg/testsuite/helpers.py | 10 ++++++---- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/borg/crypto/keymanager.py b/src/borg/crypto/keymanager.py index 5f7e5978f..a997c0db6 100644 --- a/src/borg/crypto/keymanager.py +++ b/src/borg/crypto/keymanager.py @@ -4,7 +4,7 @@ from binascii import unhexlify, a2b_base64, b2a_base64 from hashlib import sha256 -from ..helpers import Manifest, NoManifestError, Error, yes, bin_to_hex, open_file_or_stdin +from ..helpers import Manifest, NoManifestError, Error, yes, bin_to_hex, dash_open from ..repository import Repository from .key import KeyfileKey, KeyfileNotFoundError, KeyBlobStorage, identify_key @@ -130,7 +130,7 @@ def grouped(s): def import_keyfile(self, args): file_id = KeyfileKey.FILE_ID first_line = file_id + ' ' + bin_to_hex(self.repository.id) + '\n' - with open_file_or_stdin(args.path, 'r') as fd: + with dash_open(args.path, 'r') as fd: file_first_line = fd.read(len(first_line)) if file_first_line != first_line: if not file_first_line.startswith(file_id): diff --git a/src/borg/helpers.py b/src/borg/helpers.py index 31e4e379a..34d7fbd05 100644 --- a/src/borg/helpers.py +++ b/src/borg/helpers.py @@ -2178,12 +2178,11 @@ def popen_with_error_handling(cmd_line: str, log_prefix='', **kwargs): return -def open_file_or_stdin(path, mode): +def dash_open(path, mode): + assert '+' not in mode # the streams are either r or w, but never both if path == '-': - if 'b' in mode: - return sys.stdin.buffer - else: - return sys.stdin + stream = sys.stdin if 'r' in mode else sys.stdout + return stream.buffer if 'b' in mode else stream else: return open(path, mode) diff --git a/src/borg/testsuite/helpers.py b/src/borg/testsuite/helpers.py index 9dba4d5e0..2a4160d2c 100644 --- a/src/borg/testsuite/helpers.py +++ b/src/borg/testsuite/helpers.py @@ -28,7 +28,7 @@ from ..helpers import chunkit from ..helpers import safe_ns, safe_s, SUPPORT_32BIT_PLATFORMS from ..helpers import popen_with_error_handling -from ..helpers import open_file_or_stdin +from ..helpers import dash_open from . import BaseTestCase, FakeInputs @@ -945,6 +945,8 @@ def test_shell(self): popen_with_error_handling('', shell=True) -def test_open_file_or_stdin(): - assert open_file_or_stdin('-', 'r') is sys.stdin - assert open_file_or_stdin('-', 'rb') is sys.stdin.buffer +def test_dash_open(): + assert dash_open('-', 'r') is sys.stdin + assert dash_open('-', 'w') is sys.stdout + assert dash_open('-', 'rb') is sys.stdin.buffer + assert dash_open('-', 'wb') is sys.stdout.buffer From e728d102ec4dd87b8ce06446b493d1c2487ef506 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Tue, 4 Jul 2017 02:56:50 +0200 Subject: [PATCH 2/2] use dash_open --- src/borg/archiver.py | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/borg/archiver.py b/src/borg/archiver.py index f84fbe4f0..f83c7e0bf 100644 --- a/src/borg/archiver.py +++ b/src/borg/archiver.py @@ -65,6 +65,7 @@ from .helpers import replace_placeholders from .helpers import ChunkIteratorFileWrapper from .helpers import popen_with_error_handling +from .helpers import dash_open from .nanorst import rst_to_terminal from .patterns import ArgparsePatternAction, ArgparseExcludeFileAction, ArgparsePatternFileAction, parse_exclude_pattern from .patterns import PatternMatcher @@ -778,10 +779,8 @@ def do_export_tar(self, args, repository, manifest, key, archive): else: filter = args.tar_filter - if args.tarfile == '-': - tarstream, tarstream_close = sys.stdout.buffer, False - else: - tarstream, tarstream_close = open(args.tarfile, 'wb'), True + tarstream = dash_open(args.tarfile, 'wb') + tarstream_close = args.tarfile != '-' if filter: # When we put a filter between us and the final destination, @@ -1737,11 +1736,8 @@ def output(fd): fd.write('\n') fd.write(' ]\n}\n') - if args.path == '-': - output(sys.stdout) - else: - with open(args.path, 'w') as fd: - output(fd) + with dash_open(args.path, 'w') as fd: + output(fd) return EXIT_SUCCESS @with_repository(compatibility=Manifest.NO_OPERATION_CHECK) @@ -1752,11 +1748,8 @@ def do_debug_dump_manifest(self, args, repository, manifest, key): meta = prepare_dump_dict(msgpack.fallback.unpackb(data, object_hook=StableDict, unicode_errors='surrogateescape')) - if args.path == '-': - json.dump(meta, sys.stdout, indent=4) - else: - with open(args.path, 'w') as fd: - json.dump(meta, fd, indent=4) + with dash_open(args.path, 'w') as fd: + json.dump(meta, fd, indent=4) return EXIT_SUCCESS @with_repository(compatibility=Manifest.NO_OPERATION_CHECK)