~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to database/schema/pending/fix-rosetta-data.py

[r=sinzui][bug=855670] Add additional checks to the private team
        launchpad.LimitedView security adaptor so more users in defined
        roles can see the team.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
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
 
 
6
"""Substitute some chars with the right value in POTranslation.translation."""
 
7
 
 
8
__metaclass__ = type
 
9
__all__ = []
 
10
 
 
11
from optparse import OptionParser
 
12
import sys
 
13
 
 
14
from canonical.lp import initZopeless
 
15
from canonical.database.sqlbase import cursor, sqlvalues
 
16
from lp.services.scripts import logger_options, logger
 
17
 
 
18
mapping = {
 
19
    # u'\u2022': u' ',  # space represented as a dot.
 
20
    u'\u21b5': u'\n', # new line represented with a graphical char.
 
21
    }
 
22
 
 
23
def change_char(ztm, log, character, newchar):
 
24
    """Substitute character with newchar in POTranslation.translation."""
 
25
    log.info("Changing all ocurrences of %r with %r" % (character, newchar))
 
26
    cur = cursor()
 
27
    cur.execute("""
 
28
        SELECT POTranslation.id, POTranslation.translation
 
29
        FROM POTranslation
 
30
        WHERE translation like %s
 
31
        """ % sqlvalues('%%%s%%' % character.encode('UTF-8')))
 
32
 
 
33
    log.info("Found %d translations that may need a fix" % cur.rowcount)
 
34
 
 
35
    for potranslation_id, translation in list(cur.fetchall()):
 
36
        cur = cursor()
 
37
 
 
38
        # Get the list of POMsgSet that will be affected by this change.
 
39
        cur.execute("""
 
40
            SELECT DISTINCT POSubmission.pomsgset
 
41
            FROM POSubmission
 
42
              JOIN POMsgSet ON POSubmission.pomsgset = POMsgSet.id
 
43
              JOIN POTMsgSet ON POMsgSet.potmsgset = POTMsgSet.id
 
44
              JOIN POMsgID ON POTMsgSet.primemsgid = POMsgID.id
 
45
            WHERE
 
46
              POSubmission.potranslation = %s AND
 
47
              POSubmission.active IS TRUE AND
 
48
              POMsgID.msgid NOT like %s
 
49
            """ % sqlvalues(
 
50
                potranslation_id, '%%%s%%' % character.encode('UTF-8')))
 
51
 
 
52
        log.info("Changing %d submissions for IPOTranslation %d" % (
 
53
            cur.rowcount, potranslation_id))
 
54
 
 
55
        pomsgset_ids = [str(id) for [id] in cur.fetchall()]
 
56
        if len(pomsgset_ids) > 0:
 
57
            # There is someone using this translation, update its review date
 
58
            # so new file exports will get this change.
 
59
            cur.execute("""
 
60
                UPDATE POMsgSet
 
61
                SET date_reviewed = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
 
62
                WHERE id IN (%s)
 
63
                """ % ','.join(pomsgset_ids))
 
64
            log.info("Updated %d submissions for IPOTranslation %d" % (
 
65
                cur.rowcount, potranslation_id))
 
66
 
 
67
            fixed_translation = translation.replace(
 
68
                character, newchar).encode('UTF-8')
 
69
 
 
70
            cur.execute("""
 
71
                SELECT id
 
72
                FROM POTranslation
 
73
                WHERE sha1(translation) = sha1(%s)
 
74
                """ % sqlvalues(fixed_translation))
 
75
 
 
76
            if cur.rowcount == 0:
 
77
                # The fixed translation is not yet in our database, let's add
 
78
                # it.
 
79
                cur.execute(
 
80
                    "INSERT INTO POTranslation(translation) VALUES(%s)" % (
 
81
                        sqlvalues(fixed_translation)))
 
82
 
 
83
                cur.execute("""
 
84
                    SELECT id
 
85
                    FROM POTranslation
 
86
                    WHERE translation = %s
 
87
                    """ % sqlvalues(fixed_translation))
 
88
 
 
89
            # Get the id of the fixed POTranslation.
 
90
            [existing_translation_id] = cur.fetchone()
 
91
 
 
92
            # Get the submissions that should point to the fixed translation.
 
93
            cur.execute("""
 
94
                SELECT id
 
95
                FROM POSubmission
 
96
                WHERE
 
97
                  potranslation = %d AND
 
98
                  pomsgset IN (%s) AND
 
99
                  active IS TRUE
 
100
                """ % (potranslation_id, ','.join(pomsgset_ids)))
 
101
 
 
102
            submission_ids = [str(id) for [id] in cur.fetchall()]
 
103
 
 
104
            # Link to the new fixed translation.
 
105
            cur.execute("""
 
106
                UPDATE POSubmission
 
107
                SET potranslation = %d
 
108
                WHERE id IN (%s)
 
109
                """ % (existing_translation_id, ','.join(submission_ids)))
 
110
 
 
111
            # Commit is done per IPOTranslation.
 
112
            ztm.commit()
 
113
 
 
114
def read_options():
 
115
    """Read the command-line options and return an options object."""
 
116
    parser = OptionParser()
 
117
    logger_options(parser)
 
118
 
 
119
    (options, args) = parser.parse_args()
 
120
 
 
121
    return options
 
122
 
 
123
def main():
 
124
    options = read_options()
 
125
    log = logger(options)
 
126
 
 
127
    ztm = None
 
128
    try:
 
129
        log.debug("Connecting to database")
 
130
        ztm = initZopeless()
 
131
        for oldchar, newchar in mapping.iteritems():
 
132
            change_char(ztm, log, oldchar, newchar)
 
133
        return 0
 
134
    except:
 
135
        log.exception("Unhandled exception")
 
136
        log.debug("Rolling back")
 
137
        if ztm is not None:
 
138
            ztm.abort()
 
139
        return 1
 
140
 
 
141
if __name__ == '__main__':
 
142
    sys.exit(main())