2 # vi: encoding=utf-8 ts=8 sts=4 sw=4 et
7 sys.path.insert(0, os.environ['HOME']+'/pld-ftp-admin/modules')
9 from common import checkdir
13 opts, args = getopt.getopt(sys.argv[1:], 'qs', [ "quiet" ])
14 except getopt.GetoptError:
15 print >>sys.stderr, "ERR: options error"
16 print >>sys.stderr, "rpmlint.py tree package1 [package2...]"
22 if o == "-q" or o == "--quiet":
28 print >>sys.stderr, "ERR: missing tree name"
29 print >>sys.stderr, "rpmlint.py tree package1 [package2...]"
37 ftpio.connect('rpmlint')
39 if not ftpio.lock(treename, True):
40 print >>sys.stderr, "ERR: %s tree already locked" % treename
50 # if no files specified, grab whole tree contents
51 tree = ftptree.FtpTree(treename, loadall = loadall)
52 tree.do_checkbuild = False
54 # this is hack, should be a param, not access private .loadedpkgs element
55 tree.mark4moving(tree.loadedpkgs)
57 tree.mark4moving(packages)
58 files = tree.rpmfiles(debugfiles = False, sourcefiles = False)
60 except (ftptree.SomeError, KeyboardInterrupt), e:
61 # In case of problems we need to unlock the tree before exiting
62 ftpio.unlock(treename)
65 ftpio.unlock(treename)
68 def __init__(self, cachedir):
70 self.packages = self.specfiles = self.errors = self.warnings = 0
71 # 1 packages and 0 specfiles checked; 0 errors, 0 warnings.
72 self.lintre = re.compile('(?P<packages>\d+) packages and (?P<specfiles>\d+) specfiles checked; (?P<errors>\d+) errors, (?P<warnings>\d+) warnings.')
74 self._rpmlint = '/usr/bin/rpmlint'
76 self.cachedir = os.path.expanduser(cachedir)
77 if not os.path.isdir(self.cachedir):
78 os.makedirs(self.cachedir)
80 def cachefile(self, file):
81 (dirname, filename) = os.path.split(file)
82 return os.path.join(self.cachedir, filename+'.txt')
84 def get_stats(self, file):
85 cachefile = self.cachefile(file)
87 # show last line (that contains status)
88 l = (open(cachefile, 'r').readlines())[-1]
89 m = self.lintre.match(l)
94 'packages': int(m.group('packages')),
95 'specfiles': int(m.group('specfiles')),
96 'errors': int(m.group('errors')),
97 'warnings': int(m.group('warnings')),
101 update stats from cachefile
103 def update_stats(self, file):
104 m = self.get_stats(file)
107 self.packages += m['packages']
108 self.specfiles += m['specfiles']
109 self.errors += m['errors']
110 self.warnings += m['warnings']
113 def print_stats(self, file = None):
115 (dirname, filename) = os.path.split(file)
116 print "\r\033[0K%d packages and %d specfiles checked; %d errors, %d warnings. [%s]" % (self.packages, self.specfiles, self.errors, self.warnings, filename),
118 print "\r\033[0K%d packages and %d specfiles checked; %d errors, %d warnings." % (self.packages, self.specfiles, self.errors, self.warnings)
121 def show_results(self, file):
122 m = self.get_stats(file)
126 cachefile = self.cachefile(file)
127 if m['errors'] > 0 or m['warnings'] > 0:
128 print "".join(open(cachefile, 'r').readlines())
130 def rpmlint(self, file):
131 cachefile = self.cachefile(file)
134 if not os.path.exists(cachefile) or os.stat(file).st_mtime > os.stat(cachefile).st_mtime:
135 cmd = [self._rpmlint, file]
136 outfd = open(cachefile, 'w')
138 rc = subprocess.call(cmd, stdin = subprocess.PIPE, stdout = outfd, stderr = outfd, close_fds = True)
139 except KeyboardInterrupt:
143 if not self.update_stats(file):
149 print "rpmlint of %d files from %d packages" % (len(files), len(tree.loadedpkgs))
150 lint = LintPkg("~/tmp/rpmlint")
154 lint.print_stats(file)
156 lint.show_results(file)