dcavalca / rpms / dnf

Forked from rpms/dnf 2 years ago
Clone
Blob Blame History Raw
diff --git a/dnf/crypto.py b/dnf/crypto.py
index a1839eefe..24a13ed6f 100644
--- a/dnf/crypto.py
+++ b/dnf/crypto.py
@@ -26,12 +26,64 @@
 import dnf.pycomp
 import dnf.util
 import dnf.yum.misc
-import gpg
 import io
 import logging
 import os
 import tempfile
 
+try:
+    from gpg import Context
+    from gpg import Data
+except ImportError:
+    import gpgme
+
+
+    class Context(object):
+        def __init__(self):
+            self.__dict__["ctx"] = gpgme.Context()
+
+        def __enter__(self):
+            return self
+
+        def __exit__(self, type, value, tb):
+            pass
+
+        @property
+        def armor(self):
+            return self.ctx.armor
+
+        @armor.setter
+        def armor(self, value):
+            self.ctx.armor = value
+
+        def op_import(self, key_fo):
+            if isinstance(key_fo, basestring):
+                key_fo = io.BytesIO(key_fo)
+            self.ctx.import_(key_fo)
+
+        def op_export(self, pattern, mode, keydata):
+            self.ctx.export(pattern, keydata)
+
+        def __getattr__(self, name):
+            return getattr(self.ctx, name)
+
+
+    class Data(object):
+        def __init__(self):
+            self.__dict__["buf"] = io.BytesIO()
+
+        def __enter__(self):
+            return self
+
+        def __exit__(self, type, value, tb):
+            pass
+
+        def read(self):
+            return self.buf.getvalue()
+
+        def __getattr__(self, name):
+            return getattr(self.buf, name)
+
 
 GPG_HOME_ENV = 'GNUPGHOME'
 logger = logging.getLogger('dnf')
@@ -67,7 +119,7 @@ def keyids_from_pubring(gpgdir):
     if not os.path.exists(gpgdir):
         return []
 
-    with pubring_dir(gpgdir), gpg.Context() as ctx:
+    with pubring_dir(gpgdir), Context() as ctx:
         keyids = []
         for k in ctx.keylist():
             subkey = _extract_signing_subkey(k)
@@ -101,7 +153,7 @@ def pubring_dir(pubring_dir):
 def rawkey2infos(key_fo):
     pb_dir = tempfile.mkdtemp()
     keyinfos = []
-    with pubring_dir(pb_dir), gpg.Context() as ctx:
+    with pubring_dir(pb_dir), Context() as ctx:
         ctx.op_import(key_fo)
         for key in ctx.keylist():
             subkey = _extract_signing_subkey(key)
@@ -110,7 +162,7 @@ def rawkey2infos(key_fo):
             keyinfos.append(Key(key, subkey))
         ctx.armor = True
         for info in keyinfos:
-            with gpg.Data() as sink:
+            with Data() as sink:
                 ctx.op_export(info.id_, 0, sink)
                 sink.seek(0, os.SEEK_SET)
                 info.raw_key = sink.read()
diff --git a/dnf/yum/misc.py b/dnf/yum/misc.py
index 97f8be94d..248bff884 100644
--- a/dnf/yum/misc.py
+++ b/dnf/yum/misc.py
@@ -32,7 +32,6 @@
 import dnf.i18n
 import errno
 import glob
-import gpg
 import gzip
 import hashlib
 import io
@@ -278,7 +277,7 @@ def import_key_to_pubring(rawkey, keyid, gpgdir=None, make_ro_copy=True):
     if not os.path.exists(gpgdir):
         os.makedirs(gpgdir)
 
-    with dnf.crypto.pubring_dir(gpgdir), gpg.Context() as ctx:
+    with dnf.crypto.pubring_dir(gpgdir), dnf.crypto.Context() as ctx:
         # import the key
         with open(os.path.join(gpgdir, 'gpg.conf'), 'wb') as fp:
             fp.write(b'')