~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/python -S
#
# Copyright 2009 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

# pylint: disable-msg=W0403

"""
Cron job that parses pending HWDB submissions.


Options:
    -m, --max-submissions: (optional) The maximum number of submissions
        which will be processed.

This script iterates over the HWDB submissions with the status
SUBMITTED, beginning with the oldest submissions, populate the
HWDB tables with the data from these submissions.

Properly processed submissions are set to the status PROCESSED;
submissions that cannot be processed are set to the status INVALID.
"""

import _pythonpath

from lp.services.scripts.base import LaunchpadScript
from lp.hardwaredb.scripts.hwdbsubmissions import (
    reprocess_invalid_submissions)


class HWDBSubmissionProcessor(LaunchpadScript):

    def add_my_options(self):
        """See `LaunchpadScript`."""
        self.parser.add_option(
            '-m', '--max-submissions',
            help='Limit the number of submissions which will be processed.')
        self.parser.add_option(
            '-w', '--warnings', action="store_true", default=False,
            help='Include warnings.')
        self.parser.add_option(
            '-s', '--start',
            help=('Process HWSubmission records having an id greater or '
                  'equal than this value.'))

    def main(self):
        max_submissions = self.options.max_submissions
        if max_submissions is not None:
            try:
                max_submissions = int(self.options.max_submissions)
            except ValueError:
                self.logger.error(
                    'Invalid value for --max_submissions specified: %r.'
                    % max_submissions)
                return
            if max_submissions <= 0:
                self.logger.error(
                    '--max_submissions must be a positive integer.')
                return
        if self.options.start is None:
            self.logger.error('Option --start not specified.')
            return
        try:
            start = int(self.options.start)
        except ValueError:
            self.logger.error('Option --start must have an integer value.')
            return
        if start < 0:
            self.logger.error('--start must be a positive integer.')
            return

        reprocess_invalid_submissions(
            start, self.txn, self.logger,
            max_submissions, self.options.warnings)

if __name__ == '__main__':
    script = HWDBSubmissionProcessor(
        'hwdbsubmissions', dbuser='hwdb-submission-processor')
    script.lock_and_run()