~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
#!/usr/bin/env python
# Copyright 2004 Canonical Ltd.  All rights reserved.

import logging, sys

from optparse import OptionParser

from canonical.lp import initZopeless
from canonical.launchpad.scripts import execute_zcml_for_scripts
from canonical.launchpad.scripts.lockfile import LockFile
from canonical.launchpad.database import POTemplateSet, POFileSet

_default_lock_file = '/var/lock/launchpad-poimport.lock'

class ImportProcess:
    def __init__(self):
        self._tm = initZopeless()
        self.po_templates = POTemplateSet()
        self.po_files = POFileSet()

    def commit(self):
        self._tm.commit()

    def abort(self):
        self._tm.abort()

    def getPendingImports(self):
        '''Iterate over all PO templates and PO files which are waiting to be
        imported.
        '''

        for template in self.po_templates.getTemplatesPendingImport():
            logger.info('Importing the template: %s' % template.title)
            yield template

        for pofile in self.po_files.getPOFilesPendingImport():
            logger.info('Importing the %s translation of %s' % (
                pofile.language.englishname, pofile.potemplate.title))
            yield pofile

    def run(self):
        for object in self.getPendingImports():
            # object could be a POTemplate or a POFile but both
            # objects implement the doRawImport method so we don't
            # need to care about it here.
            try:
                object.doRawImport(logger)
            except KeyboardInterrupt:
                self.abort()
                raise
            except:
                # If we have any exception, we log it before terminating
                # the process.
                logger.error('We got an unexpected exception while importing',
                             exc_info = 1)
                self.abort()
                continue

            # As soon as the import is done, we commit the transaction
            # so it's not lost.
            try:
                self.commit()
            except KeyboardInterrupt:
                self.abort()
                raise
            except:
                # If we have any exception, we log it before terminating
                # the process.
                logger.error('We got an unexpected exception while committing'
                             'the transaction', exc_info = 1)
                self.abort()

def parse_options():
    parser = OptionParser()
    parser.add_option("-v", "--verbose", dest="verbose",
        default=0, action="count",
        help="Displays extra information.")
    parser.add_option("-q", "--quiet", dest="quiet",
        default=0, action="count",
        help="Display less information.")
    parser.add_option("-l", "--lockfile", dest="lockfilename",
        default=_default_lock_file,
        help="The file the script should use to lock the process.")

    (options, args) = parser.parse_args()

    return options

def main():
    # Do the import of all pending files from the queue.
    process = ImportProcess()
    logger.debug('Starting the import process')
    process.run()
    logger.debug('Finished the import process')

def setUpLogger():
    loglevel = logging.WARN

    for i in range(options.verbose):
        if loglevel == logging.INFO:
            loglevel = logging.DEBUG
        elif loglevel == logging.WARN:
            loglevel = logging.INFO
    for i in range(options.quiet):
        if loglevel == logging.WARN:
            loglevel = logging.ERROR
        elif loglevel == logging.ERROR:
            loglevel = logging.CRITICAL

    hdlr = logging.StreamHandler(strm=sys.stderr)
    hdlr.setFormatter(logging.Formatter(
        fmt='%(asctime)s %(levelname)s %(message)s'
        ))
    logger.addHandler(hdlr)
    logger.setLevel(loglevel)

if __name__ == '__main__':
    execute_zcml_for_scripts()

    options = parse_options()

    # Get the global logger for this task.
    logger = logging.getLogger("poimport")
    # customized the logger output.
    setUpLogger()

    # Create a lock file so we don't have two daemons running at the same time.
    lockfile = LockFile(options.lockfilename, logger=logger)

    try:
        lockfile.acquire()
    except OSError:
        logger.info("lockfile %s already exists, exiting",
                    options.lockfilename)
        sys.exit(0)

    try:
        main()
    finally:
        # Release the lock so next planned task can be executed.
        lockfile.release()