Merge branch 'fix/24-output-to-filename' into develop

fix/44-verbose-arg 1.3.1
Isis Lovecruft 2014-08-02 04:18:27 +00:00
commit 87928205a8
2 changed files with 70 additions and 11 deletions

View File

@ -766,10 +766,10 @@ class GPGBase(object):
**recipients** keys. If False, display trust
warnings. (default: True)
:param str output: The output file to write to. If not specified, the
encrypted output is returned, and thus should be
stored as an object in Python. For example:
:type output: str or file-like object
:param output: The output file to write to. If not specified, the
encrypted output is returned, and thus should be stored
as an object in Python. For example:
>>> import shutil
>>> import gnupg
@ -808,17 +808,23 @@ class GPGBase(object):
"""
args = []
## FIXME: GnuPG appears to ignore the --output directive when being
## programmatically driven. We'll handle the IO ourselves to fix this
## for now.
output_filename = None
if output:
if getattr(output, 'fileno', None) is not None:
## avoid overwrite confirmation message
if getattr(output, 'name', None) is None:
if os.path.exists(output):
os.remove(output)
args.append('--output %s' % output)
else:
if getattr(output, 'name', None) is not None:
output_filename = output.name
if os.path.exists(output.name):
os.remove(output.name)
args.append('--output %s' % output.name)
#args.append('--output %s' % output.name)
else:
output_filename = output
if os.path.exists(output):
os.remove(output)
#args.append('--output %s' % output)
if armor: args.append('--armor')
if always_trust: args.append('--always-trust')
@ -877,4 +883,12 @@ class GPGBase(object):
self._handle_io(args, data, result,
passphrase=passphrase, binary=True)
log.debug("\n%s" % result.data)
if output_filename:
log.info("Writing encrypted output to file: %s" % output_filename)
with open(output_filename, 'w+') as fh:
fh.write(result.data)
fh.flush()
log.info("Encrypted output written successfully.")
return result

View File

@ -990,6 +990,49 @@ know, maybe you shouldn't be doing it in the first place.
log.debug("new (from decryption): %r" % ddata)
self.assertEqual(data, ddata)
def test_encryption_to_filename(self):
"""Test that ``encrypt(..., output='somefile.gpg')`` is successful."""
with open(os.path.join(_files, 'kat.sec')) as katsec:
self.gpg.import_keys(katsec.read())
fpr = self.gpg.list_keys('kat')[0]['fingerprint']
output = os.path.join(self.gpg.homedir, 'test-encryption-to-filename.gpg')
message_filename = os.path.join(_files, 'cypherpunk_manifesto')
message_file = open(message_filename)
message = message_file.read()
message_file.close()
encrypted = self.gpg.encrypt(message, fpr, output=output)
self.assertTrue(encrypted.ok)
self.assertTrue(os.path.isfile(output))
# Check the contents:
with open(output) as fh:
encrypted_message = fh.read()
log.debug("Encrypted file contains:\n\n%s\n" % encrypted_message)
def test_encryption_to_filehandle(self):
"""Test that ``encrypt(..., output=filelikething)`` is successful."""
with open(os.path.join(_files, 'kat.sec')) as katsec:
self.gpg.import_keys(katsec.read())
fpr = self.gpg.list_keys('kat')[0]['fingerprint']
output = os.path.join(self.gpg.homedir, 'test-encryption-to-filehandle.gpg')
output_file = open(output, 'w+')
message_filename = os.path.join(_files, 'cypherpunk_manifesto')
message_file = open(message_filename)
message = message_file.read()
message_file.close()
encrypted = self.gpg.encrypt(message, fpr, output=output_file)
self.assertTrue(encrypted.ok)
self.assertTrue(os.path.isfile(output))
# Check the contents:
with open(output) as fh:
encrypted_message = fh.read()
log.debug("Encrypted file contains:\n\n%s\n" % encrypted_message)
suites = { 'parsers': set(['test_parsers_fix_unsafe',
'test_parsers_fix_unsafe_semicolon',
@ -1034,7 +1077,9 @@ suites = { 'parsers': set(['test_parsers_fix_unsafe',
'test_encryption_decryption_multi_recipient',
'test_decryption',
'test_symmetric_encryption_and_decryption',
'test_file_encryption_and_decryption']),
'test_file_encryption_and_decryption',
'test_encryption_to_filename',
'test_encryption_to_filehandle',]),
'listkeys': set(['test_list_keys_after_generation']),
'keyrings': set(['test_public_keyring',
'test_secret_keyring',