~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to scripts/rosetta/check-distroseries-translations-diffs.py

  • Committer: Steve Kowalik
  • Date: 2011-08-07 04:05:52 UTC
  • mto: This revision was merged to the branch mainline in revision 13626.
  • Revision ID: stevenk@ubuntu.com-20110807040552-mwnxo0flmhvl35e8
Correct the notification based on review comments, and remove request{,ed}
from the function names, switching to create{,d}.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python -S
 
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
 
 
6
import _pythonpath
 
7
import sys
 
8
 
 
9
from optparse import OptionParser
 
10
from zope.component import getUtility
 
11
 
 
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
 
16
 
 
17
 
 
18
def parse_options(args):
 
19
    """Parse a set of command line options.
 
20
 
 
21
    Return an optparse.Values object.
 
22
    """
 
23
    parser = OptionParser()
 
24
    parser.add_option("-d", "--distribution", dest="distro",
 
25
        default='ubuntu',
 
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.")
 
29
 
 
30
    logger_options(parser)
 
31
 
 
32
    (options, args) = parser.parse_args(args)
 
33
 
 
34
    return options
 
35
 
 
36
def compare_translations(orig_distroseries, dest_distroseries):
 
37
 
 
38
    from difflib import unified_diff
 
39
 
 
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))
 
46
 
 
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')
 
54
        if len(output) > 0:
 
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')
 
69
            if len(output) > 0:
 
70
                return u'%s is different than its parent %s:\n%s' % (
 
71
                    new_pofile.title, old_pofile.title, output)
 
72
    return None
 
73
 
 
74
def main(argv):
 
75
    options = parse_options(argv[1:])
 
76
 
 
77
    logger_object = logger(options, 'check')
 
78
 
 
79
    # Setup zcml machinery to be able to use getUtility
 
80
    execute_zcml_for_scripts()
 
81
    ztm = initZopeless()
 
82
 
 
83
    distribution = getUtility(IDistributionSet)[options.distro]
 
84
    release = distribution[options.release]
 
85
 
 
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...')
 
91
 
 
92
 
 
93
if __name__ == '__main__':
 
94
    main(sys.argv)