]> TLD Linux GIT Repositories - tld-builder.git/blobdiff - TLD_Builder/gpg.py
- more python3 fixes, dropped python2 support
[tld-builder.git] / TLD_Builder / gpg.py
index aeb8ebd4e524a3cf1da1fe35002a17544a792d02..942cfb54947f1f40923d0fc8c3ce7a27603fd754 100644 (file)
@@ -3,8 +3,8 @@
 import log
 import subprocess
 import re
-import StringIO
-
+import sys
+from io import StringIO
 import util
 import os
 import pipeutil
@@ -16,22 +16,22 @@ def get_keys(buf):
 
     if not os.path.isfile('/usr/bin/gpg'):
         log.error("missing gnupg binary: /usr/bin/gpg")
-        raise OSError, 'Missing gnupg binary'
+        raise OSError('Missing gnupg binary')
 
     d_stdout = None
     d_stderr = None
     cmd = ['/usr/bin/gpg', '--batch', '--no-tty', '--decrypt']
     gpg_run = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
     try:
-        d_stdout, d_stderr = gpg_run.communicate(buf.encode('utf-8'))
-    except OSError, e:
+        d_stdout, d_stderr = gpg_run.communicate(util.to_bytes(buf))
+    except OSError as e:
         log.error("gnupg run, does gpg binary exist? : %s" % e)
         raise
 
-    rx = re.compile("^gpg: Signature made .*using [DR]SA key ID (.+)")
+    rx = re.compile("^gpg:.*using\s[DR]SA\skey\s(?:ID\s)?(\w+)")
     keys = []
 
-    for l in d_stderr.split('\n'):
+    for l in util.to_str(d_stderr).split('\n'):
         m = rx.match(l)
         if m:
             keys.append(m.group(1))
@@ -48,21 +48,21 @@ def verify_sig(buf):
 
     if not os.path.isfile('/usr/bin/gpg'):
         log.error("missing gnupg binary: /usr/bin/gpg")
-        raise OSError, 'Missing gnupg binary'
+        raise OSError('Missing gnupg binary')
 
     d_stdout = None
     d_stderr = None
     cmd = ['/usr/bin/gpg', '--batch', '--no-tty', '--decrypt']
     gpg_run = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
     try:
-        d_stdout, d_stderr = gpg_run.communicate(buf.encode('utf-8'))
-    except OSError, e:
+        d_stdout, d_stderr = gpg_run.communicate(util.to_bytes(buf))
+    except OSError as e:
         log.error("gnupg run failed, does gpg binary exist? : %s" % e)
         raise
 
     rx = re.compile("^gpg: (Good signature from|                aka) .*<([^>]+)>")
     emails = []
-    for l in d_stderr.split('\n'):
+    for l in util.to_str(d_stderr).split('\n'):
         m = rx.match(l)
         if m:
             emails.append(m.group(2))
@@ -73,7 +73,7 @@ def verify_sig(buf):
 def sign(buf):
     if not os.path.isfile('/usr/bin/gpg'):
         log.error("missing gnupg binary: /usr/bin/gpg")
-        raise OSError, 'Missing gnupg binary'
+        raise OSError('Missing gnupg binary')
 
     d_stdout = None
     d_stderr = None
@@ -81,12 +81,12 @@ def sign(buf):
     # TODO: check for gpg return code!
     gpg_run = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
     try:
-        d_stdout, d_stderr = gpg_run.communicate(buf.encode('utf-8'))
-    except OSError, e:
+        d_stdout, d_stderr = gpg_run.communicate(util.to_bytes(buf))
+    except OSError as e:
         log.error("gnupg signing failed, does gpg binary exist? : %s" % e)
         raise
 
     if len(d_stderr):
-        log.error("gpg: %s" % d_stderr)
+        log.error("gpg: %s" % util.to_str(d_stderr))
 
     return d_stdout