~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to scripts/gina.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2010-08-25 14:38:56 UTC
  • mfrom: (11416.2.2 gina-log-to-scriptactivity)
  • Revision ID: launchpad@pqm.canonical.com-20100825143856-8iumovg4n502gpjd
[r=adeuring][ui=none][bug=449408] Change scripts/gina.py to sub-class
        LaunchpadCronScript.

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.
25
25
 
26
26
import _pythonpath
27
27
 
28
 
from optparse import OptionParser
29
28
import os
30
29
import psycopg2
31
30
import sys
33
32
 
34
33
from zope.component import getUtility
35
34
 
36
 
from contrib.glock import GlobalLock, LockAlreadyAcquired
37
 
 
38
35
from canonical import lp
39
 
from canonical.lp import initZopeless
40
36
from canonical.config import config
 
37
from canonical.launchpad.scripts import log
 
38
 
 
39
from lp.services.scripts.base import LaunchpadCronScript
41
40
from lp.soyuz.interfaces.component import IComponentSet
42
 
from canonical.launchpad.scripts import (
43
 
    execute_zcml_for_scripts, logger_options, log)
44
 
 
45
41
from lp.soyuz.scripts.gina import ExecutionError
46
42
from lp.soyuz.scripts.gina.katie import Katie
47
43
from lp.soyuz.scripts.gina.archive import (ArchiveComponentItems,
48
44
    PackagesMap, MangledArchiveError)
49
 
 
50
45
from lp.soyuz.scripts.gina.handlers import (ImporterHandler,
51
46
    MultiplePackageReleaseError, NoSourcePackageError, DataSetupError)
52
47
from lp.soyuz.scripts.gina.packages import (SourcePackageData,
65
60
    return keyrings
66
61
 
67
62
 
68
 
def main():
69
 
    execute_zcml_for_scripts()
70
 
    parser = OptionParser("Usage: %prog [OPTIONS] [target ...]")
71
 
    logger_options(parser)
72
 
 
73
 
    parser.add_option("-n", "--dry-run", action="store_true",
74
 
            help="Don't commit changes to the database",
75
 
            dest="dry_run", default=False)
76
 
 
77
 
    parser.add_option("-a", "--all", action="store_true",
78
 
            help="Run all sections defined in launchpad.conf (in order)",
79
 
            dest="all", default=False)
80
 
 
81
 
    parser.add_option( "-l", "--lockfile",
82
 
            default="/var/lock/launchpad-gina.lock",
83
 
            help="Ensure only one process is running that locks LOCKFILE",
84
 
            metavar="LOCKFILE"
85
 
            )
86
 
 
87
 
    (options, targets) = parser.parse_args()
88
 
 
89
 
    possible_targets = [target.category_and_section_names[1]
90
 
                        for target in config.getByCategory('gina_target')]
91
 
 
92
 
    if options.all:
93
 
        targets = possible_targets[:]
94
 
    else:
95
 
        if not targets:
96
 
            parser.error("Must specify at least one target to run, or --all")
97
 
 
98
 
        for target in targets:
99
 
            if target not in possible_targets:
100
 
                parser.error("No Gina target %s in config file" % target)
101
 
 
102
 
    lockfile = GlobalLock(options.lockfile, logger=log)
103
 
    try:
104
 
        lockfile.acquire()
105
 
    except LockAlreadyAcquired:
106
 
        log.info('Lockfile %s already locked. Exiting.', options.lockfile)
107
 
        sys.exit(1)
108
 
 
109
 
    ztm = initZopeless(dbuser=config.gina.dbuser)
110
 
    try:
111
 
        for target in targets:
112
 
            target_section = config['gina_target.%s' % target]
113
 
            run_gina(options, ztm, target_section)
114
 
    finally:
115
 
        lockfile.release()
116
 
 
117
 
 
118
63
def run_gina(options, ztm, target_section):
119
64
    # Avoid circular imports.
120
65
    from lp.registry.interfaces.pocket import PackagePublishingPocket
352
297
    importer_handler.commit()
353
298
 
354
299
 
 
300
class Gina(LaunchpadCronScript):
 
301
 
 
302
    def __init__(self):
 
303
        super(Gina, self).__init__(name='gina', dbuser=config.gina.dbuser)
 
304
 
 
305
    def add_my_options(self):
 
306
        self.parser.add_option("-n", "--dry-run", action="store_true",
 
307
            help="Don't commit changes to the database",
 
308
            dest="dry_run", default=False)
 
309
        self.parser.add_option("-a", "--all", action="store_true",
 
310
            help="Run all sections defined in launchpad.conf (in order)",
 
311
            dest="all", default=False)
 
312
 
 
313
    def main(self):
 
314
        possible_targets = [target.category_and_section_names[1]
 
315
            for target in config.getByCategory('gina_target')]
 
316
        targets = self.args
 
317
        if self.options.all:
 
318
            targets = possible_targets[:]
 
319
        else:
 
320
            if not targets:
 
321
                self.parser.error(
 
322
                    "Must specify at least one target to run, or --all")
 
323
            for target in targets:
 
324
                if target not in possible_targets:
 
325
                    self.parser.error(
 
326
                        "No Gina target %s in config file" % target)
 
327
 
 
328
        for target in targets:
 
329
            target_section = config['gina_target.%s' % target]
 
330
            run_gina(self.options, self.txn, target_section)
 
331
 
 
332
 
355
333
if __name__ == "__main__":
356
 
    main()
357
 
 
 
334
    gina = Gina()
 
335
    gina.lock_and_run()