~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to scripts/ftpmaster-tools/initialize-from-parent.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2012-01-06 23:29:30 UTC
  • mfrom: (14646.3.2 unassign-bugs-0)
  • Revision ID: launchpad@pqm.canonical.com-20120106232930-0t6v7pj0nn2k5e89
[r=bac][bug=910876] Permit logged in users to unassign bugs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 
6
6
"""Initialize a new distroseries from its parent series."""
7
7
 
8
 
from optparse import OptionParser
9
 
import sys
10
 
 
11
8
import _pythonpath
12
 
from contrib.glock import GlobalLock
 
9
 
 
10
import transaction
13
11
from zope.component import getUtility
14
12
 
15
 
from canonical.config import config
16
 
from canonical.launchpad.scripts import (
17
 
    execute_zcml_for_scripts,
18
 
    logger,
19
 
    logger_options,
20
 
    )
21
 
from canonical.lp import initZopeless
22
13
from lp.app.errors import NotFoundError
23
14
from lp.registry.interfaces.distribution import IDistributionSet
 
15
from lp.services.config import config
 
16
from lp.services.scripts.base import (
 
17
    LaunchpadScript,
 
18
    LaunchpadScriptFailure,
 
19
    )
24
20
from lp.soyuz.scripts.initialize_distroseries import (
25
21
    InitializationError,
26
22
    InitializeDistroSeries,
27
23
    )
28
24
 
29
25
 
30
 
def main():
31
 
    # Parse command-line arguments
32
 
    parser = OptionParser()
33
 
    logger_options(parser)
34
 
 
35
 
    parser.add_option("-N", "--dry-run", action="store_true",
36
 
                      dest="dryrun", metavar="DRY_RUN", default=False,
37
 
                      help="Whether to treat this as a dry-run or not.")
38
 
 
39
 
    parser.add_option("-d", "--distro", dest="distribution", metavar="DISTRO",
40
 
                      default="ubuntu",
41
 
                      help="Distribution name")
42
 
 
43
 
    parser.add_option(
44
 
        "-a", "--arches", dest="arches",
45
 
        help="A comma-seperated list of arches to limit the child "
46
 
        "distroseries to inheriting")
47
 
 
48
 
    (options, args) = parser.parse_args()
49
 
 
50
 
    log = logger(options, "initialize")
51
 
 
52
 
    if len(args) != 1:
53
 
        log.error("Need to be given exactly one non-option argument. "
54
 
                  "Namely the distroseries to initialize.")
55
 
        return 1
56
 
 
57
 
    distroseries_name = args[0]
58
 
 
59
 
    log.debug("Acquiring lock")
60
 
    lock = GlobalLock('/var/lock/launchpad-initialize.lock')
61
 
    lock.acquire(blocking=True)
62
 
 
63
 
    log.debug("Initializing connection.")
64
 
 
65
 
    execute_zcml_for_scripts()
66
 
    ztm = initZopeless(dbuser=config.archivepublisher.dbuser)
67
 
 
68
 
    try:
69
 
        # 'ubuntu' is the default option.distribution value
70
 
        distribution = getUtility(IDistributionSet)[options.distribution]
71
 
        distroseries = distribution[distroseries_name]
72
 
    except NotFoundError, info:
73
 
        log.error('%s not found' % info)
74
 
        return 1
75
 
 
76
 
    try:
77
 
        log.debug('Check empty mutable queues in parentseries')
78
 
        log.debug('Check for no pending builds in parentseries')
79
 
        log.debug('Copying distroarchseries from parent(s) '
80
 
                      'and setting nominatedarchindep.')
81
 
        arches = ()
82
 
        if options.arches is not None:
83
 
            arches = tuple(options.arches.split(','))
84
 
        # InitializeDistroSeries does not like it if the parent series is
85
 
        # specified on the child, so we must unset it and pass it in. This is
86
 
        # a temporary hack until confidence in InitializeDistroSeriesJob is
87
 
        # good, at which point this script will be obsolete.
88
 
        parent, distroseries.previous_series = (
89
 
            distroseries.previous_series, None)
90
 
        ids = InitializeDistroSeries(distroseries, [parent.id], arches)
91
 
        ids.check()
92
 
        log.debug('initializing from parent(s), copying publishing records.')
93
 
        ids.initialize()
94
 
    except InitializationError, e:
95
 
        log.error(e)
96
 
        return 1
97
 
 
98
 
    if options.dryrun:
99
 
        log.debug('Dry-Run mode, transaction aborted.')
100
 
        ztm.abort()
101
 
    else:
102
 
        log.debug('Committing transaction.')
103
 
        ztm.commit()
104
 
 
105
 
    log.debug("Releasing lock")
106
 
    lock.release()
107
 
    return 0
 
26
class InitializeFromParentScript(LaunchpadScript):
 
27
 
 
28
    usage = "Usage: %prog [options] <SERIES>"
 
29
 
 
30
    def add_my_options(self):
 
31
        self.parser.add_option(
 
32
            "-N", "--dry-run", action="store_true",
 
33
            dest="dryrun", metavar="DRY_RUN", default=False,
 
34
            help="Whether to treat this as a dry-run or not.")
 
35
        self.parser.add_option(
 
36
            "-d", "--distro", dest="distribution",
 
37
            metavar="DISTRO", default="ubuntu", help="Distribution name")
 
38
        self.parser.add_option(
 
39
            "-a", "--arches", dest="arches",
 
40
            help="A comma-separated list of arches to limit the child "
 
41
            "distroseries to inheriting")
 
42
 
 
43
    def main(self):
 
44
        if len(self.args) != 1:
 
45
            self.parser.error("SERIES is required")
 
46
 
 
47
        distroseries_name = self.args[0]
 
48
 
 
49
        try:
 
50
            # 'ubuntu' is the default option.distribution value
 
51
            distribution = getUtility(IDistributionSet)[
 
52
                self.options.distribution]
 
53
            distroseries = distribution[distroseries_name]
 
54
        except NotFoundError, info:
 
55
            raise LaunchpadScriptFailure('%s not found' % info)
 
56
 
 
57
        try:
 
58
            arches = ()
 
59
            if self.options.arches is not None:
 
60
                arches = tuple(self.options.arches.split(','))
 
61
            ids = InitializeDistroSeries(distroseries, arches=arches)
 
62
            self.logger.debug("Checking preconditions")
 
63
            ids.check()
 
64
            self.logger.debug(
 
65
                "Initializing from parent(s), copying publishing records.")
 
66
            ids.initialize()
 
67
        except InitializationError, e:
 
68
            transaction.abort()
 
69
            raise LaunchpadScriptFailure(e)
 
70
 
 
71
        if self.options.dryrun:
 
72
            self.logger.debug('Dry-Run mode, transaction aborted.')
 
73
            transaction.abort()
 
74
        else:
 
75
            self.logger.debug('Committing transaction.')
 
76
            transaction.commit()
108
77
 
109
78
 
110
79
if __name__ == '__main__':
111
 
    sys.exit(main())
 
80
    script = InitializeFromParentScript(
 
81
        'initialize-from-parent', config.initializedistroseries.dbuser)
 
82
    script.lock_and_run()