summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichał Górny <mgorny@gentoo.org>2017-11-27 15:06:59 +0100
committerMichał Górny <mgorny@gentoo.org>2017-11-27 15:06:59 +0100
commit912fd592c8b97a309ebf9fb261abe52ea1a10c2d (patch)
treec06e5ff1ea99602b4c25be4e33172c743980dfe7
parent57684e54e2a2f402f02ba055d11bea07a710f52e (diff)
downloadgemato-912fd592c8b97a309ebf9fb261abe52ea1a10c2d.tar.gz
openpgp: Terminate gpg-agent only if gpg was actually called
-rw-r--r--gemato/openpgp.py16
1 files changed, 12 insertions, 4 deletions
diff --git a/gemato/openpgp.py b/gemato/openpgp.py
index 3408087..6df28de 100644
--- a/gemato/openpgp.py
+++ b/gemato/openpgp.py
@@ -28,6 +28,9 @@ def _spawn_gpg(options, env_instance, stdin):
else:
raise
+ if env_instance is not None:
+ env_instance._set_started()
+
out, err = p.communicate(stdin)
return (p.wait(), out, err)
@@ -41,10 +44,11 @@ class OpenPGPEnvironment(object):
or use as a context manager (via 'with').
"""
- __slots__ = ['_home']
+ __slots__ = ['_home', '_started']
def __init__(self):
self._home = tempfile.mkdtemp()
+ self._started = False
def __enter__(self):
return self
@@ -53,6 +57,9 @@ class OpenPGPEnvironment(object):
if self._home is not None:
self.close()
+ def _set_started(self):
+ self._started = True
+
@staticmethod
def _rmtree_error_handler(func, path, exc_info):
# ignore ENOENT -- it probably means a race condition between
@@ -63,9 +70,10 @@ class OpenPGPEnvironment(object):
def close(self):
if self._home is not None:
- # terminate the agent spawned by the process
- subprocess.Popen(['gpgconf', '--kill', 'all'],
- env={'GNUPGHOME': self._home}).wait()
+ if self._started:
+ # terminate the agent spawned by the process
+ subprocess.Popen(['gpgconf', '--kill', 'all'],
+ env={'GNUPGHOME': self._home}).wait()
shutil.rmtree(self._home, onerror=self._rmtree_error_handler)
self._home = None