-#!/usr/bin/env python
+#!/usr/bin/env python3
# vi: encoding=utf-8 ts=8 sts=4 sw=4 et
+from __future__ import print_function
+
import sys, os, re
import getopt
import subprocess
import ftpio
try:
- opts, args = getopt.getopt(sys.argv[1:], 'qs', [ "quiet" ])
+ opts, args = getopt.getopt(sys.argv[1:], 'qsdo:', [ "quiet" ])
except getopt.GetoptError:
- print >>sys.stderr, "ERR: options error"
- print >>sys.stderr, "rpmlint.py tree package1 [package2...]"
+ print("ERR: options error", file=sys.stderr)
+ print("rpmlint.py tree package1 [package2...]", file=sys.stderr)
sys.exit(1)
quiet = False
show = False
+debugfiles = False
+outstream = sys.stdout
for o, a in opts:
if o == "-q" or o == "--quiet":
quiet = True
if o == "-s":
show = True
+ if o == "-d":
+ debugfiles = True
+ if o == "-o":
+ outstream = open(a, 'w')
if len(args) < 1:
- print >>sys.stderr, "ERR: missing tree name"
- print >>sys.stderr, "rpmlint.py tree package1 [package2...]"
+ print("ERR: missing tree name", file=sys.stderr)
+ print("rpmlint.py tree package1 [package2...]", file=sys.stderr)
sys.exit(1)
treename = args[0]
ftpio.connect('rpmlint')
if not ftpio.lock(treename, True):
- print >>sys.stderr, "ERR: %s tree already locked" % treename
+ print("ERR: %s tree already locked" % treename, file=sys.stderr)
sys.exit(1)
files = []
tree.mark4moving(tree.loadedpkgs)
else:
tree.mark4moving(packages)
- files = tree.rpmfiles(debugfiles = False, sourcefiles = False)
+ files = tree.rpmfiles(debugfiles = debugfiles, sourcefiles = False)
-except (ftptree.SomeError, KeyboardInterrupt), e:
+except (ftptree.SomeError, KeyboardInterrupt) as e:
# In case of problems we need to unlock the tree before exiting
ftpio.unlock(treename)
sys.exit(1)
class LintPkg:
def __init__(self, cachedir):
+ self.outstream = sys.stdout
+
# for rpmlint stats
self.packages = self.specfiles = self.errors = self.warnings = 0
# 1 packages and 0 specfiles checked; 0 errors, 0 warnings.
self._rpmlint = '/usr/bin/rpmlint'
+ # mtime, which invalidates all caches
+ self.mtime = None
+ rpmlintrc = os.path.expanduser("~/.config/rpmlint")
+ if os.path.exists(rpmlintrc):
+ self.mtime = os.stat(rpmlintrc).st_mtime
+
self.cachedir = os.path.expanduser(cachedir)
if not os.path.isdir(self.cachedir):
os.makedirs(self.cachedir)
def get_stats(self, file):
cachefile = self.cachefile(file)
- if not os.path.exists(cachefile):
+ if not os.path.exists(cachefile) or os.path.getsize(cachefile) <= 0:
return None
# show last line (that contains status)
def print_stats(self, file = None):
if file:
(dirname, filename) = os.path.split(file)
- print "\r\033[0K%d packages and %d specfiles checked; %d errors, %d warnings. [%s]" % (self.packages, self.specfiles, self.errors, self.warnings, filename),
+ print("\r\033[0K%d packages and %d specfiles checked; %d errors, %d warnings. [%s]" % (self.packages, self.specfiles, self.errors, self.warnings, filename), file=self.outstream)
else:
- print "\r\033[0K%d packages and %d specfiles checked; %d errors, %d warnings." % (self.packages, self.specfiles, self.errors, self.warnings)
+ print("\r\033[0K%d packages and %d specfiles checked; %d errors, %d warnings." % (self.packages, self.specfiles, self.errors, self.warnings), file=self.outstream)
sys.stdout.flush()
def cat(self, file):
- print "".join(open(file, 'r').readlines())
+ print("".join(open(file, 'r').readlines()), file=self.outstream)
def show_results(self, file):
m = self.get_stats(file)
cachefile = self.cachefile(file)
if not os.path.exists(cachefile):
- print "MISSING: report: %s" % file
+ print("MISSING: report: %s" % file, file=self.outstream)
if m['errors'] > 0 or m['warnings'] > 0:
+ (dirname, filename) = os.path.split(file)
+ print("rpmlint: %s" % filename, file=self.outstream)
self.cat(cachefile)
def rpmlint(self, file):
cachefile = self.cachefile(file)
rc = None
- if not os.path.exists(cachefile) or os.stat(file).st_mtime > os.stat(cachefile).st_mtime:
+ if not os.path.exists(cachefile) \
+ or os.stat(file).st_mtime > os.stat(cachefile).st_mtime \
+ or (self.mtime and self.mtime > os.stat(cachefile).st_mtime):
cmd = [self._rpmlint, file]
outfd = open(cachefile, 'w')
try:
- rc = subprocess.call(cmd, stdin = subprocess.PIPE, stdout = outfd, stderr = outfd, close_fds = True)
+ env = {'TZ': 'GMT'}
+ rc = subprocess.call(cmd, stdin = subprocess.PIPE, stdout = outfd, stderr = outfd, env = env, close_fds = True)
except KeyboardInterrupt:
os.unlink(cachefile)
raise
try:
lock = 'rpmlint:'+treename
if not ftpio.lock(lock, True):
- print >>sys.stderr, "ERR: %s tree already locked for rpmlint" % treename
+ print("ERR: %s tree already locked for rpmlint" % treename, file=sys.stderr)
sys.exit(1)
if not quiet:
- print "rpmlint of %d files from %d packages" % (len(files), len(tree.loadedpkgs))
+ print("rpmlint of %d files from %d packages" % (len(files), len(tree.loadedpkgs)), file=outstream)
lint = LintPkg("~/tmp/rpmlint")
+ lint.outstream = outstream
for file in files:
lint.rpmlint(file)
if not quiet:
lint.print_stats()
ftpio.unlock(lock)
-except Exception:
+except (Exception, KeyboardInterrupt):
ftpio.unlock(lock)
raise