summaryrefslogtreecommitdiff
path: root/bin/pfa-lintpkg
blob: 716cd88040cdaee03dae58fb875cabf9ef4317ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env python
# vi: encoding=utf-8 ts=8 sts=4 sw=4 et

from __future__ import print_function

import sys, os, re
import getopt
import subprocess
sys.path.insert(0, os.environ['HOME']+'/pld-ftp-admin/modules')
import ftptree
from common import checkdir
import ftpio

try:
    opts, args = getopt.getopt(sys.argv[1:], 'qsdo:', [ "quiet" ])
except getopt.GetoptError:
    print("ERR: options error", file=sys.stderr)
    print("rpmlint.py tree package1 [package2...]", file=sys.stderr)
    sys.exit(1)

quiet = False
show = False
debugfiles = False
outstream = sys.stdout
for o, a in opts:
    if o == "-q" or o == "--quiet":
        quiet = True
    if o == "-s":
        show = True
    if o == "-d":
        debugfiles = True
    if o == "-o":
        outstream = open(a, 'w')

if len(args) < 1:
    print("ERR: missing tree name", file=sys.stderr)
    print("rpmlint.py tree package1 [package2...]", file=sys.stderr)
    sys.exit(1)

treename = args[0]
packages = args[1:]

checkdir(treename)

ftpio.connect('rpmlint')

if not ftpio.lock(treename, True):
    print("ERR: %s tree already locked" % treename, file=sys.stderr)
    sys.exit(1)

files = []
try:
    if len(packages) < 1:
        loadall = True
    else:
        loadall = False

    # if no files specified, grab whole tree contents
    tree = ftptree.FtpTree(treename, loadall = loadall)
    tree.do_checkbuild = False
    if loadall:
        # this is hack, should be a param, not access private .loadedpkgs element
        tree.mark4moving(tree.loadedpkgs)
    else:
        tree.mark4moving(packages)
    files = tree.rpmfiles(debugfiles = debugfiles, sourcefiles = False)

except (ftptree.SomeError, KeyboardInterrupt) as e:
    # In case of problems we need to unlock the tree before exiting
    ftpio.unlock(treename)
    sys.exit(1)

ftpio.unlock(treename)

class LintPkg:
    def __init__(self, cachedir):
        self.outstream = sys.stdout

        # for rpmlint stats
        self.packages = self.specfiles = self.errors = self.warnings = 0
        # 1 packages and 0 specfiles checked; 0 errors, 0 warnings.
        self.lintre = re.compile('(?P<packages>\d+) packages and (?P<specfiles>\d+) specfiles checked; (?P<errors>\d+) errors, (?P<warnings>\d+) warnings.')

        self._rpmlint = '/usr/bin/rpmlint'

        # mtime, which invalidates all caches
        self.mtime = None
        rpmlintrc = os.path.expanduser("~/.config/rpmlint")
        if os.path.exists(rpmlintrc):
            self.mtime = os.stat(rpmlintrc).st_mtime

        self.cachedir = os.path.expanduser(cachedir)
        if not os.path.isdir(self.cachedir):
            os.makedirs(self.cachedir)

    def cachefile(self, file):
        (dirname, filename) = os.path.split(file)
        return os.path.join(self.cachedir, filename+'.txt')

    def get_stats(self, file):
        cachefile = self.cachefile(file)
        if not os.path.exists(cachefile):
            return None

        # show last line (that contains status)
        l = (open(cachefile, 'r').readlines())[-1]
        m = self.lintre.match(l)
        if not m:
            return None

        return {
            'packages': int(m.group('packages')),
            'specfiles': int(m.group('specfiles')),
            'errors': int(m.group('errors')),
            'warnings': int(m.group('warnings')),
        }

    """
    update stats from cachefile
    """
    def update_stats(self, file):
        m = self.get_stats(file)
        if not m:
            return False
        self.packages += m['packages']
        self.specfiles += m['specfiles']
        self.errors += m['errors']
        self.warnings += m['warnings']
        return True

    def print_stats(self, file = None):
        if file:
            (dirname, filename) = os.path.split(file)
            print("\r\033[0K%d packages and %d specfiles checked; %d errors, %d warnings. [%s]" % (self.packages, self.specfiles, self.errors, self.warnings, filename),, file=self.outstream)
        else:
            print("\r\033[0K%d packages and %d specfiles checked; %d errors, %d warnings." % (self.packages, self.specfiles, self.errors, self.warnings), file=self.outstream)
        sys.stdout.flush()

    def cat(self, file):
        print("".join(open(file, 'r').readlines()), file=self.outstream)

    def show_results(self, file):
        m = self.get_stats(file)
        if not m:
            return False

        cachefile = self.cachefile(file)
        if not os.path.exists(cachefile):
            print("MISSING: report: %s" % file, file=self.outstream)

        if m['errors'] > 0 or m['warnings'] > 0:
            (dirname, filename) = os.path.split(file)
            print("rpmlint: %s" % filename, file=self.outstream)
            self.cat(cachefile)

    def rpmlint(self, file):
        cachefile = self.cachefile(file)

        rc = None
        if not os.path.exists(cachefile) \
            or os.stat(file).st_mtime > os.stat(cachefile).st_mtime \
            or (self.mtime and self.mtime > os.stat(cachefile).st_mtime):
            cmd = [self._rpmlint, file]
            outfd = open(cachefile, 'w')
            try:
                env = {'TZ': 'GMT'}
                rc = subprocess.call(cmd, stdin = subprocess.PIPE, stdout = outfd, stderr = outfd, env = env, close_fds = True)
            except KeyboardInterrupt:
                os.unlink(cachefile)
                raise
            outfd.close()
        if not self.update_stats(file):
            # update failed, dump cache and remove it
            self.cat(cachefile)
            os.unlink(cachefile)
            rc = 1
        return rc == 0

try:
    lock = 'rpmlint:'+treename
    if not ftpio.lock(lock, True):
        print("ERR: %s tree already locked for rpmlint" % treename, file=sys.stderr)
        sys.exit(1)

    if not quiet:
        print("rpmlint of %d files from %d packages" % (len(files), len(tree.loadedpkgs)), file=outstream)
    lint = LintPkg("~/tmp/rpmlint")
    lint.outstream = outstream
    for file in files:
        lint.rpmlint(file)
        if not quiet:
            lint.print_stats(file)
        if show:
            lint.show_results(file)

    if not quiet:
        lint.print_stats()

    ftpio.unlock(lock)
except (Exception, KeyboardInterrupt):
    ftpio.unlock(lock)
    raise