~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.22 by Karl Fogel
Add the copyright header block to the remaining .py files.
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
8486.12.1 by Celso Providelo
Fixing the 'fixable' scripts and adding a checker with the really-broken ones blacklisted.
6
# pylint: disable-msg=W0403
7
"""Remove all translations from upstream.
8
9
This script is useful to recover from breakages after importing bad
10
.po files like the one reported at #32610.
11
"""
12
9641.1.7 by Gary Poster
fix scripts to work
13
import _pythonpath
3405.1.1 by Carlos Perello Marin
Added a script to remove translations from upstream. It will help us to remove broken imports like the one done due bug #32610.
14
15
import sys
16
import logging
17
from optparse import OptionParser
8486.12.1 by Celso Providelo
Fixing the 'fixable' scripts and adding a checker with the really-broken ones blacklisted.
18
3405.1.1 by Carlos Perello Marin
Added a script to remove translations from upstream. It will help us to remove broken imports like the one done due bug #32610.
19
from zope.component import getUtility
20
21
from canonical.config import config
5084.14.80 by Jeroen Vermeulen
Removed POMsgSet.updateFlags() and replaced POMsgSet.updateReviewerInfo().
22
from canonical.database.constants import UTC_NOW
3405.1.1 by Carlos Perello Marin
Added a script to remove translations from upstream. It will help us to remove broken imports like the one done due bug #32610.
23
from canonical.lp import initZopeless
24
from canonical.launchpad.scripts import (
25
    execute_zcml_for_scripts, logger, logger_options)
8751.1.2 by Danilo Šegan
Update configure.zcml and interface calls.
26
from canonical.launchpad.interfaces import ILaunchpadCelebrities
27
from lp.registry.interfaces.product import IProductSet
28
from lp.registry.interfaces.distribution import IDistributionSet
29
from lp.registry.interfaces.distroseries import IDistroSeriesSet
30
from lp.registry.interfaces.sourcepackagename import (
31
    ISourcePackageNameSet)
32
from lp.translations.interfaces.potemplate import IPOTemplateSet
33
from lp.translations.interfaces.translationmessage import (
34
    RosettaTranslationOrigin)
35
36
3405.1.1 by Carlos Perello Marin
Added a script to remove translations from upstream. It will help us to remove broken imports like the one done due bug #32610.
37
38
logger_name = 'remove-upstream-translations'
39
40
def parse_options(args):
41
    """Parse a set of command line options.
42
43
    Return an optparse.Values object.
44
    """
45
    parser = OptionParser()
46
47
    parser.add_option("-p", "--product", dest="product",
48
        help="The product where we should look for translations.")
49
    parser.add_option("-s", "--series", dest="series",
50
        help="The product series where we should look for translations.")
51
    parser.add_option("-d", "--distro", dest="distro",
52
        help="The distribution where we should look for translations.")
4285.2.4 by Mark Shuttleworth
Test fixes with renamed distrorelease
53
    parser.add_option("-r", "--distroseries", dest="distroseries",
54
        help="The distribution series where we should look for translations."
3405.1.1 by Carlos Perello Marin
Added a script to remove translations from upstream. It will help us to remove broken imports like the one done due bug #32610.
55
        )
56
    parser.add_option("-n", "--sourcepackagename", dest="sourcepackagename",
57
        help="The distribution where we should look for translations.")
58
    parser.add_option("-t", "--potemplatename", dest="potemplatename",
59
        help="The PO Template name where we should look for translations.")
60
    parser.add_option("-l", "--language-code", dest="languagecode",
61
        help="The language code where we should look for translations.")
62
63
    # Add the verbose/quiet options.
64
    logger_options(parser)
65
66
    (options, args) = parser.parse_args(args)
67
68
    return options
69
11122.3.4 by Danilo Šegan
Get rid of the remaining variant usage.
70
def remove_upstream_entries(ztm, potemplates, lang_code=None):
3405.1.1 by Carlos Perello Marin
Added a script to remove translations from upstream. It will help us to remove broken imports like the one done due bug #32610.
71
    """Remove all translations that came from upstream.
72
73
    :arg ztm: Zope transaction manager.
74
    :arg potemplates: A set of potemplates that we should process.
75
    :arg lang_code: A string with a language code where we should do the
76
        removal.
77
78
    If lang_code is None, we process all available languages.
79
    """
80
81
    logger_object = logging.getLogger(logger_name)
82
83
    items_deleted = 0
3691.418.1 by Carlos Perello Marin
Fixed some remaining code that was not migrated in first place and updated other tests to reflect new API
84
    # All changes should be logged as done by Rosetta Expert team.
5228.1.1 by Christian Robottom Reis
Rename rosetta_expert to rosetta_experts.
85
    rosetta_experts = getUtility(ILaunchpadCelebrities).rosetta_experts
3691.418.1 by Carlos Perello Marin
Fixed some remaining code that was not migrated in first place and updated other tests to reflect new API
86
3405.1.1 by Carlos Perello Marin
Added a script to remove translations from upstream. It will help us to remove broken imports like the one done due bug #32610.
87
    for potemplate in potemplates:
88
        if lang_code is None:
3498.1.2 by Carlos Perello Marin
Fixed some issues with the order of some objects so we always get the same order. This should make tests pass with pqm
89
            pofiles = sorted(
90
                list(potemplate.pofiles),
11122.3.4 by Danilo Šegan
Get rid of the remaining variant usage.
91
                key=lambda p: p.language.code)
3405.1.1 by Carlos Perello Marin
Added a script to remove translations from upstream. It will help us to remove broken imports like the one done due bug #32610.
92
        else:
11122.3.4 by Danilo Šegan
Get rid of the remaining variant usage.
93
            pofile = potemplate.getPOFileByLang(lang_code)
3617.3.31 by Carlos Perello Marin
Fixed a bug raised by the new sampledata additions
94
            if pofile is None:
95
                pofiles = []
96
            else:
97
                pofiles = [pofile]
3405.1.1 by Carlos Perello Marin
Added a script to remove translations from upstream. It will help us to remove broken imports like the one done due bug #32610.
98
99
        for pofile in pofiles:
100
            logger_object.debug('Processing %s...' % pofile.title)
101
            pofile_items_deleted = 0
5084.14.66 by Jeroen Vermeulen
Retiring last_touched_pomsgset.
102
            for message in pofile.translation_messages:
3691.418.1 by Carlos Perello Marin
Fixed some remaining code that was not migrated in first place and updated other tests to reflect new API
103
                active_changed = False
5084.14.66 by Jeroen Vermeulen
Retiring last_touched_pomsgset.
104
                if message.origin == RosettaTranslationOrigin.SCM:
105
                    if message.is_current:
106
                        active_changed = True
107
                    message.destroySelf()
108
                    pofile_items_deleted += 1
3691.418.1 by Carlos Perello Marin
Fixed some remaining code that was not migrated in first place and updated other tests to reflect new API
109
                if active_changed:
5084.14.80 by Jeroen Vermeulen
Removed POMsgSet.updateFlags() and replaced POMsgSet.updateReviewerInfo().
110
                    message.pofile.date_changed = UTC_NOW
5228.1.1 by Christian Robottom Reis
Rename rosetta_expert to rosetta_experts.
111
                    message.pofile.lasttranslator = rosetta_experts
112
                    message.reviewer = rosetta_experts
5084.14.80 by Jeroen Vermeulen
Removed POMsgSet.updateFlags() and replaced POMsgSet.updateReviewerInfo().
113
                    message.date_reviewed = UTC_NOW
3691.418.1 by Carlos Perello Marin
Fixed some remaining code that was not migrated in first place and updated other tests to reflect new API
114
3405.1.1 by Carlos Perello Marin
Added a script to remove translations from upstream. It will help us to remove broken imports like the one done due bug #32610.
115
            items_deleted += pofile_items_deleted
116
            logger_object.debug(
3405.1.5 by Carlos Perello Marin
Applied review comments
117
                 'Removed %d submissions' % pofile_items_deleted)
3405.1.1 by Carlos Perello Marin
Added a script to remove translations from upstream. It will help us to remove broken imports like the one done due bug #32610.
118
            pofile.updateStatistics()
3405.1.9 by Carlos Perello Marin
More review comments added and test fixes
119
            ztm.commit()
3405.1.5 by Carlos Perello Marin
Applied review comments
120
121
    # We finished the removal process, is time to notify the amount of entries
122
    # that we removed.
123
    logger_object.debug(
124
        'Removed %d submissions in total.' % items_deleted)
3405.1.1 by Carlos Perello Marin
Added a script to remove translations from upstream. It will help us to remove broken imports like the one done due bug #32610.
125
5084.14.80 by Jeroen Vermeulen
Removed POMsgSet.updateFlags() and replaced POMsgSet.updateReviewerInfo().
126
3405.1.1 by Carlos Perello Marin
Added a script to remove translations from upstream. It will help us to remove broken imports like the one done due bug #32610.
127
def main(argv):
128
    options = parse_options(argv[1:])
129
    logger_object = logger(options, logger_name)
130
131
    execute_zcml_for_scripts()
7659.4.1 by Danilo Šegan
Clean-up config options and add a new one for debian import failures.
132
    ztm = initZopeless(dbuser=config.rosetta.admin_dbuser)
3405.1.1 by Carlos Perello Marin
Added a script to remove translations from upstream. It will help us to remove broken imports like the one done due bug #32610.
133
134
    product = None
135
    series = None
136
    distro = None
4285.2.4 by Mark Shuttleworth
Test fixes with renamed distrorelease
137
    distroseries = None
3405.1.1 by Carlos Perello Marin
Added a script to remove translations from upstream. It will help us to remove broken imports like the one done due bug #32610.
138
    sourcepackagename = None
139
    potemplatename = None
140
    language_code = None
141
    if options.product is not None:
142
        productset = getUtility(IProductSet)
143
        product = productset.getByName(options.product)
144
        if product is None:
145
            logger_object.error(
146
                'The %s product does not exist.' % options.product)
147
            return 1
148
149
    if options.series is not None:
150
        if product is None:
151
            logger_object.error(
152
                'You need to specify a product if you want to select a'
153
                ' productseries.')
154
            return 1
155
156
        series = product.getSeries(options.series)
157
        if series is None:
158
            logger_object.error(
159
                'The %s series does not exist inside %s product.' % (
160
                    options.series, options.product))
161
            return 1
162
163
    if options.distro is not None:
164
        if product is not None:
165
            logger_object.error(
166
                'You cannot mix distributions and products.')
167
            return 1
168
        distroset = getUtility(IDistributionSet)
169
        distro = distroset.getByName(options.distro)
170
        if distro is None:
171
            logger_object.error(
172
                'The %s distribution does not exist.' % options.distro)
173
            return 1
174
4285.2.4 by Mark Shuttleworth
Test fixes with renamed distrorelease
175
    if options.distroseries is not None:
3405.1.1 by Carlos Perello Marin
Added a script to remove translations from upstream. It will help us to remove broken imports like the one done due bug #32610.
176
        if distro is None:
177
            logger_object.error(
178
                'You need to specify a distribution if you want to select a'
179
                ' sourcepackagename.')
4285.2.4 by Mark Shuttleworth
Test fixes with renamed distrorelease
180
        distroseriesset = getUtility(IDistroSeriesSet)
181
        distroseries = distroseriesset.queryByName(
182
            distro, options.distroseries)
183
        if distroseries is None:
3405.1.1 by Carlos Perello Marin
Added a script to remove translations from upstream. It will help us to remove broken imports like the one done due bug #32610.
184
            logger_object.error(
4285.2.4 by Mark Shuttleworth
Test fixes with renamed distrorelease
185
                'The %s distribution does not exist.' % options.distroseries)
3405.1.1 by Carlos Perello Marin
Added a script to remove translations from upstream. It will help us to remove broken imports like the one done due bug #32610.
186
            return 1
187
188
    if options.sourcepackagename is not None:
4285.2.4 by Mark Shuttleworth
Test fixes with renamed distrorelease
189
        if distroseries is None:
3405.1.1 by Carlos Perello Marin
Added a script to remove translations from upstream. It will help us to remove broken imports like the one done due bug #32610.
190
            logger_object.error(
4285.2.4 by Mark Shuttleworth
Test fixes with renamed distrorelease
191
                'You need to specify a distribution series if you want to'
3405.1.1 by Carlos Perello Marin
Added a script to remove translations from upstream. It will help us to remove broken imports like the one done due bug #32610.
192
                ' select a sourcepackagename.')
193
            return 1
194
        sourcepackagenameset = getUtility(ISourcePackageNameSet)
195
        sourcepackagename = sourcepackagenameset.queryByName(
196
            options.sourcepackagename)
197
        if sourcepackagename is None:
198
            logger_object.error(
199
                'The %s sourcepackagename does not exist.' % (
200
                    options.sourcepackagename))
201
            return 1
202
203
    potemplateset = getUtility(IPOTemplateSet)
4285.2.4 by Mark Shuttleworth
Test fixes with renamed distrorelease
204
    if series is None and distroseries is None:
3405.1.1 by Carlos Perello Marin
Added a script to remove translations from upstream. It will help us to remove broken imports like the one done due bug #32610.
205
        if options.potemplatename is None:
206
            logger_object.warning('Nothing to do. Exiting...')
207
            return 0
208
        else:
3405.1.12 by Carlos Perello Marin
Applied more review comments
209
            potemplates = potemplateset.getAllByName(
210
                options.potemplatename)
3405.1.1 by Carlos Perello Marin
Added a script to remove translations from upstream. It will help us to remove broken imports like the one done due bug #32610.
211
    else:
3405.1.11 by Carlos Perello Marin
Tested an untested code path when selecting a concrete potemplatename. Includes a test
212
        potemplate_subset = potemplateset.getSubset(
4285.2.4 by Mark Shuttleworth
Test fixes with renamed distrorelease
213
            distroseries=distroseries, sourcepackagename=sourcepackagename,
3405.1.11 by Carlos Perello Marin
Tested an untested code path when selecting a concrete potemplatename. Includes a test
214
            productseries=series)
3405.1.1 by Carlos Perello Marin
Added a script to remove translations from upstream. It will help us to remove broken imports like the one done due bug #32610.
215
        if options.potemplatename is not None:
3405.1.11 by Carlos Perello Marin
Tested an untested code path when selecting a concrete potemplatename. Includes a test
216
            potemplate = potemplate_subset.getPOTemplateByName(
3405.1.1 by Carlos Perello Marin
Added a script to remove translations from upstream. It will help us to remove broken imports like the one done due bug #32610.
217
                options.potemplatename)
218
            potemplates = [potemplate]
3405.1.11 by Carlos Perello Marin
Tested an untested code path when selecting a concrete potemplatename. Includes a test
219
        else:
220
            # Get a list from the subset of potemplates to be able to do
221
            # transaction commits.
222
            potemplates = list(potemplate_subset)
3405.1.1 by Carlos Perello Marin
Added a script to remove translations from upstream. It will help us to remove broken imports like the one done due bug #32610.
223
11122.3.17 by Danilo Segan
Apply review comments from jtv as well.
224
    remove_upstream_entries(ztm, potemplates, options.languagecode)
3405.1.1 by Carlos Perello Marin
Added a script to remove translations from upstream. It will help us to remove broken imports like the one done due bug #32610.
225
226
if __name__ == '__main__':
227
    sys.exit(main(sys.argv))