1
# -*- coding: utf-8 -*-
3
# Copyright (C) 2007 Lukáš Lalinský <lalinsky@gmail.com>
4
# Copyright (C) 2007,2009 Alexander Belchenko <bialix@ukr.net>
5
# Copyright 2011 Canonical Ltd.
7
# This program is free software; you can redistribute it and/or
8
# modify it under the terms of the GNU General Public License
9
# as published by the Free Software Foundation; either version 2
10
# of the License, or (at your option) any later version.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public License
18
# along with this program; if not, write to the Free Software
19
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
# This code is bring from bzr-explorer and modified for bzr.
23
"""build_mo command for setup.py"""
25
from distutils import log
26
from distutils.command.build import build
27
from distutils.core import Command
28
from distutils.dep_util import newer
29
from distutils.spawn import find_executable
35
class build_mo(Command):
36
"""Subcommand of build command: build_mo"""
38
description = 'compile po files to mo files'
42
# - short name (None if no short name),
44
user_options = [('build-dir=', 'd', 'Directory to build locale files'),
45
('output-base=', 'o', 'mo-files base name'),
46
('source-dir=', None, 'Directory with sources po files'),
47
('force', 'f', 'Force creation of mo files'),
48
('lang=', None, 'Comma-separated list of languages '
52
boolean_options = ['force']
54
def initialize_options(self):
56
self.output_base = None
57
self.source_dir = None
61
def finalize_options(self):
62
self.set_undefined_options('build', ('force', 'force'))
63
self.prj_name = self.distribution.get_name()
64
if self.build_dir is None:
65
self.build_dir = 'bzrlib/locale'
66
if not self.output_base:
67
self.output_base = self.prj_name or 'messages'
68
if self.source_dir is None:
69
self.source_dir = 'po'
72
re_po = re.compile(r'^(?:%s-)?([a-zA-Z_]+)\.po$' % self.prj_name)
74
re_po = re.compile(r'^([a-zA-Z_]+)\.po$')
76
for i in os.listdir(self.source_dir):
79
self.lang.append(mo.group(1))
81
self.lang = [i.strip() for i in self.lang.split(',') if i.strip()]
84
"""Run msgfmt for each language"""
89
if find_executable('msginit') is None:
90
log.warn("GNU gettext msginit utility not found!")
91
log.warn("Skip creating English PO file.")
93
log.info('Creating English PO file...')
94
pot = (self.prj_name or 'messages') + '.pot'
96
en_po = '%s-en.po' % self.prj_name
99
self.spawn(['msginit',
102
'-i', os.path.join(self.source_dir, pot),
103
'-o', os.path.join(self.source_dir, en_po),
106
basename = self.output_base
107
if not basename.endswith('.mo'):
112
po_prefix = self.prj_name + '-'
113
for lang in self.lang:
114
po = os.path.join('po', lang + '.po')
115
if not os.path.isfile(po):
116
po = os.path.join('po', po_prefix + lang + '.po')
117
dir_ = os.path.join(self.build_dir, lang, 'LC_MESSAGES')
119
mo = os.path.join(dir_, basename)
120
if self.force or newer(po, mo):
121
log.info('Compile: %s -> %s' % (po, mo))
125
build.sub_commands.insert(0, ('build_mo', None))