~launchpad-pqm/launchpad/devel

3944.1.1 by Francis J. Lacoste
Use system version python2.4 for scripts.
1
#!/usr/bin/python2.4
2756 by Canonical.com Patch Queue Manager
Queue-Accepted processor and some upload tweaks to fit it. r=stevea
2
"""Queue/Accepted processor
3
4
Given a distribution to run on, obtains all the queue items for the
8447.1.2 by Jonathan Lange
Fix up lots of lint, including removing code that's been disabled forever.
5
distribution and then gets on and deals with any accepted items, preparing
6
them for publishing as appropriate.
2756 by Canonical.com Patch Queue Manager
Queue-Accepted processor and some upload tweaks to fit it. r=stevea
7
"""
8
4590.1.1 by Julian Edwards
Re-add an import _pythonpath that make lint told us to remove :/
9
import _pythonpath
10
2756 by Canonical.com Patch Queue Manager
Queue-Accepted processor and some upload tweaks to fit it. r=stevea
11
import sys
12
from optparse import OptionParser
13
14
from zope.component import getUtility
15
16
from canonical.config import config
5842.1.1 by James Henstridge
Rename the transaction isolation level constants to match the psycopg2
17
from canonical.database.sqlbase import ISOLATION_LEVEL_READ_COMMITTED
8520.2.1 by Julian Edwards
Remove lib/lp/soyuz/interfaces/package.py and put its enums in queue.py
18
from lp.soyuz.interfaces.queue import PackageUploadStatus
2756 by Canonical.com Patch Queue Manager
Queue-Accepted processor and some upload tweaks to fit it. r=stevea
19
from canonical.launchpad.scripts import (
20
    execute_zcml_for_scripts, logger, logger_options)
8294.6.5 by Julian Edwards
Fix a bunch of circular imports, but there's still one I can't find.
21
from lp.soyuz.scripts.processaccepted import close_bugs
5842.1.1 by James Henstridge
Rename the transaction isolation level constants to match the psycopg2
22
from canonical.lp import initZopeless
2756 by Canonical.com Patch Queue Manager
Queue-Accepted processor and some upload tweaks to fit it. r=stevea
23
24
from contrib.glock import GlobalLock
25
26
def main():
8294.6.7 by Julian Edwards
Circular dependencies, gotta love those.
27
    # Prevent circular imports.
28
    from lp.registry.interfaces.distribution import IDistributionSet
29
2756 by Canonical.com Patch Queue Manager
Queue-Accepted processor and some upload tweaks to fit it. r=stevea
30
    # Parse command-line arguments
31
    parser = OptionParser()
32
    logger_options(parser)
33
3691.443.37 by Celso Providelo
Adding IArchiveSet.getAllPPAs() to the API and extending process-accepted.py to support 'ppa' and 'non_ppa'(normal) mode.
34
    parser.add_option("-n", "--dry-run", action="store_true",
2756 by Canonical.com Patch Queue Manager
Queue-Accepted processor and some upload tweaks to fit it. r=stevea
35
                      dest="dryrun", metavar="DRY_RUN", default=False,
36
                      help="Whether to treat this as a dry-run or not.")
3691.443.37 by Celso Providelo
Adding IArchiveSet.getAllPPAs() to the API and extending process-accepted.py to support 'ppa' and 'non_ppa'(normal) mode.
37
3691.443.43 by Celso Providelo
extend p-d script with PPA-mode and remove bogus publish-ppa.
38
    parser.add_option("--ppa", action="store_true",
3691.443.37 by Celso Providelo
Adding IArchiveSet.getAllPPAs() to the API and extending process-accepted.py to support 'ppa' and 'non_ppa'(normal) mode.
39
                      dest="ppa", metavar="PPA", default=False,
40
                      help="Run only over PPA archives.")
41
2756 by Canonical.com Patch Queue Manager
Queue-Accepted processor and some upload tweaks to fit it. r=stevea
42
    (options, args) = parser.parse_args()
43
44
    log = logger(options, "process-accepted")
45
46
    if len(args) != 1:
47
        log.error("Need to be given exactly one non-option argument. "
48
                  "Namely the distribution to process.")
49
        return 1
50
51
    distro_name = args[0]
52
53
    log.debug("Acquiring lock")
3023.3.10 by Daniel Silverstone
Make process-upload and process-accepted use the same globallock file
54
    lock = GlobalLock('/var/lock/launchpad-upload-queue.lock')
2756 by Canonical.com Patch Queue Manager
Queue-Accepted processor and some upload tweaks to fit it. r=stevea
55
    lock.acquire(blocking=True)
56
57
    log.debug("Initialising connection.")
3023.2.29 by Celso Providelo
Fix bit of process-accepted script
58
5821.5.14 by James Henstridge
Run execute_zcml_for_scripts() before initZopeless() in many tests.
59
    execute_zcml_for_scripts()
5842.1.1 by James Henstridge
Rename the transaction isolation level constants to match the psycopg2
60
    ztm = initZopeless(dbuser=config.uploadqueue.dbuser,
61
                       isolation=ISOLATION_LEVEL_READ_COMMITTED)
2756 by Canonical.com Patch Queue Manager
Queue-Accepted processor and some upload tweaks to fit it. r=stevea
62
4053.1.12 by Bjorn Tillenius
add bug closing to process-accepted.py.
63
    processed_queue_ids = []
2756 by Canonical.com Patch Queue Manager
Queue-Accepted processor and some upload tweaks to fit it. r=stevea
64
    try:
65
        log.debug("Finding distribution %s." % distro_name)
3691.443.37 by Celso Providelo
Adding IArchiveSet.getAllPPAs() to the API and extending process-accepted.py to support 'ppa' and 'non_ppa'(normal) mode.
66
        distribution = getUtility(IDistributionSet).getByName(distro_name)
67
4575.2.4 by Julian Edwards
Some changes based on comments from kiko to tidy the code.
68
        # target_archives is a tuple of (archive, description).
3691.443.37 by Celso Providelo
Adding IArchiveSet.getAllPPAs() to the API and extending process-accepted.py to support 'ppa' and 'non_ppa'(normal) mode.
69
        if options.ppa:
4285.3.49 by Celso Providelo
merge from RF
70
            target_archives = [
71
                (archive, archive.archive_url)
72
                for archive in distribution.getPendingAcceptancePPAs()]
3691.443.37 by Celso Providelo
Adding IArchiveSet.getAllPPAs() to the API and extending process-accepted.py to support 'ppa' and 'non_ppa'(normal) mode.
73
        else:
4285.3.49 by Celso Providelo
merge from RF
74
            target_archives = [
75
                (archive, archive.purpose.title)
76
                for archive in distribution.all_distro_archives]
3691.443.37 by Celso Providelo
Adding IArchiveSet.getAllPPAs() to the API and extending process-accepted.py to support 'ppa' and 'non_ppa'(normal) mode.
77
4575.2.4 by Julian Edwards
Some changes based on comments from kiko to tidy the code.
78
        for archive, description in target_archives:
5121.2.7 by Stuart Bishop
More required code changes
79
            for distroseries in distribution.serieses:
3691.443.87 by Celso Providelo
using selective archive lookup for queue acceptance too.
80
4575.2.4 by Julian Edwards
Some changes based on comments from kiko to tidy the code.
81
                log.debug("Processing queue for %s %s" % (
5121.2.7 by Stuart Bishop
More required code changes
82
                        distroseries.name, description))
3691.443.87 by Celso Providelo
using selective archive lookup for queue acceptance too.
83
5121.2.7 by Stuart Bishop
More required code changes
84
                queue_items = distroseries.getQueueItems(
3691.443.37 by Celso Providelo
Adding IArchiveSet.getAllPPAs() to the API and extending process-accepted.py to support 'ppa' and 'non_ppa'(normal) mode.
85
                    PackageUploadStatus.ACCEPTED, archive=archive)
86
                for queue_item in queue_items:
87
                    try:
88
                        queue_item.realiseUpload(log)
89
                    except:
90
                        log.error("Failure processing queue_item %d"
91
                                  % (queue_item.id), exc_info=True)
92
                        raise
3691.443.113 by Celso Providelo
merge from RF
93
                    else:
94
                        processed_queue_ids.append(queue_item.id)
4053.1.12 by Bjorn Tillenius
add bug closing to process-accepted.py.
95
96
        if not options.dryrun:
97
            ztm.commit()
98
        else:
99
            log.debug("Dry Run mode.")
100
101
        log.debug("Closing bugs.")
102
        close_bugs(processed_queue_ids)
3023.2.29 by Celso Providelo
Fix bit of process-accepted script
103
3691.443.114 by Celso Providelo
fix commit procedure in p-a
104
        if not options.dryrun:
105
            ztm.commit()
106
2756 by Canonical.com Patch Queue Manager
Queue-Accepted processor and some upload tweaks to fit it. r=stevea
107
    finally:
108
        log.debug("Rolling back any remaining transactions.")
109
        ztm.abort()
110
        log.debug("Releasing lock")
111
        lock.release()
112
113
    return 0
114
115
if __name__ == '__main__':
116
    sys.exit(main())
117