From d7e63f97a03aa06c45998d0edd6af005caaf98a3 Mon Sep 17 00:00:00 2001 From: Robert Mayr Date: Sep 05 2017 19:38:34 +0000 Subject: Revert "Merge #723 `tools: Port update-gpg-keys to Python 3`" This reverts commit 79e7913a39a490b9eace95ca96e01fe878b78308, reversing changes made to 7382927e58761207e77231857218533497361395. --- diff --git a/tools/update-gpg-keys b/tools/update-gpg-keys index aa5c30c..4b34fa1 100755 --- a/tools/update-gpg-keys +++ b/tools/update-gpg-keys @@ -1,4 +1,4 @@ -#!/usr/bin/python3 +#!/usr/bin/python """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,9 +17,6 @@ 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 @@ -86,7 +83,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, encoding='utf-8') + p = Popen(cmd, stdout=PIPE, stderr=PIPE) stdout, stderr = p.communicate() if p.returncode: raise GpgError(-1, stderr) @@ -131,10 +128,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 as e: + except Exception, e: raise SystemExit('Failed to create %s: %s' % (opts.keydir, e.strerror)) # Handle removal requests @@ -147,54 +144,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 as e: - print('Failed to remove %s: %s' % (path, e.strerror)) + except Exception, 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 as e: - print('Failed to get keyinfo for %s: %s' % (f, e.strerror)) + except Exception, 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, encoding='utf-8') + p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE) output, stderr = p.communicate(input=keydata) if p.returncode: raise GpgError(-1, stderr) - except Exception as e: - print('Failed to read %s: %s' % (f, e.strerror)) + except Exception, 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() @@ -205,21 +202,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 as e: - print('Skipping %s: %s' % (keyfile, e.strerror)) + except GpgError, 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: @@ -235,15 +232,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') @@ -253,24 +250,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(list(keys.keys()))]) +cmd.extend([keys[k][1] for k in natsorted(keys.keys())]) try: - p = Popen(cmd, stdout=PIPE, stderr=PIPE, encoding='utf-8') + p = Popen(cmd, stdout=PIPE, stderr=PIPE) stdout, stderr = p.communicate() if p.returncode: raise GpgError(-1, stderr) -except Exception as e: +except Exception, 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, encoding='utf-8') + p = Popen(cmd, stdout=PIPE, stderr=PIPE) stdout, stderr = p.communicate() if p.returncode: raise GpgError(-1, stderr) -except Exception as e: +except Exception, e: shutil.rmtree(gpgdir) raise SystemExit('Failed to export key(s): %s' % e.strerror) @@ -283,5 +280,5 @@ try: f = open(keyblock, 'w') f.write(stdout) f.close() -except Exception as e: - print('Failed to write %s keyblock: %s' % (keyblock, e.strerror)) +except Exception, e: + print 'Failed to write %s keyblock: %s' % (keyblock, e.strerror)