~bzr-pqm/bzr/bzr.dev

5830.3.1 by INADA Naoki
Add build_mo command to setup. It makes .mo files into bzrlib/<lang>/LC_MESSAGES/bzr.mo
1
# -*- coding: utf-8 -*-
2
#
6162.4.10 by Jonathan Riddell
add random meaningless characters as required by testsuite
3
# Copyright (C) 2007,2009,2011 Canonical Ltd.
5830.3.1 by INADA Naoki
Add build_mo command to setup. It makes .mo files into bzrlib/<lang>/LC_MESSAGES/bzr.mo
4
#
6162.4.9 by Jonathan Riddell
use header format consistent with rest of bzr. change copyright to canonical in line with copyright agreement
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
5830.3.1 by INADA Naoki
Add build_mo command to setup. It makes .mo files into bzrlib/<lang>/LC_MESSAGES/bzr.mo
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
6162.4.9 by Jonathan Riddell
use header format consistent with rest of bzr. change copyright to canonical in line with copyright agreement
17
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
5830.3.1 by INADA Naoki
Add build_mo command to setup. It makes .mo files into bzrlib/<lang>/LC_MESSAGES/bzr.mo
18
6162.4.9 by Jonathan Riddell
use header format consistent with rest of bzr. change copyright to canonical in line with copyright agreement
19
# This code is from bzr-explorer and modified for bzr.
5830.3.1 by INADA Naoki
Add build_mo command to setup. It makes .mo files into bzrlib/<lang>/LC_MESSAGES/bzr.mo
20
21
"""build_mo command for setup.py"""
22
23
from distutils import log
24
from distutils.core import Command
25
from distutils.dep_util import newer
26
from distutils.spawn import find_executable
27
import os
28
import re
29
30
31
class build_mo(Command):
32
    """Subcommand of build command: build_mo"""
33
34
    description = 'compile po files to mo files'
35
36
    # List of options:
37
    #   - long name,
38
    #   - short name (None if no short name),
39
    #   - help string.
40
    user_options = [('build-dir=', 'd', 'Directory to build locale files'),
41
                    ('output-base=', 'o', 'mo-files base name'),
42
                    ('source-dir=', None, 'Directory with sources po files'),
43
                    ('force', 'f', 'Force creation of mo files'),
44
                    ('lang=', None, 'Comma-separated list of languages '
45
                                    'to process'),
46
                   ]
47
48
    boolean_options = ['force']
49
50
    def initialize_options(self):
51
        self.build_dir = None
52
        self.output_base = None
53
        self.source_dir = None
54
        self.force = None
55
        self.lang = None
56
57
    def finalize_options(self):
58
        self.set_undefined_options('build', ('force', 'force'))
59
        self.prj_name = self.distribution.get_name()
60
        if self.build_dir is None:
61
            self.build_dir = 'bzrlib/locale'
62
        if not self.output_base:
63
            self.output_base = self.prj_name or 'messages'
64
        if self.source_dir is None:
65
            self.source_dir = 'po'
66
        if self.lang is None:
5830.3.7 by INADA Naoki
Create en translation automatically. Remove 'bzr-' prefex.
67
            re_po = re.compile(r'^([a-zA-Z_]+)\.po$')
5830.3.1 by INADA Naoki
Add build_mo command to setup. It makes .mo files into bzrlib/<lang>/LC_MESSAGES/bzr.mo
68
            self.lang = []
69
            for i in os.listdir(self.source_dir):
70
                mo = re_po.match(i)
71
                if mo:
72
                    self.lang.append(mo.group(1))
73
        else:
74
            self.lang = [i.strip() for i in self.lang.split(',') if i.strip()]
75
76
    def run(self):
77
        """Run msgfmt for each language"""
78
        if not self.lang:
79
            return
80
5830.3.9 by INADA Naoki
Don't use msgfmt.
81
        if find_executable('msgfmt') is None:
82
            log.warn("GNU gettext msgfmt utility not found!")
83
            log.warn("Skip compiling po files.")
84
            return
85
86
        if 'en' in self.lang:
87
            if find_executable('msginit') is None:
88
                log.warn("GNU gettext msginit utility not found!")
89
                log.warn("Skip creating English PO file.")
90
            else:
91
                log.info('Creating English PO file...')
92
                pot = (self.prj_name or 'messages') + '.pot'
93
                en_po = 'en.po'
94
                self.spawn(['msginit',
95
                    '--no-translator',
96
                    '-l', 'en',
97
                    '-i', os.path.join(self.source_dir, pot),
98
                    '-o', os.path.join(self.source_dir, en_po),
99
                    ])
100
5830.3.1 by INADA Naoki
Add build_mo command to setup. It makes .mo files into bzrlib/<lang>/LC_MESSAGES/bzr.mo
101
        basename = self.output_base
102
        if not basename.endswith('.mo'):
103
            basename += '.mo'
104
105
        for lang in self.lang:
106
            po = os.path.join('po', lang + '.po')
107
            if not os.path.isfile(po):
5830.3.7 by INADA Naoki
Create en translation automatically. Remove 'bzr-' prefex.
108
                po = os.path.join('po', lang + '.po')
5830.3.1 by INADA Naoki
Add build_mo command to setup. It makes .mo files into bzrlib/<lang>/LC_MESSAGES/bzr.mo
109
            dir_ = os.path.join(self.build_dir, lang, 'LC_MESSAGES')
110
            self.mkpath(dir_)
111
            mo = os.path.join(dir_, basename)
112
            if self.force or newer(po, mo):
113
                log.info('Compile: %s -> %s' % (po, mo))
5830.3.9 by INADA Naoki
Don't use msgfmt.
114
                self.spawn(['msgfmt', '-o', mo, po])
5830.3.1 by INADA Naoki
Add build_mo command to setup. It makes .mo files into bzrlib/<lang>/LC_MESSAGES/bzr.mo
115
116