#724 tools: Port update-gpg-keys to Python 3
Merged 2 years ago by robyduck. Opened 2 years ago by sanqui.
sanqui/fedora-websites python3  into  master

file modified
+40 -35

@@ -1,4 +1,4 @@ 

- #!/usr/bin/python

+ #!/usr/bin/python3

  

  """Update gpg keys used on getfedora.org.  This script helps automate the

  process of maintaining the fedora key files used on the website.  It manages

@@ -17,6 +17,9 @@ 

  # supported releases and extract the keys from them, prompting to verify the

  # keys, of course.

  

+ import sys

+ assert sys.version_info >= (3,), "This script requires Python 3"

+ 

  import os

  import re

  import glob

@@ -83,6 +86,7 @@ 

      cmd = ['gpg', '--with-colons', path]

      p = Popen(cmd, stdout=PIPE, stderr=PIPE)

      stdout, stderr = p.communicate()

+     stdout = stdout.decode('utf-8')

      if p.returncode:

          raise GpgError(-1, stderr)

      for line in stdout.split('\n'):

@@ -126,11 +130,11 @@ 

          raise SystemExit('Error: %s' % msg)

  else:

      if opts.verbose:

-         print 'Creating keydir: %s' % opts.keydir

+         print('Creating keydir: %s' % opts.keydir)

      try:

          os.makedirs(opts.keydir)

-     except Exception, e:

-         raise SystemExit('Failed to create %s: %s' % (opts.keydir, e.strerror))

+     except Exception as e:

+         raise SystemExit('Failed to create %s: %s' % (opts.keydir, str(e)))

  

  # Handle removal requests

  for k in opts.rmkeys:

@@ -142,38 +146,38 @@ 

          if os.path.exists(f):

              path = f

          else:

-             print '%s appears to be keyid, but %s not found' % (k, f)

+             print('%s appears to be keyid, but %s not found' % (k, f))

              continue

      else:

          f = os.path.join(opts.keydir, k)

          if os.path.exists(f):

              path = f

          else:

-             print 'No matches found for %s' % k

+             print('No matches found for %s' % k)

              continue

      if path:

          try:

              os.remove(path)

-         except Exception, e:

-             print 'Failed to remove %s: %s' % (path, e.strerror)

+         except Exception as e:

+             print('Failed to remove %s: %s' % (path, str(e)))

              continue

-         print 'Removed %s' % path

+         print('Removed %s' % path)

  

  # Process any key files passed in as arguments

  for f in args:

      if not os.path.isfile(f):

-         print '%s is not a file, skipping...' % f

+         print('%s is not a file, skipping...' % f)

          continue

      keydata = open(f).read()

      m = keyblock_re.match(keydata)

      if not m:

-         print '%s does not appear to be a gpg key file, skipping...' % f

+         print('%s does not appear to be a gpg key file, skipping...' % f)

          continue

      keydata = m.group(1) + '\n'

      try:

          keyinfo = get_keyinfo(f)

-     except Exception, e:

-         print 'Failed to get keyinfo for %s: %s' % (f, e.strerror)

+     except Exception as e:

+         print('Failed to get keyinfo for %s: %s' % (f, str(e)))

          continue

      keyfile = os.path.join(opts.keydir, '%s.txt' % keyinfo['keyid'])

      cmd = ['gpg']

@@ -181,15 +185,16 @@ 

          cmd.append('--with-fingerprint')

      try:

          p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)

-         output, stderr = p.communicate(input=keydata)

+         output, stderr = p.communicate(input=keydata.encode('utf-8'))

+         output = output.decode('utf-8')

          if p.returncode:

              raise GpgError(-1, stderr)

-     except Exception, e:

-         print 'Failed to read %s: %s' % (f, e.strerror)

+     except Exception as e:

+         print('Failed to read %s: %s' % (f, str(e)))

          continue

      keydata = output + '\n' + keydata

      if opts.verbose:

-         print 'Adding key data to %s:\n%s' % (keyfile, output)

+         print('Adding key data to %s:\n%s' % (keyfile, output))

      kf = open(keyfile, 'w')

      kf.write(keydata)

      kf.close()

@@ -200,21 +205,21 @@ 

      basename = os.path.splitext(os.path.basename(keyfile))[0]

      if not keyid_re.match(basename):

          if opts.verbose > 2:

-             print 'Skipping %s: Does not match keyid regex\n' % keyfile

+             print('Skipping %s: Does not match keyid regex\n' % keyfile)

          continue

      if opts.verbose:

-         print 'Processing %s' % keyfile

+         print('Processing %s' % keyfile)

      if keyblock_begin not in open(keyfile).read():

          if opts.verbose:

-             print "  %s doesn't start with %s\n" % (keyfile, keyblock_begin)

+             print("  %s doesn't start with %s\n" % (keyfile, keyblock_begin))

          continue

      try:

          keyinfo = get_keyinfo(keyfile)

-     except GpgError, e:

-         print 'Skipping %s: %s' % (keyfile, e.strerror)

+     except GpgError as e:

+         print('Skipping %s: %s' % (keyfile, str(e)))

      if keyinfo['keyid'] in obsolete_keys:

          if opts.verbose:

-             print '  Skipping: obsolete key\n'

+             print('  Skipping: obsolete key\n')

          continue

      if 'EPEL' in keyinfo['userid']:

          try:

@@ -230,15 +235,15 @@ 

          uid = keyinfo['userid'].lower()

          if arch in uid and arch not in version.lower():

              if opts.verbose > 1:

-                 print '  Adding %s to version' % arch

+                 print('  Adding %s to version' % arch)

              version += '-%s' % arch

              break

      if opts.verbose > 1:

-         print '  userid = %s' % keyinfo['userid']

-         print '  version = %s' % version

+         print('  userid = %s' % keyinfo['userid'])

+         print('  version = %s' % version)

      keys[version] = (keyinfo['keyid'], keyfile)

      if opts.verbose:

-         print

+         print()

  

  if not keys:

      raise SystemExit('No keys were found')

@@ -248,15 +253,15 @@ 

  

  # Import key(s) to tmp keyring

  cmd = ['gpg', '--homedir', gpgdir, '--quiet', '--import']

- cmd.extend([keys[k][1] for k in natsorted(keys.keys())])

+ cmd.extend([keys[k][1] for k in natsorted(list(keys.keys()))])

  try:

      p = Popen(cmd, stdout=PIPE, stderr=PIPE)

      stdout, stderr = p.communicate()

      if p.returncode:

          raise GpgError(-1, stderr)

- except Exception, e:

+ except Exception as e:

      shutil.rmtree(gpgdir)

-     raise SystemExit('Failed to import key(s): %s' % e.strerror)

+     raise SystemExit('Failed to import key(s): %s' % str(e))

  

  # Export key(s) from tmp keyring

  cmd = ['gpg', '--homedir', gpgdir, '--armor', '--export']

@@ -265,9 +270,9 @@ 

      stdout, stderr = p.communicate()

      if p.returncode:

          raise GpgError(-1, stderr)

- except Exception, e:

+ except Exception as e:

      shutil.rmtree(gpgdir)

-     raise SystemExit('Failed to export key(s): %s' % e.strerror)

+     raise SystemExit('Failed to export key(s): %s' % str(e))

  

  # Remove tmp gpgdir

  shutil.rmtree(gpgdir)

@@ -275,8 +280,8 @@ 

  # Write fedora keyblock

  keyblock = os.path.join(opts.keydir, opts.keyblock)

  try:

-     f = open(keyblock, 'w')

+     f = open(keyblock, 'wb')

      f.write(stdout)

      f.close()

- except Exception, e:

-     print 'Failed to write %s keyblock: %s' % (keyblock, e.strerror)

+ except Exception as e:

+     print('Failed to write %s keyblock: %s' % (keyblock, str(e)))

Applied it locally, but I still get the keyinfo error.

Failed to get keyinfo for RPM-GPG-KEY-fedora-28-primary: [Errno -1] b'gpg: lookup_hashtable failed: eof\ngpg: lookup_hashtable failed: eof\ngpg: lookup_hashtable failed: eof\n'
Skipping ../getfedora.org/static/64DAB85D.txt: [Errno -1] b'gpg: lookup_hashtable failed: eof\ngpg: lookup_hashtable failed: eof\ngpg: lookup_hashtable failed: eof\n'
Traceback (most recent call last):
  File "./update-gpg-keys", line 222, in <module>
    if keyinfo['keyid'] in obsolete_keys:
NameError: name 'keyinfo' is not defined

Pull-Request has been merged by robyduck

2 years ago
Metadata