diffclean.py 3.36 KB
Newer Older
Jonny Lamb's avatar
Jonny Lamb committed
1
2
3
4
# -*- coding: utf-8 -*-
#
#   diffclean.py — diffclean 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>
Jonny Lamb's avatar
Jonny Lamb committed
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
#
#   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 diffclean plugin.
"""

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

42
import subprocess
Jonny Lamb's avatar
Jonny Lamb committed
43
44
45
import logging

from debexpo.lib import constants
46
from debexpo.plugins.api import *
Jonny Lamb's avatar
Jonny Lamb committed
47
48
49

log = logging.getLogger(__name__)

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

@test_result
class DiffCleanTest(PluginResult):
    """ Result of the diffclean QA test """
    dirty = bool_field('dirty')
    diff_file = bool_field('diff_file')
    severity = int_field('severity')

    def __str__(self):
        outcome = 'Diff file  is {} clean'.format('not' if self.dirty else '')
        return outcome


class DiffFile(PluginResult):
    filename = string_field('filename')
    stats = string_field('stats')


Jonny Lamb's avatar
Jonny Lamb committed
68
69
class DiffCleanPlugin(BasePlugin):

70
    @importercmd
Jonny Lamb's avatar
Jonny Lamb committed
71
72
73
74
75
76
77
78
    def test_diff_clean(self):
        """
        Check to make sure the diff.gz is clean.
        """
        log.debug('Checking to make sure the diff.gz is clean')

        difffile = self.changes.get_diff()

79
        if difffile is None or not difffile.endswith('.diff.gz'):
80
81
            log.warning('Package has no diff.gz file;'
                        'native or format 3.0 package?')
Jonny Lamb's avatar
Jonny Lamb committed
82
83
            return

84
85
        diffstat = subprocess.Popen(["diffstat", "-p1", difffile],
                                    stdout=subprocess.PIPE).communicate()[0]
Jonny Lamb's avatar
Jonny Lamb committed
86

87
        test_result = self.new_test_result()
Jonny Lamb's avatar
Jonny Lamb committed
88

89
90
91
92
        # Last line is the summary line
        for item in diffstat.splitlines()[:-1]:
            filename, stats = [i.strip() for i in item.split("|")]
            if not filename.startswith('debian/'):
93
94
                test_resut.dirty = True
                self.new_result(DiffFile, filename=filename, stats=stats)
95

96
        if not test_result.dirty:
Jonny Lamb's avatar
Jonny Lamb committed
97
98
99
            log.debug('Diff file %s is clean' % difffile)
        else:
            log.error('Diff file %s is not clean' % difffile)
100
            test_result.severity = constants.PLUGIN_SEVERITY_WARNING
Jonny Lamb's avatar
Jonny Lamb committed
101
102

plugin = DiffCleanPlugin
103
104
105
106
models = [
    DiffCleanTest,
    DiffFile,
    ]