3
# Copyright 2009 Canonical Ltd. This software is licensed under the
4
# GNU Affero General Public License version 3 (see the file LICENSE).
9
from optparse import OptionParser
10
from zope.component import getUtility
12
from canonical.lp import initZopeless
13
from lp.registry.interfaces.distribution import IDistributionSet
14
from canonical.launchpad.scripts import execute_zcml_for_scripts
15
from canonical.launchpad.scripts import logger, logger_options
18
def parse_options(args):
19
"""Parse a set of command line options.
21
Return an optparse.Values object.
23
parser = OptionParser()
24
parser.add_option("-d", "--distribution", dest="distro",
26
help="The distribution we want to check.")
27
parser.add_option("-r", "--release", dest="release",
28
help="The distroseries that we want to check.")
30
logger_options(parser)
32
(options, args) = parser.parse_args(args)
36
def compare_translations(orig_distroseries, dest_distroseries):
38
from difflib import unified_diff
40
orig_templates = sorted(
41
orig_distroseries.potemplates,
42
key=lambda x: (x.name, x.sourcepackagename.name))
43
dest_templates = sorted(
44
dest_distroseries.potemplates,
45
key=lambda x: (x.name, x.sourcepackagename.name))
47
for i in range(len(orig_templates)):
48
old_template = orig_templates[i]
49
new_template = dest_templates[i]
50
output = '\n'.join(list(unified_diff(
51
old_template.export().split('\n'),
52
new_template.export().split('\n'))))
53
output = output.decode('UTF-8')
55
return u'%s is different than its parent %s:\n%s' % (
56
new_template.title, old_template.title, output)
57
for old_pofile in old_template.pofiles:
58
new_pofile = new_template.getPOFileByLang(
59
old_pofile.language.code)
60
old_pofile_content = old_pofile.uncachedExport(
61
included_obsolete=False,
62
force_utf8=True).split('\n')
63
new_pofile_content = new_pofile.uncachedExport(
64
included_obsolete=False,
65
force_utf8=True).split('\n')
66
output = '\n'.join(list(unified_diff(
67
old_pofile_content, new_pofile_content)))
68
output = output.decode('UTF-8')
70
return u'%s is different than its parent %s:\n%s' % (
71
new_pofile.title, old_pofile.title, output)
75
options = parse_options(argv[1:])
77
logger_object = logger(options, 'check')
79
# Setup zcml machinery to be able to use getUtility
80
execute_zcml_for_scripts()
83
distribution = getUtility(IDistributionSet)[options.distro]
84
release = distribution[options.release]
86
logger_object.info('Starting...')
87
output = compare_translations(release.parent_series, release)
88
if output is not None:
89
logger_object.error(output)
90
logger_object.info('Done...')
93
if __name__ == '__main__':