3
# Copyright 2009 Canonical Ltd. This software is licensed under the
4
# GNU Affero General Public License version 3 (see the file LICENSE).
6
"""Substitute some chars with the right value in POTranslation.translation."""
11
from optparse import OptionParser
14
from canonical.lp import initZopeless
15
from canonical.database.sqlbase import cursor, sqlvalues
16
from lp.services.scripts import logger_options, logger
19
# u'\u2022': u' ', # space represented as a dot.
20
u'\u21b5': u'\n', # new line represented with a graphical char.
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))
28
SELECT POTranslation.id, POTranslation.translation
30
WHERE translation like %s
31
""" % sqlvalues('%%%s%%' % character.encode('UTF-8')))
33
log.info("Found %d translations that may need a fix" % cur.rowcount)
35
for potranslation_id, translation in list(cur.fetchall()):
38
# Get the list of POMsgSet that will be affected by this change.
40
SELECT DISTINCT POSubmission.pomsgset
42
JOIN POMsgSet ON POSubmission.pomsgset = POMsgSet.id
43
JOIN POTMsgSet ON POMsgSet.potmsgset = POTMsgSet.id
44
JOIN POMsgID ON POTMsgSet.primemsgid = POMsgID.id
46
POSubmission.potranslation = %s AND
47
POSubmission.active IS TRUE AND
48
POMsgID.msgid NOT like %s
50
potranslation_id, '%%%s%%' % character.encode('UTF-8')))
52
log.info("Changing %d submissions for IPOTranslation %d" % (
53
cur.rowcount, potranslation_id))
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.
61
SET date_reviewed = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
63
""" % ','.join(pomsgset_ids))
64
log.info("Updated %d submissions for IPOTranslation %d" % (
65
cur.rowcount, potranslation_id))
67
fixed_translation = translation.replace(
68
character, newchar).encode('UTF-8')
73
WHERE sha1(translation) = sha1(%s)
74
""" % sqlvalues(fixed_translation))
77
# The fixed translation is not yet in our database, let's add
80
"INSERT INTO POTranslation(translation) VALUES(%s)" % (
81
sqlvalues(fixed_translation)))
86
WHERE translation = %s
87
""" % sqlvalues(fixed_translation))
89
# Get the id of the fixed POTranslation.
90
[existing_translation_id] = cur.fetchone()
92
# Get the submissions that should point to the fixed translation.
97
potranslation = %d AND
100
""" % (potranslation_id, ','.join(pomsgset_ids)))
102
submission_ids = [str(id) for [id] in cur.fetchall()]
104
# Link to the new fixed translation.
107
SET potranslation = %d
109
""" % (existing_translation_id, ','.join(submission_ids)))
111
# Commit is done per IPOTranslation.
115
"""Read the command-line options and return an options object."""
116
parser = OptionParser()
117
logger_options(parser)
119
(options, args) = parser.parse_args()
124
options = read_options()
125
log = logger(options)
129
log.debug("Connecting to database")
131
for oldchar, newchar in mapping.iteritems():
132
change_char(ztm, log, oldchar, newchar)
135
log.exception("Unhandled exception")
136
log.debug("Rolling back")
141
if __name__ == '__main__':