~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/bzr_distutils.py

  • Committer: Tarmac
  • Author(s): Vincent Ladeuil
  • Date: 2017-01-30 14:42:05 UTC
  • mfrom: (6620.1.1 trunk)
  • Revision ID: tarmac-20170130144205-r8fh2xpmiuxyozpv
Merge  2.7 into trunk including fix for bug #1657238 [r=vila]

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# Copyright (C) 2007,2009,2011 Canonical Ltd.
 
4
#
 
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.
 
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
 
17
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
18
 
 
19
# This code is from bzr-explorer and modified for bzr.
 
20
 
 
21
"""build_mo command for setup.py"""
 
22
 
 
23
from __future__ import absolute_import
 
24
 
 
25
from distutils import log
 
26
from distutils.core import Command
 
27
from distutils.dep_util import newer
 
28
from distutils.spawn import find_executable
 
29
import os
 
30
import re
 
31
 
 
32
 
 
33
class build_mo(Command):
 
34
    """Subcommand of build command: build_mo"""
 
35
 
 
36
    description = 'compile po files to mo files'
 
37
 
 
38
    # List of options:
 
39
    #   - long name,
 
40
    #   - short name (None if no short name),
 
41
    #   - help string.
 
42
    user_options = [('build-dir=', 'd', 'Directory to build locale files'),
 
43
                    ('output-base=', 'o', 'mo-files base name'),
 
44
                    ('source-dir=', None, 'Directory with sources po files'),
 
45
                    ('force', 'f', 'Force creation of mo files'),
 
46
                    ('lang=', None, 'Comma-separated list of languages '
 
47
                                    'to process'),
 
48
                   ]
 
49
 
 
50
    boolean_options = ['force']
 
51
 
 
52
    def initialize_options(self):
 
53
        self.build_dir = None
 
54
        self.output_base = None
 
55
        self.source_dir = None
 
56
        self.force = None
 
57
        self.lang = None
 
58
 
 
59
    def finalize_options(self):
 
60
        self.set_undefined_options('build', ('force', 'force'))
 
61
        self.prj_name = self.distribution.get_name()
 
62
        if self.build_dir is None:
 
63
            self.build_dir = 'bzrlib/locale'
 
64
        if not self.output_base:
 
65
            self.output_base = self.prj_name or 'messages'
 
66
        if self.source_dir is None:
 
67
            self.source_dir = 'po'
 
68
        if self.lang is None:
 
69
            re_po = re.compile(r'^([a-zA-Z_]+)\.po$')
 
70
            self.lang = []
 
71
            for i in os.listdir(self.source_dir):
 
72
                mo = re_po.match(i)
 
73
                if mo:
 
74
                    self.lang.append(mo.group(1))
 
75
        else:
 
76
            self.lang = [i.strip() for i in self.lang.split(',') if i.strip()]
 
77
 
 
78
    def run(self):
 
79
        """Run msgfmt for each language"""
 
80
        if not self.lang:
 
81
            return
 
82
 
 
83
        if find_executable('msgfmt') is None:
 
84
            log.warn("GNU gettext msgfmt utility not found!")
 
85
            log.warn("Skip compiling po files.")
 
86
            return
 
87
 
 
88
        if 'en' in self.lang:
 
89
            if find_executable('msginit') is None:
 
90
                log.warn("GNU gettext msginit utility not found!")
 
91
                log.warn("Skip creating English PO file.")
 
92
            else:
 
93
                log.info('Creating English PO file...')
 
94
                pot = (self.prj_name or 'messages') + '.pot'
 
95
                en_po = 'en.po'
 
96
                self.spawn(['msginit',
 
97
                    '--no-translator',
 
98
                    '-l', 'en',
 
99
                    '-i', os.path.join(self.source_dir, pot),
 
100
                    '-o', os.path.join(self.source_dir, en_po),
 
101
                    ])
 
102
 
 
103
        basename = self.output_base
 
104
        if not basename.endswith('.mo'):
 
105
            basename += '.mo'
 
106
 
 
107
        for lang in self.lang:
 
108
            po = os.path.join('po', lang + '.po')
 
109
            if not os.path.isfile(po):
 
110
                po = os.path.join('po', lang + '.po')
 
111
            dir_ = os.path.join(self.build_dir, lang, 'LC_MESSAGES')
 
112
            self.mkpath(dir_)
 
113
            mo = os.path.join(dir_, basename)
 
114
            if self.force or newer(po, mo):
 
115
                log.info('Compile: %s -> %s' % (po, mo))
 
116
                self.spawn(['msgfmt', '-o', mo, po])
 
117
 
 
118