pld-ftp-admin: scripts/lintpkg.py - migrate using class in code

glen glen at pld-linux.org
Thu Feb 10 13:44:24 CET 2011


Author: glen                         Date: Thu Feb 10 12:44:24 2011 GMT
Module: pld-ftp-admin                 Tag: HEAD
---- Log message:
- migrate using class in code

---- Files affected:
pld-ftp-admin/scripts:
   lintpkg.py (1.4 -> 1.5) 

---- Diffs:

================================================================
Index: pld-ftp-admin/scripts/lintpkg.py
diff -u pld-ftp-admin/scripts/lintpkg.py:1.4 pld-ftp-admin/scripts/lintpkg.py:1.5
--- pld-ftp-admin/scripts/lintpkg.py:1.4	Thu Feb 10 06:47:48 2011
+++ pld-ftp-admin/scripts/lintpkg.py	Thu Feb 10 13:44:19 2011
@@ -44,51 +44,71 @@
 
 ftpio.unlock(sys.argv[1])
 
-cachedir = os.path.expanduser("~/tmp/rpmlint")
-if not os.path.isdir(cachedir):
-    os.makedirs(cachedir)
-
-# for rpmlint stats
-packages = specfiles = errors = warnings = 0
-# 1 packages and 0 specfiles checked; 0 errors, 0 warnings.
-lintre = re.compile('(?P<packages>\d+) packages and (?P<specfiles>\d+) specfiles checked; (?P<errors>\d+) errors, (?P<warnings>\d+) warnings.')
-
-def rpmlint(file):
-    global packages
-    global specfiles
-    global errors
-    global warnings
-    (dirname,filename) = os.path.split(file)
-    cachefile = os.path.join(cachedir, filename+'.txt')
-
-    rc = None
-    if not os.path.exists(cachefile) or os.stat(file).st_mtime > os.stat(cachefile).st_mtime:
-        cmd = ['/usr/bin/rpmlint', file]
-        outfd = open(cachefile, 'w')
-        try:
-            rc = subprocess.call(cmd, stdin = subprocess.PIPE, stdout = outfd, stderr = outfd, close_fds = True)
-        except KeyboardInterrupt:
+class LintPkg:
+    def __init__(self, cachedir):
+        # for rpmlint stats
+        self.packages = self.specfiles = self.errors = self.warnings = 0
+        # 1 packages and 0 specfiles checked; 0 errors, 0 warnings.
+        self.lintre = re.compile('(?P<packages>\d+) packages and (?P<specfiles>\d+) specfiles checked; (?P<errors>\d+) errors, (?P<warnings>\d+) warnings.')
+
+        self._rpmlint = '/usr/bin/rpmlint'
+
+        self.cachedir = os.path.expanduser(cachedir)
+        if not os.path.isdir(self.cachedir):
+            os.makedirs(self.cachedir)
+
+    def cachefile(self, file):
+        (dirname, filename) = os.path.split(file)
+        return os.path.join(self.cachedir, filename+'.txt')
+
+    """
+    update stats from cachefile
+    """
+    def update_stats(self, file):
+        cachefile = self.cachefile(file)
+
+        # show last line (that contains status)
+        l = (open(cachefile, 'r').readlines())[-1]
+        m = self.lintre.match(l)
+        if not m:
+            return False
+
+        self.packages += int(m.group('packages'))
+        self.specfiles += int(m.group('specfiles'))
+        self.errors += int(m.group('errors'))
+        self.warnings += int(m.group('warnings'))
+        return True
+
+    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),
+        else:
+            print "\r\033[0K%d packages and %d specfiles checked; %d errors, %d warnings." % (self.packages, self.specfiles, self.errors, self.warnings)
+        sys.stdout.flush()
+
+    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:
+            cmd = [self._rpmlint, file]
+            outfd = open(cachefile, 'w')
+            try:
+                rc = subprocess.call(cmd, stdin = subprocess.PIPE, stdout = outfd, stderr = outfd, close_fds = True)
+            except KeyboardInterrupt:
+                os.unlink(cachefile)
+                raise
+            outfd.close()
+        if not self.update_stats(file):
             os.unlink(cachefile)
-            raise
-        outfd.close()
-
-    # show last line (that contains status)
-    l = (open(cachefile, 'r').readlines())[-1]
-    m = lintre.match(l)
-    if m:
-        packages += int(m.group('packages'))
-        specfiles += int(m.group('specfiles'))
-        errors += int(m.group('errors'))
-        warnings += int(m.group('warnings'))
-
-    return rc == 0
+            rc = 1
+        return rc == 0
 
 print "rpmlint of %d files from %d packages" % (len(files), len(tree.loadedpkgs))
-for x in files:
-    (n, f) = os.path.split(x)
-    print "\r\033[0K%d packages and %d specfiles checked; %d errors, %d warnings. [%s]" % (packages, specfiles, errors, warnings, f),
-    sys.stdout.flush()
-    rpmlint(x)
+lint = LintPkg("~/tmp/rpmlint")
+for file in files:
+    lint.rpmlint(file)
+    lint.print_stats(file)
 
-print "\r\033[0K%d packages and %d specfiles checked; %d errors, %d warnings." % (packages, specfiles, errors, warnings)
-print "Done"
+lint.print_stats()
================================================================

---- CVS-web:
    http://cvs.pld-linux.org/cgi-bin/cvsweb.cgi/pld-ftp-admin/scripts/lintpkg.py?r1=1.4&r2=1.5&f=u



More information about the pld-cvs-commit mailing list