]>
Commit | Line | Data |
---|---|---|
03861ee2 ER |
1 | #!/usr/bin/env python |
2 | # vi: encoding=utf-8 ts=8 sts=4 sw=4 et | |
3 | ||
c88699b9 | 4 | import sys, os, re |
66a58132 | 5 | import getopt |
03861ee2 ER |
6 | import subprocess |
7 | sys.path.insert(0, os.environ['HOME']+'/pld-ftp-admin/modules') | |
8 | import ftptree | |
9 | from common import checkdir | |
10 | import ftpio | |
11 | ||
66a58132 | 12 | try: |
7dd6fac6 | 13 | opts, args = getopt.getopt(sys.argv[1:], 'qsdo:', [ "quiet" ]) |
66a58132 ER |
14 | except getopt.GetoptError: |
15 | print >>sys.stderr, "ERR: options error" | |
16 | print >>sys.stderr, "rpmlint.py tree package1 [package2...]" | |
17 | sys.exit(1) | |
18 | ||
19 | quiet = False | |
413b382d | 20 | show = False |
7dd6fac6 | 21 | debugfiles = False |
cb260c8e | 22 | outstream = sys.stdout |
66a58132 ER |
23 | for o, a in opts: |
24 | if o == "-q" or o == "--quiet": | |
25 | quiet = True | |
413b382d ER |
26 | if o == "-s": |
27 | show = True | |
7dd6fac6 ER |
28 | if o == "-d": |
29 | debugfiles = True | |
cb260c8e ER |
30 | if o == "-o": |
31 | outstream = open(a, 'w') | |
66a58132 ER |
32 | |
33 | if len(args) < 1: | |
34 | print >>sys.stderr, "ERR: missing tree name" | |
03861ee2 ER |
35 | print >>sys.stderr, "rpmlint.py tree package1 [package2...]" |
36 | sys.exit(1) | |
37 | ||
66a58132 ER |
38 | treename = args[0] |
39 | packages = args[1:] | |
40 | ||
41 | checkdir(treename) | |
03861ee2 ER |
42 | |
43 | ftpio.connect('rpmlint') | |
44 | ||
66a58132 ER |
45 | if not ftpio.lock(treename, True): |
46 | print >>sys.stderr, "ERR: %s tree already locked" % treename | |
03861ee2 ER |
47 | sys.exit(1) |
48 | ||
49 | files = [] | |
50 | try: | |
66a58132 | 51 | if len(packages) < 1: |
7e771c27 ER |
52 | loadall = True |
53 | else: | |
54 | loadall = False | |
55 | ||
56 | # if no files specified, grab whole tree contents | |
66a58132 | 57 | tree = ftptree.FtpTree(treename, loadall = loadall) |
413b382d | 58 | tree.do_checkbuild = False |
7e771c27 ER |
59 | if loadall: |
60 | # this is hack, should be a param, not access private .loadedpkgs element | |
61 | tree.mark4moving(tree.loadedpkgs) | |
62 | else: | |
66a58132 | 63 | tree.mark4moving(packages) |
7dd6fac6 | 64 | files = tree.rpmfiles(debugfiles = debugfiles, sourcefiles = False) |
03861ee2 | 65 | |
5a13eb89 | 66 | except (ftptree.SomeError, KeyboardInterrupt), e: |
03861ee2 | 67 | # In case of problems we need to unlock the tree before exiting |
66a58132 | 68 | ftpio.unlock(treename) |
03861ee2 ER |
69 | sys.exit(1) |
70 | ||
66a58132 | 71 | ftpio.unlock(treename) |
03861ee2 | 72 | |
8d0f868f ER |
73 | class LintPkg: |
74 | def __init__(self, cachedir): | |
cb260c8e ER |
75 | self.outstream = sys.stdout |
76 | ||
8d0f868f ER |
77 | # for rpmlint stats |
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.') | |
81 | ||
82 | self._rpmlint = '/usr/bin/rpmlint' | |
83 | ||
84 | self.cachedir = os.path.expanduser(cachedir) | |
85 | if not os.path.isdir(self.cachedir): | |
86 | os.makedirs(self.cachedir) | |
87 | ||
88 | def cachefile(self, file): | |
89 | (dirname, filename) = os.path.split(file) | |
90 | return os.path.join(self.cachedir, filename+'.txt') | |
91 | ||
413b382d | 92 | def get_stats(self, file): |
8d0f868f | 93 | cachefile = self.cachefile(file) |
f809cdb0 ER |
94 | if not os.path.exists(cachefile): |
95 | return None | |
8d0f868f ER |
96 | |
97 | # show last line (that contains status) | |
98 | l = (open(cachefile, 'r').readlines())[-1] | |
99 | m = self.lintre.match(l) | |
100 | if not m: | |
413b382d | 101 | return None |
8d0f868f | 102 | |
413b382d ER |
103 | return { |
104 | 'packages': int(m.group('packages')), | |
105 | 'specfiles': int(m.group('specfiles')), | |
106 | 'errors': int(m.group('errors')), | |
107 | 'warnings': int(m.group('warnings')), | |
108 | } | |
109 | ||
110 | """ | |
111 | update stats from cachefile | |
112 | """ | |
113 | def update_stats(self, file): | |
114 | m = self.get_stats(file) | |
115 | if not m: | |
116 | return False | |
117 | self.packages += m['packages'] | |
118 | self.specfiles += m['specfiles'] | |
119 | self.errors += m['errors'] | |
120 | self.warnings += m['warnings'] | |
8d0f868f ER |
121 | return True |
122 | ||
123 | def print_stats(self, file = None): | |
124 | if file: | |
125 | (dirname, filename) = os.path.split(file) | |
cb260c8e | 126 | 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), |
8d0f868f | 127 | else: |
cb260c8e | 128 | print >>self.outstream, "\r\033[0K%d packages and %d specfiles checked; %d errors, %d warnings." % (self.packages, self.specfiles, self.errors, self.warnings) |
8d0f868f ER |
129 | sys.stdout.flush() |
130 | ||
f809cdb0 | 131 | def cat(self, file): |
cb260c8e | 132 | print >>self.outstream, "".join(open(file, 'r').readlines()) |
f809cdb0 | 133 | |
413b382d ER |
134 | def show_results(self, file): |
135 | m = self.get_stats(file) | |
136 | if not m: | |
137 | return False | |
138 | ||
139 | cachefile = self.cachefile(file) | |
f809cdb0 | 140 | if not os.path.exists(cachefile): |
cb260c8e | 141 | print >>self.outsteram, "MISSING: report: %s" % file |
f809cdb0 | 142 | |
413b382d | 143 | if m['errors'] > 0 or m['warnings'] > 0: |
cb260c8e ER |
144 | (dirname, filename) = os.path.split(file) |
145 | print >>self.outstream, "rpmlint: %s" % filename | |
f809cdb0 | 146 | self.cat(cachefile) |
413b382d | 147 | |
8d0f868f ER |
148 | def rpmlint(self, file): |
149 | cachefile = self.cachefile(file) | |
150 | ||
151 | rc = None | |
152 | if not os.path.exists(cachefile) or os.stat(file).st_mtime > os.stat(cachefile).st_mtime: | |
153 | cmd = [self._rpmlint, file] | |
154 | outfd = open(cachefile, 'w') | |
155 | try: | |
ffcd80d9 ER |
156 | env = {'TZ': 'GMT'} |
157 | rc = subprocess.call(cmd, stdin = subprocess.PIPE, stdout = outfd, stderr = outfd, env = env, close_fds = True) | |
8d0f868f ER |
158 | except KeyboardInterrupt: |
159 | os.unlink(cachefile) | |
160 | raise | |
161 | outfd.close() | |
162 | if not self.update_stats(file): | |
f809cdb0 ER |
163 | # update failed, dump cache and remove it |
164 | self.cat(cachefile) | |
8095749f | 165 | os.unlink(cachefile) |
8d0f868f ER |
166 | rc = 1 |
167 | return rc == 0 | |
03861ee2 | 168 | |
ec7915aa ER |
169 | try: |
170 | lock = 'rpmlint:'+treename | |
171 | if not ftpio.lock(lock, True): | |
172 | print >>sys.stderr, "ERR: %s tree already locked for rpmlint" % treename | |
173 | sys.exit(1) | |
f0cc35ee | 174 | |
66a58132 | 175 | if not quiet: |
cb260c8e | 176 | print >>outstream, "rpmlint of %d files from %d packages" % (len(files), len(tree.loadedpkgs)) |
ec7915aa | 177 | lint = LintPkg("~/tmp/rpmlint") |
cb260c8e | 178 | lint.outstream = outstream |
ec7915aa ER |
179 | for file in files: |
180 | lint.rpmlint(file) | |
181 | if not quiet: | |
182 | lint.print_stats(file) | |
183 | if show: | |
184 | lint.show_results(file) | |
8d0f868f | 185 | |
ec7915aa ER |
186 | if not quiet: |
187 | lint.print_stats() | |
f0cc35ee | 188 | |
ec7915aa | 189 | ftpio.unlock(lock) |
cb260c8e | 190 | except (Exception, KeyboardInterrupt): |
ec7915aa ER |
191 | ftpio.unlock(lock) |
192 | raise |