~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to scripts/gina.py

  • Committer: Julian Edwards
  • Date: 2010-08-26 08:02:08 UTC
  • mfrom: (11447 launchpad)
  • mto: This revision was merged to the branch mainline in revision 11453.
  • Revision ID: julian.edwards@canonical.com-20100826080208-sut7s35g9z0qtk9v
merge devel

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/python -S
2
2
#
3
 
# Copyright 2009 Canonical Ltd.  This software is licensed under the
 
3
# Copyright 2009,2010 Canonical Ltd.  This software is licensed under the
4
4
# GNU Affero General Public License version 3 (see the file LICENSE).
5
5
 
6
6
# This module uses relative imports.
30
30
# Set to non-zero if you'd like to be warned every so often
31
31
COUNTDOWN = 0
32
32
 
33
 
from optparse import OptionParser
34
33
import os
35
34
import psycopg2
36
35
import sys
38
37
 
39
38
from zope.component import getUtility
40
39
 
41
 
from contrib.glock import GlobalLock, LockAlreadyAcquired
42
 
 
43
40
from canonical import lp
44
 
from canonical.lp import initZopeless
45
41
from canonical.config import config
 
42
from canonical.launchpad.scripts import log
 
43
 
 
44
from lp.services.scripts.base import LaunchpadCronScript
46
45
from lp.soyuz.interfaces.component import IComponentSet
47
 
from canonical.launchpad.scripts import (
48
 
    execute_zcml_for_scripts, logger_options, log)
49
 
 
50
46
from lp.soyuz.scripts.gina import ExecutionError
51
47
from lp.soyuz.scripts.gina.katie import Katie
52
48
from lp.soyuz.scripts.gina.archive import (ArchiveComponentItems,
53
49
    PackagesMap, MangledArchiveError)
54
 
 
55
50
from lp.soyuz.scripts.gina.handlers import (ImporterHandler,
56
51
    MultiplePackageReleaseError, NoSourcePackageError, DataSetupError)
57
52
from lp.soyuz.scripts.gina.packages import (SourcePackageData,
70
65
    return keyrings
71
66
 
72
67
 
73
 
def main():
74
 
    execute_zcml_for_scripts()
75
 
    parser = OptionParser("Usage: %prog [OPTIONS] [target ...]")
76
 
    logger_options(parser)
77
 
 
78
 
    parser.add_option("-n", "--dry-run", action="store_true",
79
 
            help="Don't commit changes to the database",
80
 
            dest="dry_run", default=False)
81
 
 
82
 
    parser.add_option("-a", "--all", action="store_true",
83
 
            help="Run all sections defined in launchpad.conf (in order)",
84
 
            dest="all", default=False)
85
 
 
86
 
    parser.add_option( "-l", "--lockfile",
87
 
            default="/var/lock/launchpad-gina.lock",
88
 
            help="Ensure only one process is running that locks LOCKFILE",
89
 
            metavar="LOCKFILE"
90
 
            )
91
 
 
92
 
    (options, targets) = parser.parse_args()
93
 
 
94
 
    possible_targets = [target.category_and_section_names[1]
95
 
                        for target in config.getByCategory('gina_target')]
96
 
 
97
 
    if options.all:
98
 
        targets = possible_targets[:]
99
 
    else:
100
 
        if not targets:
101
 
            parser.error("Must specify at least one target to run, or --all")
102
 
 
103
 
        for target in targets:
104
 
            if target not in possible_targets:
105
 
                parser.error("No Gina target %s in config file" % target)
106
 
 
107
 
    lockfile = GlobalLock(options.lockfile, logger=log)
108
 
    try:
109
 
        lockfile.acquire()
110
 
    except LockAlreadyAcquired:
111
 
        log.info('Lockfile %s already locked. Exiting.', options.lockfile)
112
 
        sys.exit(1)
113
 
 
114
 
    ztm = initZopeless(dbuser=config.gina.dbuser)
115
 
    try:
116
 
        for target in targets:
117
 
            target_section = config['gina_target.%s' % target]
118
 
            run_gina(options, ztm, target_section)
119
 
    finally:
120
 
        lockfile.release()
121
 
 
122
 
 
123
68
def run_gina(options, ztm, target_section):
124
69
    # Avoid circular imports.
125
70
    from lp.registry.interfaces.pocket import PackagePublishingPocket
357
302
    importer_handler.commit()
358
303
 
359
304
 
 
305
class Gina(LaunchpadCronScript):
 
306
 
 
307
    def __init__(self):
 
308
        super(Gina, self).__init__(name='gina', dbuser=config.gina.dbuser)
 
309
 
 
310
    def add_my_options(self):
 
311
        self.parser.add_option("-n", "--dry-run", action="store_true",
 
312
            help="Don't commit changes to the database",
 
313
            dest="dry_run", default=False)
 
314
        self.parser.add_option("-a", "--all", action="store_true",
 
315
            help="Run all sections defined in launchpad.conf (in order)",
 
316
            dest="all", default=False)
 
317
 
 
318
    def main(self):
 
319
        possible_targets = [target.category_and_section_names[1]
 
320
            for target in config.getByCategory('gina_target')]
 
321
        targets = self.args
 
322
        if self.options.all:
 
323
            targets = possible_targets[:]
 
324
        else:
 
325
            if not targets:
 
326
                self.parser.error(
 
327
                    "Must specify at least one target to run, or --all")
 
328
            for target in targets:
 
329
                if target not in possible_targets:
 
330
                    self.parser.error(
 
331
                        "No Gina target %s in config file" % target)
 
332
 
 
333
        for target in targets:
 
334
            target_section = config['gina_target.%s' % target]
 
335
            run_gina(self.options, self.txn, target_section)
 
336
 
 
337
 
360
338
if __name__ == "__main__":
361
 
    main()
362
 
 
 
339
    gina = Gina()
 
340
    gina.lock_and_run()