2 # vi: encoding=utf-8 ts=8 sts=4 sw=4 et
7 sys.path.insert(0, os.environ['HOME']+'/tld-ftp-admin/modules')
9 from common import checkdir
13 opts, args = getopt.getopt(sys.argv[1:], 'qsdo:', [ "quiet" ])
14 except getopt.GetoptError:
15 print >>sys.stderr, "ERR: options error"
16 print >>sys.stderr, "rpmlint.py tree package1 [package2...]"
22 outstream = sys.stdout
24 if o == "-q" or o == "--quiet":
31 outstream = open(a, 'w')
34 print >>sys.stderr, "ERR: missing tree name"
35 print >>sys.stderr, "rpmlint.py tree package1 [package2...]"
43 ftpio.connect('rpmlint')
45 if not ftpio.lock(treename, True):
46 print >>sys.stderr, "ERR: %s tree already locked" % treename
56 # if no files specified, grab whole tree contents
57 tree = ftptree.FtpTree(treename, loadall = loadall)
58 tree.do_checkbuild = False
60 # this is hack, should be a param, not access private .loadedpkgs element
61 tree.mark4moving(tree.loadedpkgs)
63 tree.mark4moving(packages)
64 files = tree.rpmfiles(debugfiles = debugfiles, sourcefiles = False)
66 except (ftptree.SomeError, KeyboardInterrupt), e:
67 # In case of problems we need to unlock the tree before exiting
68 ftpio.unlock(treename)
71 ftpio.unlock(treename)
74 def __init__(self, cachedir):
75 self.outstream = sys.stdout
78 self.packages = self.specfiles = self.errors = self.warnings = 0
79 # 1 packages and 0 specfiles checked; 0 errors, 0 warnings.
80 self.lintre = re.compile('(?P<packages>\d+) packages and (?P<specfiles>\d+) specfiles checked; (?P<errors>\d+) errors, (?P<warnings>\d+) warnings.')
82 self._rpmlint = '/usr/bin/rpmlint'
84 # mtime, which invalidates all caches
86 rpmlintrc = os.path.expanduser("~/.config/rpmlint")
87 if os.path.exists(rpmlintrc):
88 self.mtime = os.stat(rpmlintrc).st_mtime
90 self.cachedir = os.path.expanduser(cachedir)
91 if not os.path.isdir(self.cachedir):
92 os.makedirs(self.cachedir)
94 def cachefile(self, file):
95 (dirname, filename) = os.path.split(file)
96 return os.path.join(self.cachedir, filename+'.txt')
98 def get_stats(self, file):
99 cachefile = self.cachefile(file)
100 if not os.path.exists(cachefile):
103 # show last line (that contains status)
104 l = (open(cachefile, 'r').readlines())[-1]
105 m = self.lintre.match(l)
110 'packages': int(m.group('packages')),
111 'specfiles': int(m.group('specfiles')),
112 'errors': int(m.group('errors')),
113 'warnings': int(m.group('warnings')),
117 update stats from cachefile
119 def update_stats(self, file):
120 m = self.get_stats(file)
123 self.packages += m['packages']
124 self.specfiles += m['specfiles']
125 self.errors += m['errors']
126 self.warnings += m['warnings']
129 def print_stats(self, file = None):
131 (dirname, filename) = os.path.split(file)
132 print >>self.outstream, "\r\033[0K%d packages and %d specfiles checked; %d errors, %d warnings. [%s]" % (self.packages, self.specfiles, self.errors, self.warnings, filename),
134 print >>self.outstream, "\r\033[0K%d packages and %d specfiles checked; %d errors, %d warnings." % (self.packages, self.specfiles, self.errors, self.warnings)
138 print >>self.outstream, "".join(open(file, 'r').readlines())
140 def show_results(self, file):
141 m = self.get_stats(file)
145 cachefile = self.cachefile(file)
146 if not os.path.exists(cachefile):
147 print >>self.outsteram, "MISSING: report: %s" % file
149 if m['errors'] > 0 or m['warnings'] > 0:
150 (dirname, filename) = os.path.split(file)
151 print >>self.outstream, "rpmlint: %s" % filename
154 def rpmlint(self, file):
155 cachefile = self.cachefile(file)
158 if not os.path.exists(cachefile) \
159 or os.stat(file).st_mtime > os.stat(cachefile).st_mtime \
160 or (self.mtime and self.mtime > os.stat(cachefile).st_mtime):
161 cmd = [self._rpmlint, file]
162 outfd = open(cachefile, 'w')
165 rc = subprocess.call(cmd, stdin = subprocess.PIPE, stdout = outfd, stderr = outfd, env = env, close_fds = True)
166 except KeyboardInterrupt:
170 if not self.update_stats(file):
171 # update failed, dump cache and remove it
178 lock = 'rpmlint:'+treename
179 if not ftpio.lock(lock, True):
180 print >>sys.stderr, "ERR: %s tree already locked for rpmlint" % treename
184 print >>outstream, "rpmlint of %d files from %d packages" % (len(files), len(tree.loadedpkgs))
185 lint = LintPkg("~/tmp/rpmlint")
186 lint.outstream = outstream
190 lint.print_stats(file)
192 lint.show_results(file)
198 except (Exception, KeyboardInterrupt):