lintian.py 6.42 KB
Newer Older
Jonny Lamb's avatar
Jonny Lamb committed
1
2
3
4
# -*- coding: utf-8 -*-
#
#   lintian.py — lintian plugin
#
Arno Töll's avatar
Arno Töll committed
5
#   This file is part of debexpo - https://alioth.debian.org/projects/debexpo/
Jonny Lamb's avatar
Jonny Lamb committed
6
#
Jonny Lamb's avatar
Jonny Lamb committed
7
#   Copyright © 2008 Jonny Lamb <jonny@debian.org>
8
#   Copyright © 2012 Nicolas Dandrimont <Nicolas.Dandrimont@crans.org>
9
#   Copyright © 2012 Clément Schreiner <clement@mux.me>
Jonny Lamb's avatar
Jonny Lamb committed
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
#
#   Permission is hereby granted, free of charge, to any person
#   obtaining a copy of this software and associated documentation
#   files (the "Software"), to deal in the Software without
#   restriction, including without limitation the rights to use,
#   copy, modify, merge, publish, distribute, sublicense, and/or sell
#   copies of the Software, and to permit persons to whom the
#   Software is furnished to do so, subject to the following
#   conditions:
#
#   The above copyright notice and this permission notice shall be
#   included in all copies or substantial portions of the Software.
#
#   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
#   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
#   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
#   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
#   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
#   OTHER DEALINGS IN THE SOFTWARE.

"""
Holds the lintian plugin.
"""

__author__ = 'Jonny Lamb'
37
38
39
__copyright__ = ', '.join([
        'Copyright © 2008 Jonny Lamb',
        'Copyright © 2012 Nicolas Dandrimont',
40
        'Copyright © 2012 Clément Schreiner',
41
        ])
Jonny Lamb's avatar
Jonny Lamb committed
42
43
__license__ = 'MIT'

44

45
import subprocess
Jonny Lamb's avatar
Jonny Lamb committed
46
import logging
47
from collections import defaultdict, namedtuple
Jonny Lamb's avatar
Jonny Lamb committed
48
49

from debexpo.lib import constants
50
51
from debexpo.plugins.api import *
from debexpo.model import meta
Jonny Lamb's avatar
Jonny Lamb committed
52
53
54

log = logging.getLogger(__name__)

55
LintianSeverity = namedtuple('LintianSeverity',
56
57
58
59
60
61
62
63
64
65
66
67
68
69
                             ['str', 'int', 'plugin_severity'])
severities = {
    'E': LintianSeverity('Package has lintian errors', 5,
                         constants.PLUGIN_SEVERITY_ERROR),
    'W': LintianSeverity('Package has lintian warnings', 4,
                         constants.PLUGIN_SEVERITY_WARNING),
    'I': LintianSeverity('Package has lintian informational warnings', 3,
                         constants.PLUGIN_SEVERITY_INFO),
    'O': LintianSeverity('Package has overridden lintian tags', 2,
                         constants.PLUGIN_SEVERITY_INFO),
    'P': LintianSeverity('Package has lintian pedantic tags', 1,
                         constants.PLUGIN_SEVERITY_INFO),
    'X': LintianSeverity('Package has lintian experimental tags', 0,
                         constants.PLUGIN_SEVERITY_INFO),
70
71
    '': LintianSeverity('Package is lintian clean', -1,
                        constants.PLUGIN_SEVERITY_INFO),
72
73
    }

74

75
76
77
78
@test_result
class LintianTest(PluginResult):
    """ Summary of the lintian results """

79
    # -1 -> package is clean
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
    max_lintian_severity = string_field('max_lintian_severity')
    severity = int_field('severity')

    def get_tags(self):
        """
        Returns the LintianTag objects for this package, as a
        dictionary.
        """

        # Yes, three levels of defaultdict and one of list...
        # FIXME: how exactly is this new API better? :/
        def defaultdict_defaultdict_list():
            def defaultdict_list():
                return defaultdict(list)
            return defaultdict(defaultdict_list)
        lintian_warnings = defaultdict(defaultdict_defaultdict_list)

        q = meta.session.query(LintianWarning)
        q = q.filter_by(package_version_id=self.package_version.id)
        for w in q.all():
            lintian_warnings[w.package][w.severity][w.tag].append(w.data)
        return lintian_warnings

    def __str__(self):
        return severities[self.max_lintian_severity].str


class LintianWarning(PluginResult):
    """ A lintian warning found for the package """
Jonny Lamb's avatar
Jonny Lamb committed
109

110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
    package = string_field('package')
    severity = string_field('severity')
    tag = string_field('tag')
    data = string_field('data')

    def __str__(self):
        return 'Lintian warning ({severity}): [{tag}] {data}'.format(
            severity=self.severity,
            tag=self.tag,
            data=self.data)


class LintianPlugin(QAPlugin):
    """ Runs lintian tests on the package's binaries """

    @importercmd
126
    def test_lintian(self):
Jonny Lamb's avatar
Jonny Lamb committed
127
128
129
130
131
        """
        Method to run lintian on the package.
        """
        log.debug('Running lintian on the package')

132
133
134
135
136
        output = subprocess.Popen(["lintian",
                                   "-E",
                                   "-I",
                                   "--pedantic",
                                   "--show-overrides",
137
138
                                   self.changes_file],
                                   stdout=subprocess.PIPE).communicate()[0]
Jonny Lamb's avatar
Jonny Lamb committed
139
140
141

        items = output.split('\n')

142
143
144
145
146
147
148
149
        lintian_severities = set()

        override_comments = []

        for item in items:
            if not item:
                continue

150
151
152
            # lintian output is of the form:
            # " SEVERITY: package: lintian_tag [lintian tag arguments] "
            # or " N: Override comment "
153
154
155
156
157
158
159
160
161
            if item.startswith("N: "):
                override_comments.append(item[3:].strip())
                continue
            severity, package, rest = item.split(': ', 2)
            lintian_severities.add(severity)
            lintian_tag_data = rest.split()
            lintian_tag = lintian_tag_data[0]
            lintian_data = lintian_tag_data[1:]
            if override_comments:
162
163
                lintian_data.append("(override comment: "
                                    + " ".join(override_comments) + ")")
164
                override_comments = []
165
            for line in lintian_data:
166
167
168
169
170
171
                self.new_result(LintianWarning, package=package,
                                severity=severity, tag=lintian_tag,
                                data=line)

        if not lintian_severities:
            max_lintian_severity = ''
172

173
174
175
        else:
            max_lintian_severity = max(lintian_severities,
                                       key=lambda s: severities[s].int)
176
177
178
179
        severity = severities[max_lintian_severity].plugin_severity

        self.new_test_result(severity=severity,
                             max_lintian_severity=max_lintian_severity)
180

181
plugin = LintianPlugin
182
183
184
185
models = [
    LintianTest,
    LintianWarning,
    ]