Merge pull request #3715 from milkey-mouse/getpass-ux-bp1.1

improve getpass user experience (1.1 backport)
This commit is contained in:
TW 2018-03-24 21:35:23 +01:00 committed by GitHub
commit 8c40c19508
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 1 deletions

View File

@ -32,6 +32,10 @@ from .low_level import AES, bytes_to_long, bytes_to_int, num_aes_blocks, hmac_sh
PREFIX = b'\0' * 8
class NoPassphraseFailure(Error):
"""can not acquire a passphrase: {}"""
class PassphraseWrong(Error):
"""passphrase supplied in BORG_PASSPHRASE or by BORG_PASSCOMMAND is incorrect."""
@ -451,7 +455,19 @@ class Passphrase(str):
@classmethod
def getpass(cls, prompt):
return cls(getpass.getpass(prompt))
try:
pw = getpass.getpass(prompt)
except EOFError:
if prompt:
print() # avoid err msg appearing right of prompt
msg = []
for env_var in 'BORG_PASSPHRASE', 'BORG_PASSCOMMAND':
env_var_set = os.environ.get(env_var) is not None
msg.append('%s is %s.' % (env_var, 'set' if env_var_set else 'not set'))
msg.append('Interactive password query failed.')
raise NoPassphraseFailure(' '.join(msg)) from None
else:
return cls(pw)
@classmethod
def verification(cls, passphrase):