~ubuntu-branches/debian/squeeze/python-distutils-extra/squeeze

« back to all changes in this revision

Viewing changes to DistUtilsExtra/command/build_i18n.py

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2008-03-18 21:59:43 UTC
  • mfrom: (1.1.7 hardy)
  • Revision ID: james.westby@ubuntu.com-20080318215943-5a7lc4qa90f1m21t
Tags: 1.91.2
* DistUtilsExtra/command/build_i18n.py: Do not merge PO files by default,
  since it is not related to building the package, should be done manually
  rather, and creates noise in revision control. Introduce a new option
  -m/--merge-po instead. Adapt doc/FAQ accordingly.
* debian/control: Update Vcs-*: fields.
* debian/copyright: Fix copyright statement and upstream URL.
* debian/changelog: Fix invalid email address to make lintian happy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""distutils_extra.command.build_i18n
 
2
 
 
3
Implements the Distutils 'build_i18n' command."""
 
4
 
 
5
import distutils
 
6
import glob
 
7
import os
 
8
import os.path
 
9
import re
 
10
import sys
 
11
import distutils.command.build
 
12
 
 
13
class build_i18n(distutils.cmd.Command):
 
14
 
 
15
    description = "integrate the gettext framework"
 
16
 
 
17
    user_options = [('desktop-files=', None, '.desktop.in files that '
 
18
                                             'should be merged'),
 
19
                    ('xml-files=', None, '.xml.in files that should be '
 
20
                                         'merged'),
 
21
                    ('schemas-files=', None, '.schemas.in files that '
 
22
                                             'should be merged'),
 
23
                    ('ba-files=', None, 'bonobo-activation files that '
 
24
                                        'should be merged'),
 
25
                    ('rfc822deb-files=', None, 'RFC822 files that should '
 
26
                                               'be merged'),
 
27
                    ('key-files=', None, '.key.in files that should be '
 
28
                                         'merged'),
 
29
                    ('domain=', 'd', 'gettext domain'),
 
30
                    ('merge-po', 'm', 'merge po files against template'),
 
31
                    ('po-dir=', 'p', 'directory that holds the i18n files'),
 
32
                    ('bug-contact=', None, 'contact address for msgid bugs')]
 
33
 
 
34
    boolean_options = ['merge-po']
 
35
 
 
36
    def initialize_options(self):
 
37
        self.desktop_files = []
 
38
        self.xml_files = []
 
39
        self.key_files = []
 
40
        self.schemas_files = []
 
41
        self.ba_files = []
 
42
        self.rfc822deb_files = []
 
43
        self.domain = None
 
44
        self.merge_po = False
 
45
        self.bug_contact = None
 
46
        self.po_dir = None
 
47
 
 
48
    def finalize_options(self):
 
49
        if self.domain is None:
 
50
            self.domain = self.distribution.metadata.name
 
51
        if self.po_dir is None:
 
52
            self.po_dir = "po"
 
53
 
 
54
    def run(self):
 
55
        """
 
56
        Update the language files, generate mo files and add them
 
57
        to the to be installed files
 
58
        """
 
59
        data_files = self.distribution.data_files
 
60
 
 
61
        if self.bug_contact is not None:
 
62
            os.environ["XGETTEXT_ARGS"] = "--msgid-bugs-address=%s " % \
 
63
                                          self.bug_contact
 
64
 
 
65
        # Print a warning if there is a Makefile that would overwrite our
 
66
        # values
 
67
        if os.path.exists("%s/Makefile" % self.po_dir):
 
68
            self.announce("""
 
69
WARNING: Intltool will use the values specified from the
 
70
         existing po/Makefile in favor of the vaules
 
71
         from setup.cfg.
 
72
         Remove the Makefile to avoid problems.""")
 
73
 
 
74
        # Update po(t) files and print a report
 
75
        # We have to change the working dir to the po dir for intltool
 
76
        cmd = ["intltool-update", (self.merge_po and "-r" or "-p"), "-g", self.domain]
 
77
        wd = os.getcwd()
 
78
        os.chdir(self.po_dir)
 
79
        self.spawn(cmd)
 
80
        os.chdir(wd)
 
81
 
 
82
        for po_file in glob.glob("%s/*.po" % self.po_dir):
 
83
            lang = os.path.basename(po_file[:-3])
 
84
            mo_dir =  os.path.join("build", "mo", lang, "LC_MESSAGES")
 
85
            mo_file = os.path.join(mo_dir, "%s.mo" % self.domain)
 
86
            if not os.path.exists(mo_dir):
 
87
                os.makedirs(mo_dir)
 
88
            cmd = ["msgfmt", po_file, "-o", mo_file]
 
89
            self.spawn(cmd)
 
90
 
 
91
            targetpath = os.path.join("share/locale", lang, "LC_MESSAGES")
 
92
            data_files.append((targetpath, (mo_file,)))
 
93
 
 
94
        # merge .in with translation
 
95
        for (option, switch) in ((self.xml_files, "-x"),
 
96
                                 (self.desktop_files, "-d"),
 
97
                                 (self.schemas_files, "-s"),
 
98
                                 (self.rfc822deb_files, "-r"),
 
99
                                 (self.ba_files, "-b"),
 
100
                                 (self.key_files, "-k"),):
 
101
            try:
 
102
                file_set = eval(option)
 
103
            except:
 
104
                continue
 
105
            for (target, files) in file_set:
 
106
                build_target = os.path.join("build", target)
 
107
                if not os.path.exists(build_target): 
 
108
                    os.makedirs(build_target)
 
109
                files_merged = []
 
110
                for file in files:
 
111
                    if file.endswith(".in"):
 
112
                        file_merged = os.path.basename(file[:-3])
 
113
                    else:
 
114
                        file_merged = os.path.basename(file)
 
115
                    file_merged = os.path.join(build_target, file_merged)
 
116
                    cmd = ["intltool-merge", switch, self.po_dir, file, 
 
117
                           file_merged]
 
118
                    self.spawn(cmd)
 
119
                    files_merged.append(file_merged)
 
120
                data_files.append((target, files_merged))
 
121
 
 
122
# class build