~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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
= "Remove Translations By" script =

Use scripts/remove-translations-by.py to delete selected
TranslationMessages from the database.  This is useful for mislicensed
translations, but also for ones submitted in the wrong language, or in
bad faith, or all messages in a specific POFile, and so on.

    >>> script = "scripts/rosetta/remove-translations-by.py"
    >>> login('foo.bar@canonical.com')

In this example, we have a template with Dutch and German translations.

    >>> nl_pofile = factory.makePOFile('nl')
    >>> potemplate = nl_pofile.potemplate
    >>> de_pofile = factory.makePOFile('de', potemplate=potemplate)
    >>> owner = potemplate.owner

    >>> def set_translation(message, pofile, text):
    ...     """Set text to be a translation for message in pofile."""
    ...     return factory.makeCurrentTranslationMessage(
    ...         pofile, message, pofile.potemplate.owner,
    ...         translations={0: text})

    >>> def print_pofile_contents(pofile):
    ...     """Return sorted list of (singular) translations in pofile."""
    ...     contents = sorted(
    ...         message.msgstr0.translation
    ...         for message in pofile.translation_messages
    ...         if message.msgstr0 is not None)
    ...     for item in contents:
    ...         print item


== Running the script==

Most options that specify which messages to delete conjunctively
constrain the deletion.  In other words, add options to make the
deletion more specific.  In principle, passing no options at all would
mean "delete absolutely all TranslationMessages."

The "id" option may be repeated to specify the ids of multiple messages
to be deleted.  (But again, they are deleted only if they match all
criteria).

    >>> from lp.testing.script import run_script
    >>> from lp.translations.interfaces.translationmessage import (
    ...     RosettaTranslationOrigin)
    >>> from storm.store import Store

    >>> message = factory.makePOTMsgSet(
    ...     potemplate, "My goose is undercooked.", sequence=0)

    >>> nl_message = set_translation(
    ...     message, nl_pofile, "Maar dan in het Nederlands.")
    >>> nl_message.is_current_upstream
    True
    >>> nl_message.is_current_ubuntu
    False
    >>> print nl_message.potmsgset.msgid_singular.msgid
    My goose is undercooked.
    >>> nl_message.origin == RosettaTranslationOrigin.ROSETTAWEB
    True

    >>> de_message = set_translation(
    ...     message, de_pofile, "Und jetzt auf deutsch.")
    >>> spare_message = factory.makePOTMsgSet(
    ...     potemplate, "Unrelated notice #931", sequence=0)
    >>> nl_spare = set_translation(spare_message, nl_pofile, "Bericht 931")
    >>> de_spare = set_translation(spare_message, de_pofile, "Nachricht 931")

    >>> print_pofile_contents(nl_pofile)
    Bericht 931
    Maar dan in het Nederlands.

    >>> print_pofile_contents(de_pofile)
    Nachricht 931
    Und jetzt auf deutsch.

    >>> options = [
    ...     '-v',
    ...     '--submitter=%s' % nl_message.submitter.id,
    ...     '--reviewer=%s' % nl_message.reviewer.id,
    ...     '--id=%s' % str(1),
    ...     '--id=%s' % str(nl_message.id),
    ...     '--id=%s' % str(2),
    ...     '--potemplate=%s' % str(potemplate.id),
    ...     '--not-language',
    ...     '--language=%s' % 'de',
    ...     '--is-current-ubuntu=%s' % 'false',
    ...     '--is-current-upstream=%s' % 'true',
    ...     '--msgid=%s' % "My goose is undercooked.",
    ...     '--origin=%s' % 'ROSETTAWEB',
    ...     '--force',
    ...     ]

    >>> Store.of(potemplate).commit()
    >>> (returncode, out, err) = run_script(script, args=options)

    # We're going to inspect these POFiles later; make sure we're not
    # gazing at an old cached copy from before the removal.
    >>> Store.of(nl_pofile).flush()

    >>> returncode
    0

The long list of matching options we gave above indicated exactly 1
message.

    >>> print err
    WARNING Deleting messages currently in use:
    WARNING Message ... is a current translation in upstream
    DEBUG Sample of messages to be deleted follows.
    DEBUG   [message] [unmasks]
    DEBUG   ...       --
    INFO  Deleting 1 message(s).

Combining the --language-code option with --not-language inverts the
language match: delete messages in any language except the given one.
This can be useful in cases where files with the same translator are
uploaded for several incorrect languages.

In this case, the only other language to delete from is Dutch.  We see
the same messages as before, minus one Dutch one.

    >>> print_pofile_contents(nl_pofile)
    Bericht 931

    >>> print_pofile_contents(de_pofile)
    Nachricht 931
    Und jetzt auf deutsch.


== Dry runs ==

Deleting messages is scary.  You should not do it lightly.  The script
has a --dry-run option that stops it from committing its changes to the
database.

    >>> (returncode, out, err) = run_script(script, [
    ...     '-v',
    ...     '--potemplate=%s' % de_pofile.potemplate.id,
    ...     '--force',
    ...     '--dry-run',
    ...     ])

    >>> returncode
    0

    >>> out
    ''

    >>> print err
    WARNING Safety override in effect.  Deleting translations for template ...
    INFO    Dry run only.  Not really deleting.
    WARNING Deleting messages currently in use:
    WARNING Message ... is a current translation in upstream
    WARNING Message ... is a current translation in upstream
    WARNING Message ... is a current translation in upstream
    DEBUG   Sample of messages to be deleted follows.
    DEBUG   [message] [unmasks]
    DEBUG   ...       --
    DEBUG   ...       --
    DEBUG   ...       --
    INFO    Deleting 3 message(s).

The "deleted" messages are still there.

    >>> print_pofile_contents(de_pofile)
    Nachricht 931
    Und jetzt auf deutsch.