~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
#!/usr/bin/python -S
#
# Copyright 2009-2011 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

# pylint: disable-msg=C0103,W0403

from optparse import OptionParser

import _pythonpath
from twisted.internet import (
    defer,
    reactor,
    )
from twisted.python import log as tplog

from lp.services.config import config
from lp.codehosting.puller import (
    mirror,
    scheduler,
    )
from lp.services.scripts import logger_options
from lp.services.twistedsupport.loggingsupport import (
    LoggingProxy,
    set_up_logging_for_script,
    )


def clean_shutdown(ignored):
    reactor.stop()


def shutdown_with_errors(failure):
    tplog.err(failure)
    failure.printTraceback()
    reactor.stop()


def run_mirror(log, manager):
    # It's conceivable that mirror() might raise an exception before it
    # returns a Deferred -- maybeDeferred means we don't have to worry.
    deferred = defer.maybeDeferred(mirror, log, manager)
    deferred.addCallback(clean_shutdown)
    deferred.addErrback(shutdown_with_errors)


if __name__ == '__main__':
    parser = OptionParser()
    logger_options(parser)
    parser.add_option('--branch-type', action='append', default=[])
    (options, arguments) = parser.parse_args()
    if arguments:
        parser.error("Unhandled arguments %s" % repr(arguments))
    log = set_up_logging_for_script(
        options, 'supermirror_puller', options.log_file)
    manager = scheduler.JobScheduler(
        LoggingProxy(config.codehosting.codehosting_endpoint, log), log,
        options.branch_type)

    reactor.callWhenRunning(run_mirror, log, manager)
    reactor.run()