watchfile.py 4.28 KB
Newer Older
Jonny Lamb's avatar
Jonny Lamb committed
1
2
3
4
# -*- coding: utf-8 -*-
#
#   watchfile.py — watchfile 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 watchfile 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
46
import logging
import os

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

log = logging.getLogger(__name__)

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

@test_result
class WatchfileTest(PluginResult):
    """ Result of the watchfile QA test """
    watch_file_present = bool_field('watch_file_present')
    watch_file_works = bool_field('watch_file_workds')
    uscan_output = string_field('uscan_output')
    latest_upstream = string_field('latest_upstream')

    def __str__(self):
        if not self.watch_file_present:
            outcome = 'Watch file is not present'

        elif not self.watch_file_works:
            outcome = "A watch file is present but doesn't work"

        else:
            outcome = 'Package is {} the latest upstream version'.format(
                '' if self.latest_upstream else 'not')

        return outcome


class WatchFilePlugin(QAPlugin):
Jonny Lamb's avatar
Jonny Lamb committed
75
76
77
78
79
80
81

    def _watch_file_present(self):
        return os.path.isfile(os.path.join('extracted', 'debian', 'watch'))

    def _run_uscan(self):
        if not hasattr(self, 'status') and not hasattr(self, 'output'):
            os.chdir('extracted')
82
83
            call = subprocess.Popen(["uscan", "--verbose", '--report'],
                                    stdout=subprocess.PIPE)
84
85
            (self.output, _) = call.communicate()
            self.status = call.returncode
Jonny Lamb's avatar
Jonny Lamb committed
86
87
88
89
90
91
            os.chdir('..')

    def _watch_file_works(self):
        self._run_uscan()
        return (self.output.find('Newest version on remote site is') != -1)

92
    @importercmd
93
    def test_uscan(self):
Jonny Lamb's avatar
Jonny Lamb committed
94
        """
95
        Run the watch file-related checks in the package
Jonny Lamb's avatar
Jonny Lamb committed
96
        """
97

98
99
100
        log.debug('Checking to see whether there is'
                  'a watch file in the package')
        test_result = self.new_test_result()
Jonny Lamb's avatar
Jonny Lamb committed
101
102
103

        if self._watch_file_present():
            log.debug('Watch file present')
104
            test_result.watch_file_present = True
Jonny Lamb's avatar
Jonny Lamb committed
105
106
        else:
            log.warning('Watch file not present')
107
            test_result.watch_file_present = False
108
            return
Jonny Lamb's avatar
Jonny Lamb committed
109

110
111
112
113
114
        test_result.watch_file_works = self._watch_file_works()
        test_result.uscan_output = self.output
        if not test_result.watch_file_works:
            log.debug('Watch file does not work')
            test_result.severity = constants.PLUGIN_SEVERITY_WARNING
115
            return
Jonny Lamb's avatar
Jonny Lamb committed
116
117
118

        log.debug('Looking whether there is a new upstream version')

119
120
        test_result.latest_upstream = self.status == 1
        if test_result.latest_upstream:
Jonny Lamb's avatar
Jonny Lamb committed
121
122
123
            log.debug('Package is the latest upstream version')
        else:
            log.warning('Package is not the latest upstream version')
124
125
            test_result.severity = constants.PLUGIN_SEVERITY_WARNING
        return
Jonny Lamb's avatar
Jonny Lamb committed
126

127

128
129
plugin = WatchFilePlugin
models = [WatchfileTest]