~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
#!/usr/bin/python -S
#
# Copyright 2009 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

import _pythonpath
import sys

from optparse import OptionParser
from zope.component import getUtility

from canonical.lp import initZopeless
from canonical.launchpad.interfaces import IDistributionSet
from canonical.launchpad.scripts import execute_zcml_for_scripts
from canonical.launchpad.scripts import logger, logger_options


def parse_options(args):
    """Parse a set of command line options.

    Return an optparse.Values object.
    """
    parser = OptionParser()
    parser.add_option("-d", "--distribution", dest="distro",
        default='ubuntu',
        help="The distribution we want to check.")
    parser.add_option("-r", "--release", dest="release",
        help="The distroseries that we want to check.")

    logger_options(parser)

    (options, args) = parser.parse_args(args)

    return options

def compare_translations(orig_distroseries, dest_distroseries):

    from difflib import unified_diff

    orig_templates = sorted(
        orig_distroseries.potemplates,
        key=lambda x: (x.name, x.sourcepackagename.name))
    dest_templates = sorted(
        dest_distroseries.potemplates,
        key=lambda x: (x.name, x.sourcepackagename.name))

    for i in range(len(orig_templates)):
        old_template = orig_templates[i]
        new_template = dest_templates[i]
        output = '\n'.join(list(unified_diff(
            old_template.export().split('\n'),
            new_template.export().split('\n'))))
        output = output.decode('UTF-8')
        if len(output) > 0:
            return u'%s is different than its parent %s:\n%s' % (
                new_template.title, old_template.title, output)
        for old_pofile in old_template.pofiles:
            new_pofile = new_template.getPOFileByLang(
                old_pofile.language.code)
            old_pofile_content = old_pofile.uncachedExport(
                    included_obsolete=False,
                    force_utf8=True).split('\n')
            new_pofile_content = new_pofile.uncachedExport(
                    included_obsolete=False,
                    force_utf8=True).split('\n')
            output = '\n'.join(list(unified_diff(
                old_pofile_content, new_pofile_content)))
            output = output.decode('UTF-8')
            if len(output) > 0:
                return u'%s is different than its parent %s:\n%s' % (
                    new_pofile.title, old_pofile.title, output)
    return None

def main(argv):
    options = parse_options(argv[1:])

    logger_object = logger(options, 'check')

    # Setup zcml machinery to be able to use getUtility
    execute_zcml_for_scripts()
    ztm = initZopeless()

    distribution = getUtility(IDistributionSet)[options.distro]
    release = distribution[options.release]

    logger_object.info('Starting...')
    output = compare_translations(release.parent_series, release)
    if output is not None:
        logger_object.error(output)
    logger_object.info('Done...')


if __name__ == '__main__':
    main(sys.argv)