~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: 2011-09-19 04:01:37 UTC
  • mfrom: (13970.7.23 port-to-LaunchpadScript)
  • Revision ID: launchpad@pqm.canonical.com-20110919040137-sr8154o9tfptnqir
[r=sinzui][no-qa] Port a dozen scripts to LaunchpadScript,
        removing their direct initZopeless dependency.

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
13
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
14
from lp.app.errors import NotFoundError
23
15
from lp.registry.interfaces.distribution import IDistributionSet
 
16
from lp.services.scripts.base import LaunchpadScript, LaunchpadScriptFailure
24
17
from lp.soyuz.scripts.initialize_distroseries import (
25
18
    InitializationError,
26
19
    InitializeDistroSeries,
27
20
    )
28
21
 
29
22
 
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.initializedistroseries.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
 
        ids = InitializeDistroSeries(distroseries, arches=arches)
85
 
        ids.check()
86
 
        log.debug('initializing from parent(s), copying publishing records.')
87
 
        ids.initialize()
88
 
    except InitializationError, e:
89
 
        ztm.abort()
90
 
        log.error(e)
91
 
        return 1
92
 
 
93
 
    if options.dryrun:
94
 
        log.debug('Dry-Run mode, transaction aborted.')
95
 
        ztm.abort()
96
 
    else:
97
 
        log.debug('Committing transaction.')
98
 
        ztm.commit()
99
 
 
100
 
    log.debug("Releasing lock")
101
 
    lock.release()
102
 
    return 0
 
23
class InitializeFromParentScript(LaunchpadScript):
 
24
 
 
25
    usage = "Usage: %prog [options] <SERIES>"
 
26
 
 
27
    def add_my_options(self):
 
28
        self.parser.add_option(
 
29
            "-N", "--dry-run", action="store_true",
 
30
            dest="dryrun", metavar="DRY_RUN", default=False,
 
31
            help="Whether to treat this as a dry-run or not.")
 
32
        self.parser.add_option(
 
33
            "-d", "--distro", dest="distribution",
 
34
            metavar="DISTRO", default="ubuntu", help="Distribution name")
 
35
        self.parser.add_option(
 
36
            "-a", "--arches", dest="arches",
 
37
            help="A comma-seperated list of arches to limit the child "
 
38
            "distroseries to inheriting")
 
39
 
 
40
    def main(self):
 
41
        if len(self.args) != 1:
 
42
            self.parser.error("SERIES is required")
 
43
 
 
44
        distroseries_name = self.args[0]
 
45
 
 
46
        try:
 
47
            # 'ubuntu' is the default option.distribution value
 
48
            distribution = getUtility(IDistributionSet)[
 
49
                self.options.distribution]
 
50
            distroseries = distribution[distroseries_name]
 
51
        except NotFoundError, info:
 
52
            raise LaunchpadScriptFailure('%s not found' % info)
 
53
 
 
54
        try:
 
55
            arches = ()
 
56
            if self.options.arches is not None:
 
57
                arches = tuple(self.options.arches.split(','))
 
58
            ids = InitializeDistroSeries(distroseries, arches=arches)
 
59
            self.logger.debug("Checking preconditions")
 
60
            ids.check()
 
61
            self.logger.debug(
 
62
                "Initializing from parent(s), copying publishing records.")
 
63
            ids.initialize()
 
64
        except InitializationError, e:
 
65
            transaction.abort()
 
66
            raise LaunchpadScriptFailure(e)
 
67
 
 
68
        if self.options.dryrun:
 
69
            self.logger.debug('Dry-Run mode, transaction aborted.')
 
70
            transaction.abort()
 
71
        else:
 
72
            self.logger.debug('Committing transaction.')
 
73
            transaction.commit()
103
74
 
104
75
 
105
76
if __name__ == '__main__':
106
 
    sys.exit(main())
 
77
    script = InitializeFromParentScript(
 
78
        'initialize-from-parent', config.initializedistroseries.dbuser)
 
79
    script.lock_and_run()