2 # vi: encoding=utf-8 ts=8 sts=4 sw=4 et
6 sys.path.insert(0, os.environ['HOME']+'/pld-ftp-admin/modules')
8 from common import checkdir
12 print >>sys.stderr, "ERR: not enough parameters given"
13 print >>sys.stderr, "rpmlint.py tree package1 [package2...]"
18 ftpio.connect('rpmlint')
20 if not ftpio.lock(sys.argv[1], True):
21 print >>sys.stderr, "ERR: %s tree already locked" % sys.argv[1]
31 # if no files specified, grab whole tree contents
32 tree = ftptree.FtpTree(sys.argv[1], loadall = loadall)
34 # this is hack, should be a param, not access private .loadedpkgs element
35 tree.mark4moving(tree.loadedpkgs)
37 tree.mark4moving(sys.argv[2:])
38 files = tree.rpmfiles(debugfiles = False, sourcefiles = False)
40 except (ftptree.SomeError, KeyboardInterrupt), e:
41 # In case of problems we need to unlock the tree before exiting
42 ftpio.unlock(sys.argv[1])
45 ftpio.unlock(sys.argv[1])
48 def __init__(self, cachedir):
50 self.packages = self.specfiles = self.errors = self.warnings = 0
51 # 1 packages and 0 specfiles checked; 0 errors, 0 warnings.
52 self.lintre = re.compile('(?P<packages>\d+) packages and (?P<specfiles>\d+) specfiles checked; (?P<errors>\d+) errors, (?P<warnings>\d+) warnings.')
54 self._rpmlint = '/usr/bin/rpmlint'
56 self.cachedir = os.path.expanduser(cachedir)
57 if not os.path.isdir(self.cachedir):
58 os.makedirs(self.cachedir)
60 def cachefile(self, file):
61 (dirname, filename) = os.path.split(file)
62 return os.path.join(self.cachedir, filename+'.txt')
65 update stats from cachefile
67 def update_stats(self, file):
68 cachefile = self.cachefile(file)
70 # show last line (that contains status)
71 l = (open(cachefile, 'r').readlines())[-1]
72 m = self.lintre.match(l)
76 self.packages += int(m.group('packages'))
77 self.specfiles += int(m.group('specfiles'))
78 self.errors += int(m.group('errors'))
79 self.warnings += int(m.group('warnings'))
82 def print_stats(self, file = None):
84 (dirname, filename) = os.path.split(file)
85 print "\r\033[0K%d packages and %d specfiles checked; %d errors, %d warnings. [%s]" % (self.packages, self.specfiles, self.errors, self.warnings, filename),
87 print "\r\033[0K%d packages and %d specfiles checked; %d errors, %d warnings." % (self.packages, self.specfiles, self.errors, self.warnings)
90 def rpmlint(self, file):
91 cachefile = self.cachefile(file)
94 if not os.path.exists(cachefile) or os.stat(file).st_mtime > os.stat(cachefile).st_mtime:
95 cmd = [self._rpmlint, file]
96 outfd = open(cachefile, 'w')
98 rc = subprocess.call(cmd, stdin = subprocess.PIPE, stdout = outfd, stderr = outfd, close_fds = True)
99 except KeyboardInterrupt:
103 if not self.update_stats(file):
108 print "rpmlint of %d files from %d packages" % (len(files), len(tree.loadedpkgs))
109 lint = LintPkg("~/tmp/rpmlint")
112 lint.print_stats(file)