~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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/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).

"""Initialise a new distroseries from its parent series."""

from optparse import OptionParser
import sys

import _pythonpath
from contrib.glock import GlobalLock
from zope.component import getUtility

from canonical.config import config
from canonical.launchpad.scripts import (
    execute_zcml_for_scripts,
    logger,
    logger_options,
    )
from canonical.lp import initZopeless
from lp.app.errors import NotFoundError
from lp.registry.interfaces.distribution import IDistributionSet
from lp.soyuz.scripts.initialise_distroseries import (
    InitialisationError,
    InitialiseDistroSeries,
    )


def main():
    # Parse command-line arguments
    parser = OptionParser()
    logger_options(parser)

    parser.add_option("-N", "--dry-run", action="store_true",
                      dest="dryrun", metavar="DRY_RUN", default=False,
                      help="Whether to treat this as a dry-run or not.")

    parser.add_option("-d", "--distro", dest="distribution", metavar="DISTRO",
                      default="ubuntu",
                      help="Distribution name")

    parser.add_option(
        "-a", "--arches", dest="arches",
        help="A comma-seperated list of arches to limit the child "
        "distroseries to inheriting")

    (options, args) = parser.parse_args()

    log = logger(options, "initialise")

    if len(args) != 1:
        log.error("Need to be given exactly one non-option argument. "
                  "Namely the distroseries to initialise.")
        return 1

    distroseries_name = args[0]

    log.debug("Acquiring lock")
    lock = GlobalLock('/var/lock/launchpad-initialise.lock')
    lock.acquire(blocking=True)

    log.debug("Initialising connection.")

    execute_zcml_for_scripts()
    ztm = initZopeless(dbuser=config.archivepublisher.dbuser)

    try:
        # 'ubuntu' is the default option.distribution value
        distribution = getUtility(IDistributionSet)[options.distribution]
        distroseries = distribution[distroseries_name]
    except NotFoundError, info:
        log.error('%s not found' % info)
        return 1

    try:
        log.debug('Check empty mutable queues in parentseries')
        log.debug('Check for no pending builds in parentseries')
        log.debug('Copying distroarchseries from parent '
                      'and setting nominatedarchindep.')
        arches = ()
        if options.arches is not None:
            arches = tuple(options.arches.split(','))
        # InitialiseDistroSeries does not like it if the parent series is
        # specified on the child, so we must unset it and pass it in. This is
        # a temporary hack until confidence in InitialiseDistroSeriesJob is
        # good, at which point this script will be obsolete.
        parent, distroseries.parent_series = distroseries.parent_series, None
        ids = InitialiseDistroSeries(parent, distroseries, arches)
        ids.check()
        log.debug('initialising from parent, copying publishing records.')
        ids.initialise()
    except InitialisationError, e:
        log.error(e)
        return 1

    if options.dryrun:
        log.debug('Dry-Run mode, transaction aborted.')
        ztm.abort()
    else:
        log.debug('Committing transaction.')
        ztm.commit()

    log.debug("Releasing lock")
    lock.release()
    return 0


if __name__ == '__main__':
    sys.exit(main())