~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
13901.3.1 by Abel Deuring
transofrm reprocess-hwdb-submissions.py into a cronscript
29
from lp.services.scripts.base import LaunchpadCronScript
13843.6.2 by Abel Deuring
script file added
30
from lp.hardwaredb.scripts.hwdbsubmissions import (
31
    reprocess_invalid_submissions)
32
33
13901.3.1 by Abel Deuring
transofrm reprocess-hwdb-submissions.py into a cronscript
34
class HWDBSubmissionProcessor(LaunchpadCronScript):
13843.6.2 by Abel Deuring
script file added
35
36
    def add_my_options(self):
37
        """See `LaunchpadScript`."""
38
        self.parser.add_option(
39
            '-m', '--max-submissions',
40
            help='Limit the number of submissions which will be processed.')
41
        self.parser.add_option(
42
            '-w', '--warnings', action="store_true", default=False,
43
            help='Include warnings.')
44
        self.parser.add_option(
13901.3.1 by Abel Deuring
transofrm reprocess-hwdb-submissions.py into a cronscript
45
            '-s', '--start-file', default=None,
46
            help=('The name of a file storing the smallest ID of a\n'
47
                  'hardware database submission that should be processed.\n'
48
                  'This script must have read and write access to the file.'))
13843.6.2 by Abel Deuring
script file added
49
50
    def main(self):
51
        max_submissions = self.options.max_submissions
52
        if max_submissions is not None:
53
            try:
54
                max_submissions = int(self.options.max_submissions)
55
            except ValueError:
56
                self.logger.error(
57
                    'Invalid value for --max_submissions specified: %r.'
58
                    % max_submissions)
59
                return
60
            if max_submissions <= 0:
61
                self.logger.error(
62
                    '--max_submissions must be a positive integer.')
63
                return
13901.3.1 by Abel Deuring
transofrm reprocess-hwdb-submissions.py into a cronscript
64
65
        if self.options.start_file is None:
66
            self.logger.error('Option --start-file not specified.')
67
            return
68
        try:
69
            start_file = open(self.options.start_file, 'r+')
70
            start_id = start_file.read().strip()
71
        except IOError, error:
72
            self.logger.error(
73
                'Cannot access file %s: %s' % (
74
                    self.options.start_file, error))
75
            return
76
        try:
77
            start_id = int(start_id)
13843.6.2 by Abel Deuring
script file added
78
        except ValueError:
13901.3.1 by Abel Deuring
transofrm reprocess-hwdb-submissions.py into a cronscript
79
            self.logger.error(
80
                '%s must contain only an integer' % self.options.start_file)
13843.6.6 by Henning Eggers
Fix options.warnings
81
            return
13901.3.1 by Abel Deuring
transofrm reprocess-hwdb-submissions.py into a cronscript
82
        if start_id < 0:
83
            self.logger.error(
84
                '%s must contain a positive integer'
85
                % self.options.start_file)
13843.6.6 by Henning Eggers
Fix options.warnings
86
            return
13843.6.2 by Abel Deuring
script file added
87
13901.3.1 by Abel Deuring
transofrm reprocess-hwdb-submissions.py into a cronscript
88
        next_start = reprocess_invalid_submissions(
89
            start_id, self.txn, self.logger,
13843.6.6 by Henning Eggers
Fix options.warnings
90
            max_submissions, self.options.warnings)
13843.6.2 by Abel Deuring
script file added
91
13901.3.1 by Abel Deuring
transofrm reprocess-hwdb-submissions.py into a cronscript
92
        start_file.seek(0)
93
        start_file.write('%i' % next_start)
94
        start_file.close()
95
13843.6.2 by Abel Deuring
script file added
96
if __name__ == '__main__':
97
    script = HWDBSubmissionProcessor(
98
        'hwdbsubmissions', dbuser='hwdb-submission-processor')
99
    script.lock_and_run()