File: piuparts-analyze.py

package info (click to toggle)
piuparts 1.0.0
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,108 kB
  • sloc: python: 7,409; sh: 3,989; makefile: 167
file content (374 lines) | stat: -rw-r--r-- 13,975 bytes parent folder | download | duplicates (3)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright 2005 Lars Wirzenius (liw@iki.fi)
# Copyright 2011 Mika Pflüger (debian@mikapflueger.de)
# Copyright © 2012-2017 Andreas Beckmann (anbe@debian.org)
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <https://www.gnu.org/licenses/>


"""Analyze failed piuparts logs and move them around when the errors are known.

This program looks at piuparts log files in ./fail, and queries the bts to find
out if bugs have been filed already. If so, it moves them to ./bugged.
It tries to detect if new versions of bugged packages are uploaded without solving
the bug and will then update the bts with the new found versions, and copy the
headers of the log in ./fail to the one in ./bugged and vice versa. It will then
move the failed log to ./bugged as well.

"""
from __future__ import print_function

import os
import sys
import time
import re
import shutil
import subprocess
import fcntl

import debianbts
import apt_pkg
from signal import alarm, signal, SIGALRM
from collections import deque

import piupartslib.conf
from piupartslib.conf import MissingSection


CONFIG_FILE = "/etc/piuparts/piuparts.conf"

apt_pkg.init_system()

error_pattern = re.compile(r"(?<=\n).*error.*\n?", flags=re.IGNORECASE)
chroot_pattern = re.compile(r"tmp/tmp.*?'")

############################################################################

class BTS_Timeout(Exception):
    pass


def alarm_handler(signum, frame):
    raise BTS_Timeout


class PiupartsBTS():

    def __init__(self):
        self._bugs_usertagged_piuparts = None
        self._bugs_in_package = dict()
        self._bugs_affecting_package = dict()
        self._bug_versions = dict()

        self._queries = 0
        self._misses = 0

    def all_piuparts_bugs(self):
        if self._bugs_usertagged_piuparts is None:
            self._bugs_usertagged_piuparts = debianbts.get_usertag("debian-qa@lists.debian.org", 'piuparts')['piuparts']
        return self._bugs_usertagged_piuparts

    def bugs_in(self, package):
        if not package in self._bugs_in_package:
            self._misses += 1
            signal(SIGALRM, alarm_handler)
            alarm(120)
            bugs = debianbts.get_bugs('package', package, 'bugs', self.all_piuparts_bugs(), 'archive', 'both')
            bugs += debianbts.get_bugs('package', 'src:' + package, 'bugs', self.all_piuparts_bugs(), 'archive', 'both')
            alarm(0)
            self._bugs_in_package[package] = sorted(set(bugs), reverse=True)
        self._queries += 1
        return self._bugs_in_package[package]

    def bugs_affecting(self, package):
        if not package in self._bugs_affecting_package:
            self._misses += 1
            signal(SIGALRM, alarm_handler)
            alarm(120)
            bugs = debianbts.get_bugs('affects', package, 'bugs', self.all_piuparts_bugs(), 'archive', 'both')
            bugs += debianbts.get_bugs('affects', 'src:' + package, 'bugs', self.all_piuparts_bugs(), 'archive', 'both')
            alarm(0)
            self._bugs_affecting_package[package] = sorted(set(bugs), reverse=True)
        self._queries += 1
        return self._bugs_affecting_package[package]

    def bug_versions(self, bug):
        """Gets a list of only the version numbers for which the bug is found.
        Newest versions are returned first."""
        # debianbts returns it in the format package/1.2.3 or 1.2.3 which will become 1.2.3
        if not bug in self._bug_versions:
            self._misses += 1
            signal(SIGALRM, alarm_handler)
            alarm(60)
            found_versions = debianbts.get_status(bug)[0].found_versions
            alarm(0)
            versions = []
            for found_version in found_versions:
                v = found_version.rsplit('/', 1)[-1]
                if v == "None":
                    # ignore $DISTRO/None versions
                    pass
                else:
                    versions.append(v)
            self._bug_versions[bug] =  list(reversed(sorted(versions, cmp=apt_pkg.version_compare))) or ['~']
        self._queries += 1
        return self._bug_versions[bug]

    def print_stats(self):
        print("PiupartsBTS: %d queries, %d forwarded to debianbts" % (self._queries, self._misses))


piupartsbts = PiupartsBTS()

############################################################################

class Busy(Exception):

    def __init__(self):
        self.args = "section is locked by another process",


class Config(piupartslib.conf.Config):
    def __init__(self, section="global", defaults_section=None):
        self.section = section
        piupartslib.conf.Config.__init__(self, section,
                                         {
                                         "sections": "report",
                                         "master-directory": ".",
                                         },
                                         defaults_section=defaults_section)


def find_logs(directory):
    """Returns list of logfiles sorted by age, newest first."""
    logs = [os.path.join(directory, x)
            for x in os.listdir(directory) if x.endswith(".log")]
    return [y[1] for y in reversed(sorted([(os.path.getmtime(x), x) for x in logs]))]


def find_bugged_logs(failed_log):
    package = package_name(failed_log)
    pat = "/" + package + "_"
    return [x for x in find_logs("bugged") + find_logs("affected") if pat in x]


def package_name(log):
    return os.path.basename(log).split("_", 1)[0]


def package_version(log):
    return os.path.basename(log).split("_", 1)[1].rstrip('.log')


def package_source_version(log):
    version = package_version(log)
    possible_binnmu_part = version.rsplit('+', 1)[-1]
    if possible_binnmu_part.startswith('b') and possible_binnmu_part[1:].isdigit():
        # the package version contains a binnmu-part which is not part of the source version
        # and therefore not accepted/tracked by the bts. Remove it.
        version = version.rsplit('+', 1)[0]
    return version


def extract_errors(log):
    """This pretty stupid implementation is basically just 'grep -i error', and then
    removing the timestamps and the name of the chroot and the package version itself."""
    with open(log, "r") as f:
        data = f.read()
    whole = ''
    pversion = package_version(log)
    for match in error_pattern.finditer(data):
        text = match.group()
        # Get rid of timestamps
        if text[:1].isdigit():
            text = text.split(" ", 1)[1]
        # Get rid of chroot names
        if 'tmp/tmp' in text:
            text = re.sub(chroot_pattern, "chroot'", text)
        # Get rid of the package version
        text = text.replace(pversion, '')
        whole += text
    return whole


def extract_headers(log):
    with open(log, "r") as f:
        data = f.read()
    headers = []
    headers = data.partition("\nExecuting:")[0]
    if headers and not headers.endswith("\n"):
        headers += "\n"
    return headers


def prepend_to_file(filename, data):
    with open(filename, "r") as f:
        old_data = f.read()
    with open(filename + ".tmp", "w") as f:
        f.write(data)
        f.write(old_data)

    shutil.copymode(filename, filename + ".tmp")

    os.rename(filename, filename + "~")
    os.rename(filename + ".tmp", filename)
    os.remove(filename + "~")


def write_bug_file(failed_log, bugs):
    if bugs:
        with open(os.path.splitext(failed_log)[0] + '.bug', "w") as f:
            for bug in bugs:
                f.write('<a href="https://bugs.debian.org/%s" target="_blank">#%s</a>\n' % (bug, bug))


def move_to_bugged(failed_log, bugged="bugged", bug=None):
    print("Moving %s to %s (#%s)" % (failed_log, bugged, bug))
    os.rename(failed_log, os.path.join(bugged, os.path.basename(failed_log)))
    if bug is not None:
        write_bug_file(os.path.join(bugged, os.path.basename(failed_log)), [bug])


def mark_bugged_version(failed_log, bugged_log):
    """Copies the headers from the old log to the new log and vice versa and
    moves the new log to bugged. Removes the old log in bugged."""
    bugged_headers = extract_headers(bugged_log)
    failed_headers = extract_headers(failed_log)
    prepend_to_file(failed_log, bugged_headers)
    prepend_to_file(bugged_log, failed_headers)
    move_to_bugged(failed_log)


def bts_update_found(bugnr, newversion):
    if "DEBEMAIL" in os.environ and os.environ["DEBEMAIL"]:
        # subprocess.check_call(('bts', 'found', bugnr, newversion))
        print(' '.join(('bts', 'found', str(bugnr), newversion)))


def mark_logs_with_reported_bugs():
    for failed_log in find_logs("fail") + find_logs("untestable"):
        try:
            pname = package_name(failed_log)
            pversion = package_source_version(failed_log)
            try:
                failed_errors = extract_errors(failed_log)
            except IOError:
                print('IOError while processing %s' % failed_log)
                continue
            moved = False
            abugs = piupartsbts.bugs_affecting(pname)
            bugs = piupartsbts.bugs_in(pname)
            for bug in abugs + bugs:
                if moved:
                    break
                if bug in abugs:
                    bugged = "affected"
                else:
                    bugged = "bugged"
                found_versions = piupartsbts.bug_versions(bug)
                if pversion in found_versions:
                    move_to_bugged(failed_log, bugged, bug)
                    moved = True
                    break
                for bug_version in found_versions:
                    # print('DEBUG: %s/%s #%d %s' % (pname, pversion, bug, bug_version))

                    if apt_pkg.version_compare(pversion, bug_version) > 0:  # pversion > bug_version
                        bugged_logs = find_bugged_logs(failed_log)
                        if not bugged_logs and not moved:
                            print('%s/%s: Maybe the bug was filed earlier: https://bugs.debian.org/%d against %s/%s'
                                  % (pname, pversion, bug, pname, bug_version))
                            break
                        for bugged_log in bugged_logs:
                            old_pversion = package_source_version(bugged_log)
                            bugged_errors = extract_errors(bugged_log)
                            if (apt_pkg.version_compare(old_pversion, bug_version) == 0  # old_pversion == bug_version
                                and
                                    failed_errors == bugged_errors):
                                # a bug was filed for an old version of the package,
                                # and the errors were the same back then - assume it is the same bug.
                                if not moved:
                                    mark_bugged_version(failed_log, bugged_log)
                                    moved = True
                                    bts_update_found(bug, pversion)
                                    break
            if not moved:
                write_bug_file(failed_log, abugs + bugs)
        except KeyboardInterrupt:
            raise
        except:
            print('ERROR processing %s' % failed_log)
            print(sys.exc_info()[0])
        alarm(0)


def main():
    conf = Config()
    conf.read(CONFIG_FILE)

    master_directory = conf["master-directory"]
    if len(sys.argv) > 1:
        sections = sys.argv[1:]
    else:
        sections = conf['sections'].split()

    with open(os.path.join(master_directory, "analyze.lock"), "we") as lock:
        try:
            fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
        except IOError:
            if sys.stdout.isatty():
                sys.exit("another piuparts-analyze process is already running")
            else:
                sys.exit(0)

        todo = deque([(s, 0) for s in sections])
        while len(todo):
            (section_name, next_try) = todo.popleft()
            now = time.time()
            if (now < next_try):
                print("Sleeping while section is busy")
                time.sleep(max(30, next_try - now) + 30)
            print(time.strftime("%a %b %2d %H:%M:%S %Z %Y", time.localtime()))
            print("%s:" % section_name)
            try:
                section_directory = os.path.join(master_directory, section_name)
                if not os.path.exists(section_directory):
                    raise MissingSection("", section_name)
                with open(os.path.join(section_directory, "master.lock"), "we") as lock:
                    try:
                        fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
                    except IOError:
                        raise Busy()

                    oldcwd = os.getcwd()
                    os.chdir(section_directory)
                    mark_logs_with_reported_bugs()
                    os.chdir(oldcwd)
            except Busy:
                print("Section is busy")
                todo.append((section_name, time.time() + 300))
            except MissingSection as e:
                print("Configuration Error in section '%s': %s" % (section_name, e))
            print("")

        print(time.strftime("%a %b %2d %H:%M:%S %Z %Y", time.localtime()))
        piupartsbts.print_stats()


if __name__ == "__main__":
    main()

# vi:set et ts=4 sw=4 :