[Yum-devel] [PATCH 1/2] Make repomanage.py flake8 clean
Matěj Cepl
mcepl at redhat.com
Wed Jul 24 09:08:25 UTC 2013
The only exceptions are functions main and trimRpms which are too
complex for McCabe tests (23 and 8, respectively).
Signed-off-by: Matěj Cepl <mcepl at redhat.com>
---
repomanage.py | 118 +++++++++++++++++++++++++++++-----------------------------
1 file changed, 58 insertions(+), 60 deletions(-)
diff --git a/repomanage.py b/repomanage.py
index bef3b03..d4f6cb7 100755
--- a/repomanage.py
+++ b/repomanage.py
@@ -18,7 +18,8 @@
# need hdropen, dir traversing, version comparison, and getopt (eventually)
-# this should take a dir, traverse it - build a dict of foo[(name, arch)] = [/path/to/file/that/is/highest, /path/to/equalfile]
+# this should take a dir, traverse it - build a dict of
+# foo[(name, arch)] = [/path/to/file/that/is/highest, /path/to/equalfile]
import os
import sys
@@ -33,19 +34,19 @@ from optparse import OptionParser
def errorprint(stuff):
print >> sys.stderr, stuff
-
-
+
+
def getFileList(path, ext, filelist):
- """Return all files in path matching ext, store them in filelist, recurse dirs
- return list object"""
-
+ """Return all files in path matching ext, store them in filelist, recurse
+ dirs, return list object"""
+
extlen = len(ext)
try:
dir_list = os.listdir(path)
except OSError, e:
errorprint('Error accessing directory %s, %s' % (path, str(e)))
return []
-
+
for d in dir_list:
if os.path.isdir(path + '/' + d):
filelist = getFileList(path + '/' + d, ext, filelist)
@@ -53,7 +54,7 @@ def getFileList(path, ext, filelist):
if string.lower(d[-extlen:]) == '%s' % (ext):
newpath = os.path.normpath(path + '/' + d)
filelist.append(newpath)
-
+
return filelist
@@ -68,78 +69,75 @@ def trimRpms(rpms, excludeGlobs):
badrpms.append(fn)
for fn in badrpms:
if fn in rpms:
- rpms.remove(fn)
+ rpms.remove(fn)
# print 'Post-Trim Len: %d' % len(rpms)
return rpms
def parseargs(args):
usage = """
- repomanage: manage a directory of rpm packages. returns lists of newest
+ repomanage: manage a directory of rpm packages. returns lists of newest
or oldest packages in a directory for easy piping to xargs
or similar programs.
repomanage [--old] [--new] path.
"""
parser = OptionParser(usage=usage)
-
- # new is only used to make sure that the user is not trying to get both
- # new and old, after this old and not old will be used.
+
+ # new is only used to make sure that the user is not trying to get both
+ # new and old, after this old and not old will be used.
# (default = not old = new)
parser.add_option("-o", "--old", default=False, action="store_true",
- help='print the older packages')
+ help='print the older packages')
parser.add_option("-n", "--new", default=False, action="store_true",
- help='print the newest packages')
+ help='print the newest packages')
parser.add_option("-s", "--space", default=False, action="store_true",
- help='space separated output, not newline')
+ help='space separated output, not newline')
parser.add_option("-k", "--keep", default=1, dest='keep', action="store",
- help='newest N packages to keep - defaults to 1')
- parser.add_option("-c", "--nocheck", default=0, action="store_true",
- help='do not check package payload signatures/digests')
-
- (opts, args)= parser.parse_args()
-
-
+ help='newest N packages to keep - defaults to 1')
+ parser.add_option("-c", "--nocheck", default=0, action="store_true",
+ help='do not check package payload signatures/digests')
+
+ (opts, args) = parser.parse_args()
+
if opts.new and opts.old:
errorprint('\nPass either --old or --new, not both!\n')
print parser.format_help()
sys.exit(1)
-
+
if len(args) > 1:
errorprint('Error: Only one directory allowed per run.')
print parser.format_help()
sys.exit(1)
-
+
if len(args) < 1:
errorprint('Error: Must specify a directory to index.')
print parser.format_help()
sys.exit(1)
-
+
return (opts, args)
def main(args):
-
+
(options, args) = parseargs(args)
mydir = args[0]
-
rpmList = []
rpmList = getFileList(mydir, '.rpm', rpmList)
verfile = {}
- pkgdict = {} # hold all of them - put them in (n,a) = [(e,v,r),(e1,v1,r1)]
-
- keepnum = int(options.keep)*(-1) # the number of items to keep
-
+ pkgdict = {} # hold all of them - put them in (n,a) = [(e,v,r),(e1,v1,r1)]
+
+ keepnum = int(options.keep) * (-1) # the number of items to keep
+
if len(rpmList) == 0:
errorprint('No files to process')
sys.exit(1)
-
ts = rpm.TransactionSet()
if options.nocheck:
ts.setVSFlags(~(rpm._RPMVSF_NOPAYLOAD))
else:
- ts.setVSFlags(~(rpm.RPMVSF_NOMD5|rpm.RPMVSF_NEEDPAYLOAD))
+ ts.setVSFlags(~(rpm.RPMVSF_NOMD5 | rpm.RPMVSF_NEEDPAYLOAD))
for pkg in rpmList:
try:
hdr = rpmUtils.miscutils.hdrFromPackage(ts, pkg)
@@ -147,58 +145,58 @@ def main(args):
msg = "Error opening pkg %s: %s" % (pkg, str(e))
errorprint(msg)
continue
-
+
pkgtuple = rpmUtils.miscutils.pkgTupleFromHeader(hdr)
- (n,a,e,v,r) = pkgtuple
+ (n, a, e, v, r) = pkgtuple
del hdr
-
- if (n,a) not in pkgdict:
- pkgdict[(n,a)] = []
- pkgdict[(n,a)].append((e,v,r))
-
+
+ if (n, a) not in pkgdict:
+ pkgdict[(n, a)] = []
+ pkgdict[(n, a)].append((e, v, r))
+
if pkgtuple not in verfile:
verfile[pkgtuple] = []
verfile[pkgtuple].append(pkg)
-
+
for natup in pkgdict.keys():
evrlist = pkgdict[natup]
if len(evrlist) > 1:
evrlist = misc.unique(evrlist)
evrlist.sort(rpmUtils.miscutils.compareEVR)
pkgdict[natup] = evrlist
-
+
del ts
# now we have our dicts - we can return whatever by iterating over them
-
+
outputpackages = []
-
+
#if new
if not options.old:
- for (n,a) in pkgdict.keys():
- evrlist = pkgdict[(n,a)]
-
+ for (n, a) in pkgdict.keys():
+ evrlist = pkgdict[(n, a)]
+
if len(evrlist) < abs(keepnum):
newevrs = evrlist
else:
newevrs = evrlist[keepnum:]
-
- for (e,v,r) in newevrs:
- for pkg in verfile[(n,a,e,v,r)]:
+
+ for (e, v, r) in newevrs:
+ for pkg in verfile[(n, a, e, v, r)]:
outputpackages.append(pkg)
-
+
if options.old:
- for (n,a) in pkgdict.keys():
- evrlist = pkgdict[(n,a)]
-
+ for (n, a) in pkgdict.keys():
+ evrlist = pkgdict[(n, a)]
+
if len(evrlist) < abs(keepnum):
continue
-
+
oldevrs = evrlist[:keepnum]
- for (e,v,r) in oldevrs:
- for pkg in verfile[(n,a,e,v,r)]:
+ for (e, v, r) in oldevrs:
+ for pkg in verfile[(n, a, e, v, r)]:
outputpackages.append(pkg)
-
+
outputpackages.sort()
for pkg in outputpackages:
if options.space:
@@ -218,7 +216,7 @@ def usage():
-h --help - duh
By default it will output the full path to the newest packages in the path.
"""
-
+
if __name__ == "__main__":
if len(sys.argv) < 1:
--
1.8.3.GIT
More information about the Yum-devel
mailing list