]> git.pld-linux.org Git - projects/pld-ftp-admin.git/blob - bin/pfa-lintpkg
- add getopt support and -q option
[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:], 'q', [ "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 for o, a in opts:
21     if o == "-q" or o == "--quiet":
22         quiet = True
23
24 if len(args) < 1:
25     print >>sys.stderr, "ERR: missing tree name"
26     print >>sys.stderr, "rpmlint.py tree package1 [package2...]"
27     sys.exit(1)
28
29 treename = args[0]
30 packages = args[1:]
31
32 checkdir(treename)
33
34 ftpio.connect('rpmlint')
35
36 if not ftpio.lock(treename, True):
37     print >>sys.stderr, "ERR: %s tree already locked" % treename
38     sys.exit(1)
39
40 files = []
41 try:
42     if len(packages) < 1:
43         loadall = True
44     else:
45         loadall = False
46
47     # if no files specified, grab whole tree contents
48     tree = ftptree.FtpTree(treename, loadall = loadall)
49     if loadall:
50         # this is hack, should be a param, not access private .loadedpkgs element
51         tree.mark4moving(tree.loadedpkgs)
52     else:
53         tree.mark4moving(packages)
54     files = tree.rpmfiles(debugfiles = False, sourcefiles = False)
55
56 except (ftptree.SomeError, KeyboardInterrupt), e:
57     # In case of problems we need to unlock the tree before exiting
58     ftpio.unlock(treename)
59     sys.exit(1)
60
61 ftpio.unlock(treename)
62
63 class LintPkg:
64     def __init__(self, cachedir):
65         # for rpmlint stats
66         self.packages = self.specfiles = self.errors = self.warnings = 0
67         # 1 packages and 0 specfiles checked; 0 errors, 0 warnings.
68         self.lintre = re.compile('(?P<packages>\d+) packages and (?P<specfiles>\d+) specfiles checked; (?P<errors>\d+) errors, (?P<warnings>\d+) warnings.')
69
70         self._rpmlint = '/usr/bin/rpmlint'
71
72         self.cachedir = os.path.expanduser(cachedir)
73         if not os.path.isdir(self.cachedir):
74             os.makedirs(self.cachedir)
75
76     def cachefile(self, file):
77         (dirname, filename) = os.path.split(file)
78         return os.path.join(self.cachedir, filename+'.txt')
79
80     """
81     update stats from cachefile
82     """
83     def update_stats(self, file):
84         cachefile = self.cachefile(file)
85
86         # show last line (that contains status)
87         l = (open(cachefile, 'r').readlines())[-1]
88         m = self.lintre.match(l)
89         if not m:
90             return False
91
92         self.packages += int(m.group('packages'))
93         self.specfiles += int(m.group('specfiles'))
94         self.errors += int(m.group('errors'))
95         self.warnings += int(m.group('warnings'))
96         return True
97
98     def print_stats(self, file = None):
99         if file:
100             (dirname, filename) = os.path.split(file)
101             print "\r\033[0K%d packages and %d specfiles checked; %d errors, %d warnings. [%s]" % (self.packages, self.specfiles, self.errors, self.warnings, filename),
102         else:
103             print "\r\033[0K%d packages and %d specfiles checked; %d errors, %d warnings." % (self.packages, self.specfiles, self.errors, self.warnings)
104         sys.stdout.flush()
105
106     def rpmlint(self, file):
107         cachefile = self.cachefile(file)
108
109         rc = None
110         if not os.path.exists(cachefile) or os.stat(file).st_mtime > os.stat(cachefile).st_mtime:
111             cmd = [self._rpmlint, file]
112             outfd = open(cachefile, 'w')
113             try:
114                 rc = subprocess.call(cmd, stdin = subprocess.PIPE, stdout = outfd, stderr = outfd, close_fds = True)
115             except KeyboardInterrupt:
116                 os.unlink(cachefile)
117                 raise
118             outfd.close()
119         if not self.update_stats(file):
120             os.unlink(cachefile)
121             rc = 1
122         return rc == 0
123
124 if not quiet:
125     print "rpmlint of %d files from %d packages" % (len(files), len(tree.loadedpkgs))
126 lint = LintPkg("~/tmp/rpmlint")
127 for file in files:
128     lint.rpmlint(file)
129     if not quiet:
130         lint.print_stats(file)
131
132 if not quiet:
133     lint.print_stats()
This page took 0.028635 seconds and 4 git commands to generate.