~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to cronscripts/create-debwatches.py

  • Committer: James Henstridge
  • Date: 2007-03-15 02:27:17 UTC
  • mfrom: (3948 launchpad)
  • mto: This revision was merged to the branch mainline in revision 4227.
  • Revision ID: james.henstridge@canonical.com-20070315022717-02kmowmcm2amk5c4
merge from rocketfuel

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
 
1
#!/usr/bin/python2.4
2
2
 
3
3
# Copyright 2005 Canonical Ltd.  All rights reserved.
4
4
 
9
9
__metaclass__ = type
10
10
 
11
11
import os
12
 
import sys
13
12
import logging
14
13
import _pythonpath
15
 
from optparse import OptionParser
16
14
 
17
15
# zope bits
18
16
from zope.component import getUtility
19
17
 
20
18
# canonical launchpad modules
21
 
from canonical.lp import initZopeless
22
 
from canonical.launchpad.scripts import (execute_zcml_for_scripts,
23
 
    logger_options, logger as logger_from_options)
24
 
from canonical.launchpad.scripts.lockfile import LockFile
25
19
from canonical.launchpad.scripts.debsync import (
26
 
    bug_filter, do_import, import_bug)
 
20
    do_import)
 
21
from canonical.launchpad.scripts.base import (
 
22
    LaunchpadScript, LaunchpadScriptFailure)
27
23
from canonical.launchpad.interfaces import ILaunchpadCelebrities
28
24
 
29
25
 
35
31
MIN_AGE = 7
36
32
 
37
33
 
38
 
def main(args):
39
 
    parser = OptionParser(description=("This script syncs debbugs from "
40
 
        "http://bugs.debian.org/ into Malone. It selects interesting "
41
 
        "bugs in debian and makes sure that there is a Malone bug for "
42
 
        "each of them. See debwatchsync for a tool that syncronises "
43
 
        "the bugs in Malone and debbugs, too."))
44
 
    logger_options(parser, logging.WARNING)
45
 
    parser.set_defaults(max=None, debbugs=debbugs_location_default)
46
 
    parser.add_option('--debbugs', action='store', type='string',
47
 
        dest='debbugs',
48
 
        help="The location of your debbugs database.")
49
 
    parser.add_option('--max', action='store', type='int', dest='max',
50
 
        help="The maximum number of bugs to create.")
51
 
    parser.add_option('--package', action='append', type='string',
52
 
        help="A list of packages for which we should import bugs.",
53
 
        dest="packages", default=[])
54
 
    options, args = parser.parse_args()
55
 
 
56
 
    # setup the logger
57
 
    logger = logger_from_options(options)
58
 
 
59
 
    # make sure the debbugs location looks sane
60
 
    if not os.path.exists(os.path.join(options.debbugs, 'index/index.db')):
61
 
        logger.error('%s is not a debbugs db.' % options.debbugs)
62
 
        return 1
63
 
 
64
 
    # Make sure we import any Debian bugs specified on the command line
65
 
    for arg in args:
66
 
        try:
67
 
            target_bug = int(arg)
68
 
        except ValueError:
69
 
            logger.error('%s is not a valid debian bug number.' % arg)
70
 
        target_bugs.add(target_bug)
71
 
 
72
 
    logger.info('Setting up utilities...')
73
 
    execute_zcml_for_scripts()
74
 
 
75
 
    target_bugs = set()
76
 
    target_package_set = set()
77
 
    previousimportset = set()
78
 
 
79
 
    logger.info('Connecting to database...')
80
 
    ztm = initZopeless(implicitBegin=False)
81
 
    ztm.begin()
82
 
 
83
 
    logger.info('Calculating target package set...')
84
 
 
85
 
    # first find all the published ubuntu packages
86
 
    ubuntu = getUtility(ILaunchpadCelebrities).ubuntu
87
 
    for p in ubuntu.currentrelease.publishedBinaryPackages(
88
 
        component='main'):
89
 
        target_package_set.add(p.binarypackagename.name)
90
 
    # then add packages passed on the command line
91
 
    for package in options.packages:
92
 
        target_package_set.add(package)
93
 
    logger.info('%d binary packages targeted.' % len(target_package_set))
94
 
 
95
 
    lockfile_path = '/var/lock/launchpad-debbugs-mkwatch.lock'
96
 
    lockfile = LockFile(lockfile_path)
97
 
    try:
98
 
        lockfile.acquire()
99
 
    except OSError:
100
 
        logger.info('Lockfile %s already exists, exiting.' % lockfile_path)
101
 
        return 0
102
 
 
103
 
    ztm.abort()
104
 
    try:
105
 
        ztm.begin()
106
 
        do_import(logger, options.max, options.debbugs, target_bugs,
107
 
            target_package_set, previousimportset, MIN_AGE, debbugs_pl)
108
 
        ztm.commit()
109
 
    except:
110
 
        logger.exception('Uncaught exception!')
111
 
        lockfile.release()
112
 
        return 1
113
 
 
114
 
    logger.info('Done!')
115
 
    lockfile.release()
116
 
    return 0
 
34
class CreateDebWatches(LaunchpadScript):
 
35
    description = """
 
36
    This script syncs debbugs from http://bugs.debian.org/ into Malone.
 
37
    It selects interesting bugs in debian and makes sure that there is a
 
38
    Malone bug for each of them. See debwatchsync for a tool that
 
39
    syncronises the bugs in Malone and debbugs, too.
 
40
    """
 
41
    loglevel = logging.WARNING
 
42
    def add_my_options(self):
 
43
        self.parser.set_defaults(max=None, debbugs=debbugs_location_default)
 
44
        self.parser.add_option('--debbugs', action='store', type='string',
 
45
            dest='debbugs',
 
46
            help="The location of your debbugs database.")
 
47
        self.parser.add_option('--max', action='store', type='int', dest='max',
 
48
            help="The maximum number of bugs to create.")
 
49
        self.parser.add_option('--package', action='append', type='string',
 
50
            help="A list of packages for which we should import bugs.",
 
51
            dest="packages", default=[])
 
52
 
 
53
    def main(self):
 
54
        if not os.path.exists(os.path.join(self.options.debbugs, 'index/index.db')):
 
55
            # make sure the debbugs location looks sane
 
56
            raise LaunchpadScriptFailure('%s is not a debbugs db.'
 
57
                                         % self.options.debbugs)
 
58
 
 
59
        # Make sure we import any Debian bugs specified on the command line
 
60
        target_bugs = set()
 
61
        for arg in self.args:
 
62
            try:
 
63
                target_bug = int(arg)
 
64
            except ValueError:
 
65
                self.logger.error('%s is not a valid debian bug number.' % arg)
 
66
            target_bugs.add(target_bug)
 
67
 
 
68
        target_package_set = set()
 
69
        previousimportset = set()
 
70
 
 
71
        self.logger.info('Calculating target package set...')
 
72
 
 
73
        # first find all the published ubuntu packages
 
74
        ubuntu = getUtility(ILaunchpadCelebrities).ubuntu
 
75
        for p in ubuntu.currentrelease.publishedBinaryPackages(
 
76
            component='main'):
 
77
            target_package_set.add(p.binarypackagename.name)
 
78
        # then add packages passed on the command line
 
79
        for package in self.options.packages:
 
80
            target_package_set.add(package)
 
81
        self.logger.info('%d binary packages targeted.' % len(target_package_set))
 
82
 
 
83
        self.txn.abort()
 
84
        self.txn.begin()
 
85
        do_import(self.logger, self.options.max, self.options.debbugs,
 
86
            target_bugs, target_package_set, previousimportset, MIN_AGE,
 
87
            debbugs_pl)
 
88
        self.txn.commit()
 
89
 
 
90
        self.logger.info('Done!')
117
91
 
118
92
if __name__ == '__main__':
119
 
    sys.exit(main(sys.argv))
 
93
    script = CreateDebWatches("debbugs-mkwatch")
 
94
    script.lock_and_run()
120
95