commit
87928205a8
|
@ -766,10 +766,10 @@ class GPGBase(object):
|
||||||
**recipients** keys. If False, display trust
|
**recipients** keys. If False, display trust
|
||||||
warnings. (default: True)
|
warnings. (default: True)
|
||||||
|
|
||||||
:param str output: The output file to write to. If not specified, the
|
:type output: str or file-like object
|
||||||
encrypted output is returned, and thus should be
|
:param output: The output file to write to. If not specified, the
|
||||||
stored as an object in Python. For example:
|
encrypted output is returned, and thus should be stored
|
||||||
|
as an object in Python. For example:
|
||||||
|
|
||||||
>>> import shutil
|
>>> import shutil
|
||||||
>>> import gnupg
|
>>> import gnupg
|
||||||
|
@ -808,17 +808,23 @@ class GPGBase(object):
|
||||||
"""
|
"""
|
||||||
args = []
|
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 output:
|
||||||
if getattr(output, 'fileno', None) is not None:
|
if getattr(output, 'fileno', None) is not None:
|
||||||
## avoid overwrite confirmation message
|
## avoid overwrite confirmation message
|
||||||
if getattr(output, 'name', None) is None:
|
if getattr(output, 'name', None) is not None:
|
||||||
if os.path.exists(output):
|
output_filename = output.name
|
||||||
os.remove(output)
|
|
||||||
args.append('--output %s' % output)
|
|
||||||
else:
|
|
||||||
if os.path.exists(output.name):
|
if os.path.exists(output.name):
|
||||||
os.remove(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 armor: args.append('--armor')
|
||||||
if always_trust: args.append('--always-trust')
|
if always_trust: args.append('--always-trust')
|
||||||
|
@ -877,4 +883,12 @@ class GPGBase(object):
|
||||||
self._handle_io(args, data, result,
|
self._handle_io(args, data, result,
|
||||||
passphrase=passphrase, binary=True)
|
passphrase=passphrase, binary=True)
|
||||||
log.debug("\n%s" % result.data)
|
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
|
return result
|
||||||
|
|
|
@ -990,6 +990,49 @@ know, maybe you shouldn't be doing it in the first place.
|
||||||
log.debug("new (from decryption): %r" % ddata)
|
log.debug("new (from decryption): %r" % ddata)
|
||||||
self.assertEqual(data, 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',
|
suites = { 'parsers': set(['test_parsers_fix_unsafe',
|
||||||
'test_parsers_fix_unsafe_semicolon',
|
'test_parsers_fix_unsafe_semicolon',
|
||||||
|
@ -1034,7 +1077,9 @@ suites = { 'parsers': set(['test_parsers_fix_unsafe',
|
||||||
'test_encryption_decryption_multi_recipient',
|
'test_encryption_decryption_multi_recipient',
|
||||||
'test_decryption',
|
'test_decryption',
|
||||||
'test_symmetric_encryption_and_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']),
|
'listkeys': set(['test_list_keys_after_generation']),
|
||||||
'keyrings': set(['test_public_keyring',
|
'keyrings': set(['test_public_keyring',
|
||||||
'test_secret_keyring',
|
'test_secret_keyring',
|
||||||
|
|
Loading…
Reference in New Issue