]> git.pld-linux.org Git - projects/pld-ftp-admin.git/blobdiff - bin/pfa-lintpkg
Switch to Python 3 for rpm.org rpm
[projects/pld-ftp-admin.git] / bin / pfa-lintpkg
old mode 100644 (file)
new mode 100755 (executable)
index f7da0d7..1cb1edf
@@ -1,6 +1,8 @@
-#!/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
@@ -10,23 +12,29 @@ from common import checkdir
 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]
@@ -37,7 +45,7 @@ checkdir(treename)
 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 = []
@@ -55,9 +63,9 @@ try:
         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)
@@ -66,6 +74,8 @@ ftpio.unlock(treename)
 
 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.
@@ -73,6 +83,12 @@ class LintPkg:
 
         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)
@@ -83,7 +99,7 @@ class LintPkg:
 
     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)
@@ -115,13 +131,13 @@ class LintPkg:
     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)
@@ -130,20 +146,25 @@ class LintPkg:
 
         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
@@ -158,12 +179,13 @@ class LintPkg:
 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:
@@ -175,6 +197,6 @@ try:
         lint.print_stats()
 
     ftpio.unlock(lock)
-except Exception:
+except (Exception, KeyboardInterrupt):
     ftpio.unlock(lock)
     raise
This page took 0.045741 seconds and 4 git commands to generate.