Skip to content

Commit

Permalink
Merge pull request #16 from stfc/exception_handling
Browse files Browse the repository at this point in the history
Improve exception handling
  • Loading branch information
Oli-Rest authored Jul 16, 2024
2 parents 5919c63 + 889da0e commit 06e5d26
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions mrepo.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ def __init__(self, args):
opts, args = getopt.getopt(args, 'c:d:fghnqr:t:uvx',
('config=', 'dist=', 'dry-run', 'force', 'generate', 'help', 'quiet', 'repo=',
'type=', 'update', 'verbose', 'version', 'extras'))
except getopt.error, exc:
print 'mrepo: %s, try mrepo -h for a list of all the options' % str(exc)
except getopt.error, instance:
print 'mrepo: %s, try mrepo -h for a list of all the options' % str(instance)
sys.exit(1)

for opt, arg in opts:
Expand Down Expand Up @@ -232,13 +232,13 @@ def read(self, configfile):
configfh = urllib.urlopen(configfile)
try:
self.cfg.readfp(configfh)
except ConfigParser.MissingSectionHeaderError:
except IOError:
die(6, 'Error accessing URL: %s' % configfile)
else:
if os.access(configfile, os.R_OK):
try:
self.cfg.read(configfile)
except:
except ConfigParser.MissingSectionHeaderError:
die(7, 'Syntax error reading file: %s' % configfile)
else:
die(6, 'Error accessing file: %s' % configfile)
Expand Down Expand Up @@ -311,9 +311,9 @@ def getoption(self, section, option, var):
try:
var = self.cfg.get(section, option)
info(3, 'Setting option %s in section [%s] to: %s' % (option, section, var))
except ConfigParser.NoSectionError, e:
except ConfigParser.NoSectionError:
error(5, 'Failed to find section [%s]' % section)
except ConfigParser.NoOptionError, e:
except ConfigParser.NoOptionError:
info(5, 'Setting option %s in section [%s] to: %s (default)' % (option, section, var))
return var

Expand Down Expand Up @@ -470,9 +470,9 @@ def mirror(self):
mirrorreposync(url, self.srcdir, '%s-%s' % (self.dist.nick, self.name), self.dist)
else:
error(2, 'Scheme %s:// not implemented yet (in %s)' % (s, url))
except mrepoMirrorException, e:
error(0, 'Mirroring failed for %s with message:\n %s' % (url, e.value))
exitcode = 2
except MrepoMirrorException as instance:
error(0, 'Mirroring failed for %s with message:\n %s' % (url, instance.value))
EXITCODE = 2
if not self.url:
### Create directory in case no URL is given
mkdir(self.srcdir)
Expand Down Expand Up @@ -578,14 +578,14 @@ def createmd(self):
if md in ('createrepo', 'repomd'):
self.repomd()

except mrepoGenerateException, e:
error(0, 'Generating repo failed for %s with message:\n %s' % (self.name, e.value))
except MrepoGenerateException as instance:
error(0, 'Generating repo failed for %s with message:\n %s' % (self.name, instance.value))
exitcode = 2

def repomd(self):
"Create a repomd repository"
if not cf.cmd['createrepo']:
raise mrepoGenerateException('Command createrepo is not found. Skipping.')
raise MrepoGenerateException('Command createrepo is not found. Skipping.')

groupfilename = 'comps.xml'

Expand Down Expand Up @@ -613,18 +613,18 @@ def repomd(self):
info(2, '%s: Create repomd repository for %s' % (self.dist.nick, self.name))
ret = run('%s %s %s' % (cf.cmd['createrepo'], repoopts, self.wwwdir))
if ret:
raise(mrepoGenerateException('%s failed with return code: %s' % (cf.cmd['createrepo'], ret)))
raise MrepoGenerateException('%s failed with return code: %s' % (cf.cmd['createrepo'], ret))


class mrepoMirrorException(Exception):
class MrepoMirrorException(Exception):
def __init__(self, value):
self.value = value

def __str__(self):
return repr(self.value)


class mrepoGenerateException(Exception):
class MrepoGenerateException(Exception):
def __init__(self, value):
self.value = value

Expand Down Expand Up @@ -889,7 +889,7 @@ def mirrorrsync(url, path):

ret = run('%s %s %s %s' % (cf.cmd['rsync'], opts, url, path), dryrun=True)
if ret:
raise(mrepoMirrorException('Failed with return code: %s' % ret))
raise MrepoMirrorException('Failed with return code: %s' % ret)


def mirrorlftp(url, path, dist):
Expand Down Expand Up @@ -932,7 +932,7 @@ def mirrorlftp(url, path, dist):

ret = run('%s %s -c \'%s mirror %s %s %s\'' % (cf.cmd['lftp'], opts, cmds, mirroropts, url, path), dryrun=True)
if ret:
raise(mrepoMirrorException('Failed with return code: %s' % ret))
raise MrepoMirrorException('Failed with return code: %s' % ret)


def mirrorreposync(url, path, reponame, dist):
Expand Down Expand Up @@ -987,7 +987,7 @@ def mirrorreposync(url, path, reponame, dist):
os.remove(reposync_conf_file)

if ret:
raise(mrepoMirrorException('Failed with return code: %s' % ret))
raise MrepoMirrorException('Failed with return code: %s' % ret)


def which(cmd):
Expand Down Expand Up @@ -1252,7 +1252,7 @@ def formatlist(pkglist):
cf = readconfig()
try:
main()
except KeyboardInterrupt, e:
except KeyboardInterrupt:
die(6, 'Exiting on user request')
sys.exit(exitcode)

Expand Down

0 comments on commit 06e5d26

Please sign in to comment.