Skip to content

Commit

Permalink
Make code compatible with Python 3 [CLOUDDST-27]
Browse files Browse the repository at this point in the history
2to3 tool was used to automatically detect code lines which are
problematic for Python 3. They were fixed in a way that ensures
continued Python 2 compatibility. 'six' package was added as a
dependency as it is used in some fixes.
  • Loading branch information
querti committed Apr 15, 2020
1 parent 628b92e commit cf1fb7a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 45 deletions.
2 changes: 1 addition & 1 deletion alt_src/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from alt_src import *
from .alt_src import *
88 changes: 45 additions & 43 deletions alt_src/alt_src.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -180,29 +182,29 @@ 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)
self.logger.addHandler(handler)

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)
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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)
Expand Down Expand Up @@ -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']
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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"""
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand All @@ -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]
Expand All @@ -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')
Expand All @@ -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"""
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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']:
Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand All @@ -2003,7 +2005,7 @@ def wipe_git_dir(dirname):


def die(msg):
print msg
print(msg)
sys.exit(1)


Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ rpm-py-installer
requests
PyYAML
simplejson
six
1 change: 0 additions & 1 deletion test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
mock
PyHamcrest
configparser
six > 1.10

# koji 1.20 doesn't include it
# as dependency
Expand Down

0 comments on commit cf1fb7a

Please sign in to comment.