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