mirror of
https://github.com/borgbackup/borg.git
synced 2026-06-12 02:17:54 -04:00
dash_open: generalized and renamed open_file_or_stdin
This commit is contained in:
parent
a0d849693e
commit
a96dede632
3 changed files with 12 additions and 11 deletions
|
|
@ -4,7 +4,7 @@ import textwrap
|
|||
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 @@ class KeyManager:
|
|||
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):
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ from ..helpers import swidth_slice
|
|||
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 @@ class TestPopenWithErrorHandling:
|
|||
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
|
||||
|
|
|
|||
Loading…
Reference in a new issue