From a85275d664c80798d258430bfc19fb5349cd26dc Mon Sep 17 00:00:00 2001 From: Sanqui Date: Sep 05 2017 18:40:40 +0000 Subject: tools: Port update-gpg-keys to Python 3 --- diff --git a/tools/update-gpg-keys b/tools/update-gpg-keys index 7e323c4..83ac5ee 100755 --- a/tools/update-gpg-keys +++ b/tools/update-gpg-keys @@ -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 @@ keys may be done using the --remove (-r) option, specifying a path or keyid. # 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 @@ -81,7 +84,7 @@ class GpgError(EnvironmentError): def get_keyinfo(path): """Return some basic information about a gpg key.""" cmd = ['gpg', '--with-colons', path] - p = Popen(cmd, stdout=PIPE, stderr=PIPE) + p = Popen(cmd, stdout=PIPE, stderr=PIPE, encoding='utf-8') stdout, stderr = p.communicate() if p.returncode: raise GpgError(-1, stderr) @@ -126,10 +129,10 @@ if os.path.isdir(opts.keydir): 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: + except Exception as e: raise SystemExit('Failed to create %s: %s' % (opts.keydir, e.strerror)) # Handle removal requests @@ -142,54 +145,54 @@ for k in opts.rmkeys: 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, e.strerror)) 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, e.strerror)) continue keyfile = os.path.join(opts.keydir, '%s.txt' % keyinfo['keyid']) cmd = ['gpg'] if opts.fingerprint: cmd.append('--with-fingerprint') try: - p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE) + p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, encoding='utf-8') output, stderr = p.communicate(input=keydata) 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, e.strerror)) 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 +203,21 @@ for keyfile in glob.glob('%s/*.txt' % opts.keydir): 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, e.strerror)) 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 +233,15 @@ for keyfile in glob.glob('%s/*.txt' % opts.keydir): 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,24 +251,24 @@ gpgdir = tempfile.mkdtemp(prefix='gpg', dir='.') # 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) + p = Popen(cmd, stdout=PIPE, stderr=PIPE, encoding='utf-8') 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) # Export key(s) from tmp keyring cmd = ['gpg', '--homedir', gpgdir, '--armor', '--export'] try: - p = Popen(cmd, stdout=PIPE, stderr=PIPE) + p = Popen(cmd, stdout=PIPE, stderr=PIPE, encoding='utf-8') 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) @@ -278,5 +281,5 @@ try: f = open(keyblock, 'w') 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, e.strerror))