]> git.pld-linux.org Git - projects/pld-ftp-admin.git/blame - bin/pfa-lintpkg
- add getopt support and -q option
[projects/pld-ftp-admin.git] / bin / pfa-lintpkg
CommitLineData
03861ee2
ER
1#!/usr/bin/env python
2# vi: encoding=utf-8 ts=8 sts=4 sw=4 et
3
c88699b9 4import sys, os, re
66a58132 5import getopt
03861ee2
ER
6import subprocess
7sys.path.insert(0, os.environ['HOME']+'/pld-ftp-admin/modules')
8import ftptree
9from common import checkdir
10import ftpio
11
66a58132
ER
12try:
13 opts, args = getopt.getopt(sys.argv[1:], 'q', [ "quiet" ])
14except getopt.GetoptError:
15 print >>sys.stderr, "ERR: options error"
16 print >>sys.stderr, "rpmlint.py tree package1 [package2...]"
17 sys.exit(1)
18
19quiet = False
20for o, a in opts:
21 if o == "-q" or o == "--quiet":
22 quiet = True
23
24if len(args) < 1:
25 print >>sys.stderr, "ERR: missing tree name"
03861ee2
ER
26 print >>sys.stderr, "rpmlint.py tree package1 [package2...]"
27 sys.exit(1)
28
66a58132
ER
29treename = args[0]
30packages = args[1:]
31
32checkdir(treename)
03861ee2
ER
33
34ftpio.connect('rpmlint')
35
66a58132
ER
36if not ftpio.lock(treename, True):
37 print >>sys.stderr, "ERR: %s tree already locked" % treename
03861ee2
ER
38 sys.exit(1)
39
40files = []
41try:
66a58132 42 if len(packages) < 1:
7e771c27
ER
43 loadall = True
44 else:
45 loadall = False
46
47 # if no files specified, grab whole tree contents
66a58132 48 tree = ftptree.FtpTree(treename, loadall = loadall)
7e771c27
ER
49 if loadall:
50 # this is hack, should be a param, not access private .loadedpkgs element
51 tree.mark4moving(tree.loadedpkgs)
52 else:
66a58132 53 tree.mark4moving(packages)
03861ee2
ER
54 files = tree.rpmfiles(debugfiles = False, sourcefiles = False)
55
5a13eb89 56except (ftptree.SomeError, KeyboardInterrupt), e:
03861ee2 57 # In case of problems we need to unlock the tree before exiting
66a58132 58 ftpio.unlock(treename)
03861ee2
ER
59 sys.exit(1)
60
66a58132 61ftpio.unlock(treename)
03861ee2 62
8d0f868f
ER
63class 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):
8095749f 120 os.unlink(cachefile)
8d0f868f
ER
121 rc = 1
122 return rc == 0
03861ee2 123
66a58132
ER
124if not quiet:
125 print "rpmlint of %d files from %d packages" % (len(files), len(tree.loadedpkgs))
8d0f868f
ER
126lint = LintPkg("~/tmp/rpmlint")
127for file in files:
128 lint.rpmlint(file)
66a58132
ER
129 if not quiet:
130 lint.print_stats(file)
8d0f868f 131
66a58132
ER
132if not quiet:
133 lint.print_stats()
This page took 0.115348 seconds and 4 git commands to generate.