~launchpad-pqm/launchpad/devel

10637.3.1 by Guilherme Salgado
Use the default python version instead of a hard-coded version
1
#!/usr/bin/python -S
8687.15.34 by Karl Fogel
Add license header blocks to .py, .zcml, and .pt files that don't have it
2
#
3
# Copyright 2009 Canonical Ltd.  This software is licensed under the
4
# GNU Affero General Public License version 3 (see the file LICENSE).
5
8687.1.1 by Danilo Šegan
Provide script to share Jaunty translations.
6
# pylint: disable-msg=W0403
7
# (Suppressing pylint "relative import" warning 0403 for _pythonpath)
8
9
import _pythonpath
10
11
from zope.interface import implements
12
13
from canonical.database.postgresql import drop_tables
14
from canonical.database.sqlbase import (
15
    cursor, quote, quote_identifier, sqlvalues)
16
from canonical.launchpad.interfaces.looptuner import ITunableLoop
17
from lp.services.scripts.base import LaunchpadScript
18
from canonical.launchpad.utilities.looptuner import DBLoopTuner
19
20
21
class ShareJauntyTranslationMessages:
22
    """`ITunableLoop` to share Jaunty TranslationMessages."""
23
24
    implements(ITunableLoop)
25
    def __init__(self, txn, logger, flag, seriesname):
26
        self.txn = txn
27
        self.logger = logger
28
        self.last_id = 0
29
30
        cur = cursor()
31
32
        cur.execute("""
33
            SELECT DistroSeries.id
34
            FROM DistroSeries
35
            JOIN Distribution ON Distribution.id = DistroSeries.distribution
36
            WHERE
37
                Distribution.name = 'ubuntu' AND DistroSeries.name = %s
38
            """ % quote(seriesname))
39
        self.series_id = cur.fetchone()
40
41
        substitutions = {
42
            'flag': quote_identifier(flag),
43
            'series_id': quote(self.series_id),
44
        }
45
46
        cur.execute("""
47
            SELECT DISTINCT Candidate.id
48
            INTO TEMP TABLE temp_todo
49
            FROM TranslationMessage Candidate
50
            JOIN POTemplate ON Candidate.potemplate = POTemplate.id
51
            LEFT JOIN TranslationMessage AS FlagHolder ON
52
                FlagHolder.%(flag)s IS TRUE AND
53
                FlagHolder.potmsgset = Candidate.potmsgset AND
54
                FlagHolder.potemplate IS NULL AND
11122.3.4 by Danilo Šegan
Get rid of the remaining variant usage.
55
                FlagHolder.language = Candidate.language
8687.1.1 by Danilo Šegan
Provide script to share Jaunty translations.
56
            WHERE
57
                POTemplate.distroseries = %(series_id)s AND
58
                POTemplate.iscurrent IS TRUE AND
59
                Candidate.%(flag)s IS TRUE AND
60
                FlagHolder.id IS NULL
61
            ORDER BY id
62
            """ % substitutions)
63
64
        cur.execute(
65
            "CREATE UNIQUE INDEX temp_todo__pkey ON temp_todo(id)")
66
        cur.execute("ANALYZE temp_todo(id)")
67
68
        cur.execute("SELECT max(id) FROM temp_todo")
69
        max_id, = cur.fetchone()
70
        if max_id is None:
71
            self.finish_id = 0
72
        else:
73
            self.finish_id = max_id + 1
74
75
    def isDone(self):
76
        """See `ITunableLoop`."""
77
        done = (self.last_id >= self.finish_id)
78
        if done:
79
            drop_tables(cursor(), 'temp_todo')
80
        return done
81
82
    def __call__(self, chunk_size):
83
        """See `ITunableLoop`."""
84
        chunk_size = int(chunk_size)
85
86
        cur = cursor()
87
        cur.execute("""
88
            SELECT id
89
            FROM temp_todo
90
            WHERE id >= %s
91
            ORDER BY id
92
            OFFSET %s
93
            LIMIT 1
94
            """ % sqlvalues(self.last_id, chunk_size))
95
        batch_limit = cur.fetchone()
96
        if batch_limit is None:
97
            end_id = self.finish_id
98
        else:
99
            end_id, = batch_limit
100
101
        cur.execute("""
102
            UPDATE TranslationMessage
103
            SET potemplate = NULL
104
            WHERE id IN (
105
                SELECT id
106
                FROM temp_todo
107
                WHERE id >= %s AND id < %s
108
                )
109
            """ % sqlvalues(self.last_id, end_id))
110
        self.logger.info(
111
            "Updated %d rows: %d - %d." % (
112
                cur.rowcount, self.last_id, end_id))
113
        self.txn.commit()
114
        self.txn.begin()
115
        self.last_id = end_id
116
117
118
class ShareJauntyTranslationsScript(LaunchpadScript):
119
120
    def main(self):
121
        series = 'jaunty'
8687.1.2 by Danilo Šegan
Output series name in a log message.
122
        self.logger.info("Making '%s' TranslationMessages shared." % series)
8687.1.1 by Danilo Šegan
Provide script to share Jaunty translations.
123
124
        for flag in ('is_current', 'is_imported'):
125
            self.logger.info("Sharing %s messages." % flag)
126
            loop = ShareJauntyTranslationMessages(
127
                self.txn, self.logger, flag, series)
128
            DBLoopTuner(loop, 5, log=self.logger).run()
129
130
131
if __name__ == '__main__':
132
    script = ShareJauntyTranslationsScript(
133
        'rosetta.scripts.share-jaunty-translations')
134
    script.run()