~launchpad-pqm/launchpad/devel

13843.6.2 by Abel Deuring
script file added
1
#!/usr/bin/python -S
2
#
3
# Copyright 2009 Canonical Ltd.  This software is licensed under the
4
# GNU Affero General Public License version 3 (see the file LICENSE).
5
6
# pylint: disable-msg=W0403
7
8
"""
9
Cron job that parses pending HWDB submissions.
10
11
12
Options:
13
    -m, --max-submissions: (optional) The maximum number of submissions
14
        which will be processed.
15
16
This script iterates over the HWDB submissions with the status
13901.3.1 by Abel Deuring
transofrm reprocess-hwdb-submissions.py into a cronscript
17
INVALID. It processes only submissions with an ID greater or equal
18
than the number specified by the file given a option -s.
19
20
When the script terminates, it writes the ID of the last processed
21
submission into this file.
13843.6.2 by Abel Deuring
script file added
22
23
Properly processed submissions are set to the status PROCESSED;
13901.3.1 by Abel Deuring
transofrm reprocess-hwdb-submissions.py into a cronscript
24
submissions that cannot be processed retain the status INVALID.
13843.6.2 by Abel Deuring
script file added
25
"""
26
27
import _pythonpath
28
14612.2.8 by William Grant
cronscripts
29
from lp.hardwaredb.scripts.hwdbsubmissions import (
30
    reprocess_invalid_submissions,
31
    )
13901.3.1 by Abel Deuring
transofrm reprocess-hwdb-submissions.py into a cronscript
32
from lp.services.scripts.base import LaunchpadCronScript
13843.6.2 by Abel Deuring
script file added
33
34
13901.3.1 by Abel Deuring
transofrm reprocess-hwdb-submissions.py into a cronscript
35
class HWDBSubmissionProcessor(LaunchpadCronScript):
13843.6.2 by Abel Deuring
script file added
36
37
    def add_my_options(self):
38
        """See `LaunchpadScript`."""
39
        self.parser.add_option(
40
            '-m', '--max-submissions',
41
            help='Limit the number of submissions which will be processed.')
42
        self.parser.add_option(
43
            '-w', '--warnings', action="store_true", default=False,
44
            help='Include warnings.')
45
        self.parser.add_option(
13901.3.1 by Abel Deuring
transofrm reprocess-hwdb-submissions.py into a cronscript
46
            '-s', '--start-file', default=None,
47
            help=('The name of a file storing the smallest ID of a\n'
48
                  'hardware database submission that should be processed.\n'
49
                  'This script must have read and write access to the file.'))
13843.6.2 by Abel Deuring
script file added
50
51
    def main(self):
52
        max_submissions = self.options.max_submissions
53
        if max_submissions is not None:
54
            try:
55
                max_submissions = int(self.options.max_submissions)
56
            except ValueError:
57
                self.logger.error(
58
                    'Invalid value for --max_submissions specified: %r.'
59
                    % max_submissions)
60
                return
61
            if max_submissions <= 0:
62
                self.logger.error(
63
                    '--max_submissions must be a positive integer.')
64
                return
13901.3.1 by Abel Deuring
transofrm reprocess-hwdb-submissions.py into a cronscript
65
66
        if self.options.start_file is None:
67
            self.logger.error('Option --start-file not specified.')
68
            return
69
        try:
70
            start_file = open(self.options.start_file, 'r+')
71
            start_id = start_file.read().strip()
72
        except IOError, error:
73
            self.logger.error(
74
                'Cannot access file %s: %s' % (
75
                    self.options.start_file, error))
76
            return
77
        try:
78
            start_id = int(start_id)
13843.6.2 by Abel Deuring
script file added
79
        except ValueError:
13901.3.1 by Abel Deuring
transofrm reprocess-hwdb-submissions.py into a cronscript
80
            self.logger.error(
81
                '%s must contain only an integer' % self.options.start_file)
13843.6.6 by Henning Eggers
Fix options.warnings
82
            return
13901.3.1 by Abel Deuring
transofrm reprocess-hwdb-submissions.py into a cronscript
83
        if start_id < 0:
84
            self.logger.error(
85
                '%s must contain a positive integer'
86
                % self.options.start_file)
13843.6.6 by Henning Eggers
Fix options.warnings
87
            return
13843.6.2 by Abel Deuring
script file added
88
13901.3.1 by Abel Deuring
transofrm reprocess-hwdb-submissions.py into a cronscript
89
        next_start = reprocess_invalid_submissions(
90
            start_id, self.txn, self.logger,
13843.6.6 by Henning Eggers
Fix options.warnings
91
            max_submissions, self.options.warnings)
13843.6.2 by Abel Deuring
script file added
92
13901.3.1 by Abel Deuring
transofrm reprocess-hwdb-submissions.py into a cronscript
93
        start_file.seek(0)
94
        start_file.write('%i' % next_start)
95
        start_file.close()
96
13843.6.2 by Abel Deuring
script file added
97
if __name__ == '__main__':
98
    script = HWDBSubmissionProcessor(
99
        'hwdbsubmissions', dbuser='hwdb-submission-processor')
100
    script.lock_and_run()