From 94e0f1fb0ee45cb952433cb6cf81d9a578a67e02 Mon Sep 17 00:00:00 2001 From: Robert Mayr Date: Oct 11 2017 14:16:15 +0000 Subject: Merge branch 'master' of ssh://pagure.io/fedora-websites --- diff --git a/tools/update-gpg-keys b/tools/update-gpg-keys index 4b34fa1..705a70b 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 @@ -85,6 +88,7 @@ def get_keyinfo(path): 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'): @@ -128,11 +132,11 @@ 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: - 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: @@ -144,38 +148,38 @@ 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, 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'] @@ -183,15 +187,16 @@ for f in args: 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() @@ -202,21 +207,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, 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: @@ -232,15 +237,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') @@ -250,15 +255,15 @@ 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) 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'] @@ -267,9 +272,9 @@ try: 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) @@ -277,8 +282,8 @@ shutil.rmtree(gpgdir) # 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)))