]> git.pld-linux.org Git - projects/pld-ftp-admin.git/blob - bin/pfa-lintpkg
make sure scripts are always executable
[projects/pld-ftp-admin.git] / bin / pfa-lintpkg
1 #!/usr/bin/env python
2 # vi: encoding=utf-8 ts=8 sts=4 sw=4 et
3
4 import sys, os, re
5 import getopt
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
12 try:
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...]"
17     sys.exit(1)
18
19 quiet = False
20 show = False
21 debugfiles = False
22 outstream = sys.stdout
23 for o, a in opts:
24     if o == "-q" or o == "--quiet":
25         quiet = True
26     if o == "-s":
27         show = True
28     if o == "-d":
29         debugfiles = True
30     if o == "-o":
31         outstream = open(a, 'w')
32
33 if len(args) < 1:
34     print >>sys.stderr, "ERR: missing tree name"
35     print >>sys.stderr, "rpmlint.py tree package1 [package2...]"
36     sys.exit(1)
37
38 treename = args[0]
39 packages = args[1:]
40
41 checkdir(treename)
42
43 ftpio.connect('rpmlint')
44
45 if not ftpio.lock(treename, True):
46     print >>sys.stderr, "ERR: %s tree already locked" % treename
47     sys.exit(1)
48
49 files = []
50 try:
51     if len(packages) < 1:
52         loadall = True
53     else:
54         loadall = False
55
56     # if no files specified, grab whole tree contents
57     tree = ftptree.FtpTree(treename, loadall = loadall)
58     tree.do_checkbuild = False
59     if loadall:
60         # this is hack, should be a param, not access private .loadedpkgs element
61         tree.mark4moving(tree.loadedpkgs)
62     else:
63         tree.mark4moving(packages)
64     files = tree.rpmfiles(debugfiles = debugfiles, sourcefiles = False)
65
66 except (ftptree.SomeError, KeyboardInterrupt), e:
67     # In case of problems we need to unlock the tree before exiting
68     ftpio.unlock(treename)
69     sys.exit(1)
70
71 ftpio.unlock(treename)
72
73 class LintPkg:
74     def __init__(self, cachedir):
75         self.outstream = sys.stdout
76
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
92     def get_stats(self, file):
93         cachefile = self.cachefile(file)
94         if not os.path.exists(cachefile):
95             return None
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:
101             return None
102
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']
121         return True
122
123     def print_stats(self, file = None):
124         if file:
125             (dirname, filename) = os.path.split(file)
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),
127         else:
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)
129         sys.stdout.flush()
130
131     def cat(self, file):
132         print >>self.outstream, "".join(open(file, 'r').readlines())
133
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)
140         if not os.path.exists(cachefile):
141             print >>self.outsteram, "MISSING: report: %s" % file
142
143         if m['errors'] > 0 or m['warnings'] > 0:
144             (dirname, filename) = os.path.split(file)
145             print >>self.outstream, "rpmlint: %s" % filename
146             self.cat(cachefile)
147
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:
156                 env = {'TZ': 'GMT'}
157                 rc = subprocess.call(cmd, stdin = subprocess.PIPE, stdout = outfd, stderr = outfd, env = env, close_fds = True)
158             except KeyboardInterrupt:
159                 os.unlink(cachefile)
160                 raise
161             outfd.close()
162         if not self.update_stats(file):
163             # update failed, dump cache and remove it
164             self.cat(cachefile)
165             os.unlink(cachefile)
166             rc = 1
167         return rc == 0
168
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)
174
175     if not quiet:
176         print >>outstream, "rpmlint of %d files from %d packages" % (len(files), len(tree.loadedpkgs))
177     lint = LintPkg("~/tmp/rpmlint")
178     lint.outstream = outstream
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)
185
186     if not quiet:
187         lint.print_stats()
188
189     ftpio.unlock(lock)
190 except (Exception, KeyboardInterrupt):
191     ftpio.unlock(lock)
192     raise
This page took 0.079968 seconds and 3 git commands to generate.