diff --git a/alt_src/__init__.py b/alt_src/__init__.py index 9e0a226..c8d6a1d 100644 --- a/alt_src/__init__.py +++ b/alt_src/__init__.py @@ -1 +1 @@ -from alt_src import * +from .alt_src import * diff --git a/alt_src/alt_src.py b/alt_src/alt_src.py index eec28aa..37ff7a8 100644 --- a/alt_src/alt_src.py +++ b/alt_src/alt_src.py @@ -2,10 +2,12 @@ ''' Given an srpm and product, stage for alt-src release ''' +from __future__ import print_function -import ConfigParser +import six +from six.moves import configparser import copy -import cStringIO +from six.moves import cStringIO as StringIO import datetime import errno import fcntl @@ -22,8 +24,8 @@ import sys import time import traceback -from urllib import urlencode -from urllib2 import Request, urlopen +from six.moves.urllib.parse import urlencode +from six.moves.urllib.request import Request, urlopen import yaml import simplejson as json @@ -73,7 +75,7 @@ def get_config(cfile, overrides): if not os.access(cfile, os.F_OK): die("Missing config file: %s" % cfile) - confp = ConfigParser.RawConfigParser() + confp = configparser.RawConfigParser() confp.read(cfile) if not confp.has_section('altsrc'): die("Configuration file missing [altsrc] section: %s" % cfile) @@ -180,14 +182,14 @@ def __init__(self, options): self.rpms_or_module_dir = 'rpms' if not os.path.isfile(self.source_file): - raise StartupError, "No such file: %s" % self.source_file + raise StartupError("No such file: %s" % self.source_file) def run(self): raise NotImplementedError def add_error_logger(self): """Capture all error messages for use in later notifications""" - self.error_log = cStringIO.StringIO() + self.error_log = StringIO() handler = logging.StreamHandler(self.error_log) handler.setFormatter(logging.Formatter(self.options.config['log_format'])) handler.setLevel(logging.ERROR) @@ -195,14 +197,14 @@ def add_error_logger(self): def setup_logfile(self, logname): if not os.path.isdir(self.workdir): - raise SanityError, "Work dir does not exist: %s" % self.workdir + raise SanityError("Work dir does not exist: %s" % self.workdir) fname = fname_ = os.path.join(self.workdir, logname) ntimes = 0 while os.path.exists(fname): # don't overwrite an old logfile ntimes += 1 if ntimes > 1024: - raise SanityError, "Too many log backups" + raise SanityError("Too many log backups") fname = "%s.%d" % (fname_, ntimes) self.logfile = file(fname, 'w') handler = logging.StreamHandler(self.logfile) @@ -250,7 +252,7 @@ def log_cmd(self, cmd, logfile=None, fatal=True, tries=None, **kwargs): time.sleep(sleep_time) if ret: if fatal: - raise CommandError, "command failed: %r" % cmd + raise CommandError("command failed: %r" % cmd) #otherwise self.logger.warn("Command failed: %r", cmd) return ret @@ -276,7 +278,7 @@ def get_output(self, cmd, fatal=True, **kwargs): if retval: self.logger.warn("Command failed: %r", cmd) if fatal: - raise CommandError, "command failed: %r" % cmd + raise CommandError("command failed: %r" % cmd) return output, retval def _get_koji_session_and_pathinfo(self): @@ -289,9 +291,9 @@ def get_koji_srpm(self): session, pathinfo = self._get_koji_session_and_pathinfo() rpminfo = session.getRPM(self.options.source) if not rpminfo: - raise SanityError, "No such rpm: %s" % self.options.source + raise SanityError("No such rpm: %s" % self.options.source) if rpminfo['arch'] != 'src': - raise SanityError, "Not a source rpm: %s" % self.options.source + raise SanityError("Not a source rpm: %s" % self.options.source) binfo = session.getBuild(rpminfo['build_id']) bdir = pathinfo.build(binfo) relpath = pathinfo.rpm(rpminfo) @@ -336,7 +338,7 @@ def read_srpm(self): headers = koji.get_rpm_header(self.source_file) self.headers = headers if headers[rpm.RPMTAG_SOURCEPACKAGE] != 1: - raise InputError, "%s is not a source package" % self.source_file + raise InputError("%s is not a source package" % self.source_file) data = koji.get_header_fields(headers, ['name', 'version', 'release', 'summary']) self.nvr = "%(name)s-%(version)s-%(release)s" % data self.package = data['name'] @@ -375,7 +377,7 @@ def check_package(self): self.logger.debug("Got blacklist: %r", blacklist) if blacklist and koji.util.multi_fnmatch(self.package, blacklist): # raise FilterError, 'Blacklisted package: %s' % self.package - print 'Blacklisted package: %s, quitting' % self.package + print('Blacklisted package: %s, quitting' % self.package) sys.exit(0) def git_push_url(self): @@ -440,7 +442,7 @@ def find_spec(self, relative=False): if fname.endswith('.spec'): return os.path.join(specdir, fname) - raise SanityError, 'No spec file in checkout: %s' % self.checkout + raise SanityError('No spec file in checkout: %s' % self.checkout) def duplicate_check(self): """Check to see if we're already on remote""" @@ -485,7 +487,7 @@ def get_state(self): def set_state(self, state): if not self.workdir: - raise SanityError, "No workdir to set state for" + raise SanityError("No workdir to set state for") statefile = os.path.join(self.workdir, 'state') self.logger.debug('Writing state %s to file %s', state, statefile) fobj = open(statefile, 'w') @@ -684,7 +686,7 @@ def make_workdir(self): self.workdir = dirname = self.get_workdir() koji.ensuredir(os.path.dirname(self.workdir)) if os.path.islink(dirname): - raise SanityError, "%s is a symlink" % dirname + raise SanityError("%s is a symlink" % dirname) elif os.path.isdir(dirname): # TODO - more sanity checks self.set_in_progress() @@ -707,7 +709,7 @@ def make_workdir(self): will overwrite.", dirname, state) shutil.rmtree(dirname) elif os.path.exists(dirname): - raise SanityError, "%s exists and is not a directory" % dirname + raise SanityError("%s exists and is not a directory" % dirname) self.logger.info('Creating working directory: %s', dirname) koji.ensuredir(dirname) self.set_in_progress() @@ -1019,7 +1021,7 @@ def copy_to_lookaside(self, path, digest): if st1.st_size != st2.st_size: self.logger.error("Possibly corrupt lookaside entry: %s", lpath) self.logger.error("Size: %s, but current matching source is %s", st1.st_size, st2.st_size) - raise SanityError, "Lookaside size mismatch" + raise SanityError("Lookaside size mismatch") # TODO - more sanity checks self.logger.info('Skipping source, already in digest: %s', path) @@ -1030,7 +1032,7 @@ def debrand(self): if not self.options.config['debrand']: self.logger.warning("Debranding is disabled") return - confp = ConfigParser.RawConfigParser() + confp = configparser.RawConfigParser() for name in 'altsrc-global', self.package: cfile = os.path.join(self.options.config['rulesdir'], name + '.cfg') self.logger.debug('Looking for rules in %s', cfile) @@ -1063,7 +1065,7 @@ def debrand(self): for key, rtype, section in rules: handler = 'rule_handler_%s' % rtype if not hasattr(self, handler): - raise ConfigError, "No handler for rule type %s" % rtype + raise ConfigError("No handler for rule type %s" % rtype) data = dict(confp.items(section)) if 'enabled' in data: enabled = data['enabled'].lower().strip() @@ -1142,7 +1144,7 @@ def debrand(self): cmd = ['git', 'diff', '--cached', '--name-only'] output, _ = self.get_output(cmd, cwd=self.checkout, stderr='keep', fatal=False) if not output: - raise SanityError, "Debranding rules made no changes" + raise SanityError("Debranding rules made no changes") # caller will clean up cmd = self.git_base_cmd() @@ -1302,13 +1304,13 @@ def rule_handler_mmd(self, data): if isinstance(current_data, list): gen = enumerate(current_data) elif isinstance(current_data, dict): - gen = current_data.items() + gen = list(current_data.items()) if gen: for key, val in gen: if re.match(matching_path[0], str(key)): stack.append((val, current_data, key, matching_path[1:])) - if isinstance(current_data, basestring): + if isinstance(current_data, six.string_types): replaced = re.sub(matching_path[0], data['replace'], current_data) @@ -1396,7 +1398,7 @@ def handle_add_patch(self, data): # http://www.rpm.org/max-rpm/s1-rpm-inside-tags.html if patchnum == pnum: self.logger.error("Patch %s already present: %s", patchnum, line) - raise SanityError, "Duplicate patch number" + raise SanityError("Duplicate patch number") elif alt_re.search(line): l_alt = lineno if patchnum == -1: @@ -1435,7 +1437,7 @@ def handle_add_patch(self, data): lines.insert(lnum + 1, entry) self.logger.debug("Inserting spec line: %i: %s", lnum+1, entry) else: - raise SanityError, "Unable to apply patch %s" % patchname + raise SanityError("Unable to apply patch %s" % patchname) elif data.get('apply', 'y').lower() in ('y', 'yes', '1', 'true'): entry = "%%patch%d -p%d\n" % (patchnum, patchstrip) apply_re = re.compile(r'^\s*%patch\d+\s+-p\d') @@ -1463,7 +1465,7 @@ def handle_add_patch(self, data): lines.insert(lsetup + 1, entry) self.logger.debug("Inserting spec line: %i: %s", lsetup+1, entry) else: - raise SanityError, "Unable to apply patch %s" % patchname + raise SanityError("Unable to apply patch %s" % patchname) # write it back out fobj = file(specfile, 'w') @@ -1493,7 +1495,7 @@ def handle_rm_patch(self, data): patch_re = re.compile(r'^\s*[pP]atch(' + str(patchnum) + r'):\s+(\S+?)\s*$') else: self.logger.error('No patch specified for removal') - raise SanityError, 'Invalid rule' + raise SanityError('Invalid rule') specfile = self.find_spec() fobj = file(specfile, 'r') @@ -1510,7 +1512,7 @@ def handle_rm_patch(self, data): break else: self.logger.error("No match for pattern: %r", patch_re.pattern) - raise SanityError, "Could not find patch to remove" + raise SanityError("Could not find patch to remove") # remove the matching line if lineno: del lines[lineno] @@ -1531,7 +1533,7 @@ def handle_rm_patch(self, data): self.logger.warning('Patch %s appears to be applied by %%autosetup', patchname) else: self.logger.error('No %%patch line for patch %s', patchname) - raise SanityError, "Unable to remove patch" + raise SanityError("Unable to remove patch") # write it back out fobj = file(specfile, 'w') @@ -1549,7 +1551,7 @@ def rule_handler_source(self, data): #XXX support del? else: self.logger.error('Unknown source rule method: %s', method) - raise ConfigError, 'Invalid method in source rule' + raise ConfigError('Invalid method in source rule') def handle_add_source(self, data): """Add a source entry in spec file""" @@ -1576,7 +1578,7 @@ def handle_add_source(self, data): # http://www.rpm.org/max-rpm/s1-rpm-inside-tags.html if sourcenum == snum: self.logger.error("Source %s already present: %s", sourcenum, line) - raise SanityError, "Duplicate source number" + raise SanityError("Duplicate source number") elif name_re.search(line): lname = lineno if sourcenum == -1: @@ -1626,7 +1628,7 @@ def handle_replace_source(self, data): break else: self.logger.error('Could not find source, no match for %r', source_re.pattern) - raise SanityError, 'No such source' + raise SanityError('No such source') # ... and replace it entry = "%s%s\n" % (head, sourcename) lines[lnum] = entry @@ -1646,7 +1648,7 @@ def rule_handler_script(self, data): fname = data['script'] script = os.path.join(self.options.config['rulesdir'], fname) if not os.path.isfile(script): - raise ConfigError, 'Script missing: %s' % script + raise ConfigError('Script missing: %s' % script) cmd = [script, self.checkout, self.find_spec()] self.log_cmd(cmd, cwd=self.checkout) @@ -1698,9 +1700,9 @@ def check_workdir(self): self.checkout = os.path.join(self.workdir, "checkout") self.logger.info('Checking working directory: %s', dirname) if os.path.islink(dirname): - raise SanityError, "%s is a symlink" % dirname + raise SanityError("%s is a symlink" % dirname) if not os.path.isdir(dirname): - raise SanityError, "Not staged. No such directory: %s" % dirname + raise SanityError("Not staged. No such directory: %s" % dirname) state = self.get_state() if state == 'UNTAGGED': if self.options.config['push_tags']: @@ -1713,7 +1715,7 @@ def check_workdir(self): self.logger.warn('Already pushed') return state if state != 'STAGED': - raise SanityError, "Staging incomplete" + raise SanityError("Staging incomplete") return state def add_changelog(self): @@ -1749,7 +1751,7 @@ def add_changelog(self): return elif len(parts) == 2: # should not be possible - raise SanityError, 'Unable to split changelog from spec' + raise SanityError('Unable to split changelog from spec') outf = file(spec, 'w') for part in parts[:2]: outf.write(part) @@ -1859,7 +1861,7 @@ def push_git(self, state): cmd = ['git', 'diff', '--cached', '--name-only'] output, _ = self.get_output(cmd, cwd=self.checkout, stderr='keep', fatal=False) if not output: - raise SanityError, "Debranding commits resulted in no changes?" + raise SanityError("Debranding commits resulted in no changes?") #commit cmd = self.git_base_cmd() cmd.extend(['commit', '-m', 'debrand %s' % self.nvr]) @@ -1973,7 +1975,7 @@ def explode_srpm(srpm, destdir=None, logfile=None): if header[rpm.RPMTAG_SOURCEPACKAGE] != 1: # we checked this earlier, but since we're about to rpm -i it, # let's check again - raise SanityError, "%s is not a source package" % srpm + raise SanityError("%s is not a source package" % srpm) if destdir is None: destdir = os.getcwd() else: @@ -1988,7 +1990,7 @@ def explode_srpm(srpm, destdir=None, logfile=None): proc = subprocess.Popen(cmd, **popts) ret = proc.wait() if ret: - raise CommandError, "command failed: %r" % cmd + raise CommandError("command failed: %r" % cmd) def wipe_git_dir(dirname): @@ -2003,7 +2005,7 @@ def wipe_git_dir(dirname): def die(msg): - print msg + print(msg) sys.exit(1) diff --git a/requirements.txt b/requirements.txt index 0b715c0..bacc8b9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ rpm-py-installer requests PyYAML simplejson +six diff --git a/test-requirements.txt b/test-requirements.txt index 3e979d7..4f89b8b 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,7 +1,6 @@ mock PyHamcrest configparser -six > 1.10 # koji 1.20 doesn't include it # as dependency