~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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/python2.4
# Copyright 2004-2007 Canonical Ltd.  All rights reserved.
# pylint: disable-msg=C0103,W0403

import _pythonpath

import gc
from optparse import OptionParser

from zope.component import getUtility

from canonical.archivepublisher.publishing import getPublisher
from canonical.config import config
from canonical.database.sqlbase import (
    flush_database_updates, clear_current_connection_cache)
from canonical.launchpad.interfaces import (
    ArchivePurpose, IDistributionSet, NotFoundError)
from canonical.launchpad.scripts import (
    execute_zcml_for_scripts, logger, logger_options)
from canonical.lp import initZopeless


def parse_options():
    parser = OptionParser()
    logger_options(parser)

    parser.add_option("-C", "--careful", action="store_true",
                      dest="careful", metavar="", default=False,
                      help="Turns on all the below careful options.")

    parser.add_option("-P", "--careful-publishing", action="store_true",
                      dest="careful_publishing", metavar="", default=False,
                      help="Make the package publishing process careful.")

    parser.add_option("-D", "--careful-domination", action="store_true",
                      dest="careful_domination", metavar="", default=False,
                      help="Make the domination process careful.")

    parser.add_option("-A", "--careful-apt", action="store_true",
                      dest="careful_apt", metavar="", default=False,
                      help="Make the apt-ftparchive run careful.")

    parser.add_option("-d", "--distribution",
                      dest="distribution", metavar="DISTRO", default="ubuntu",
                      help="The distribution to publish.")

    parser.add_option('-s', '--suite', metavar='SUITE', dest='suite',
                      action='append', type='string', default=[],
                      help='The suite to publish')

    parser.add_option("-R", "--distsroot",
                      dest="distsroot", metavar="SUFFIX", default=None,
                      help="Override the dists path for generation of the "
                           "PRIMARY and PARTNER archives only.")

    parser.add_option("--ppa", action="store_true",
                      dest="ppa", metavar="PPA", default=False,
                      help="Run only over PPA archives.")

    parser.add_option("--partner", action="store_true",
                      dest="partner", metavar="PARTNER", default=False,
                      help="Run only over the partner archive.")

    return parser.parse_args()

def main():
    options, args = parse_options()
    assert len(args) == 0, "publish-distro takes no arguments, only options."

    log = logger(options, "publish-distro")

    def careful_msg(what):
        """Quick handy util for the below."""
        if options.careful:
            return "Careful (Overridden)"
        if what:
            return "Careful"
        return "Normal"

    def try_and_commit(description, func, *args):
        try:
            func(*args)
            log.debug("Committing.")
            flush_database_updates()
            txn.commit()
            log.debug("Flushing caches.")
            clear_current_connection_cache()
            gc.collect()
        except:
            log.exception("Unexpected exception while %s" % description)
            txn.abort()
            raise

    log.info("  Distribution: %s" % options.distribution)
    log.info("    Publishing: %s" % careful_msg(options.careful_publishing))
    log.info("    Domination: %s" % careful_msg(options.careful_domination))

    if not options.ppa:
        log.info("Apt-FTPArchive: %s" % careful_msg(options.careful_apt))
    else:
        log.info("      Indexing: %s" % careful_msg(options.careful_apt))

    if options.partner and options.ppa:
        log.error("Can only specify one of partner or ppa")
        return

    log.debug("Initialising zopeless.")

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

    log.debug("Finding distribution object.")

    try:
        distribution = getUtility(IDistributionSet).getByName(
            options.distribution)
    except NotFoundError, info:
        log.error(info)
        raise

    allowed_suites = set()
    for suite in options.suite:
        try:
            distroseries, pocket = distribution.getDistroSeriesAndPocket(
                suite)
        except NotFoundError, info:
            log.error(info)
            raise
        allowed_suites.add((distroseries.name, pocket))

    if options.partner:
        archives = [distribution.getArchiveByComponent('partner')]
    elif options.ppa:
        if options.careful or options.careful_publishing:
            archives = distribution.getAllPPAs()
        else:
            archives = distribution.getPendingPublicationPPAs()

        if options.distsroot is not None:
            log.error("We should not define 'distsroot' in PPA mode !")
            return
    else:
        archives = [distribution.main_archive]

    for archive in archives:
        if archive.purpose != ArchivePurpose.PPA:
            log.info("Processing %s %s" % (
                distribution.name, archive.title))
        else:
            log.info("Processing %s" % archive.archive_url)

        # Only let the primary/partner archives override the distsroot.
        if archive.purpose in (ArchivePurpose.PRIMARY,
                ArchivePurpose.PARTNER):
            publisher = getPublisher(
                archive, allowed_suites, log, options.distsroot)
        else:
            publisher = getPublisher(archive, allowed_suites, log)

        try_and_commit("publishing", publisher.A_publish,
                       options.careful or options.careful_publishing)
        # Flag dirty pockets for any outstanding deletions.
        publisher.A2_markPocketsWithDeletionsDirty()
        try_and_commit("dominating", publisher.B_dominate,
                       options.careful or options.careful_domination)

        # The primary archive uses apt-ftparchive to generate the indexes,
        # everything else uses the newer internal LP code.
        if archive.purpose != ArchivePurpose.PPA:
            try_and_commit("doing apt-ftparchive", publisher.C_doFTPArchive,
                           options.careful or options.careful_apt)
        else:
            try_and_commit("building indexes", publisher.C_writeIndexes,
                           options.careful or options.careful_apt)

        try_and_commit("doing release files", publisher.D_writeReleaseFiles,
                       options.careful or options.careful_apt)

    log.debug("Ciao")


if __name__ == "__main__":
    main()