/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to tools/build_mo.py

  • Committer: Martin Pool
  • Date: 2011-05-20 14:46:02 UTC
  • mto: This revision was merged to the branch mainline in revision 5923.
  • Revision ID: mbp@canonical.com-20110520144602-bqli0t6dj01gl0pv
Various pyflakes import fixes.

Some modules were used for subclassing or at module load time, so there is no
point loading them lazily.

Some were not imported when they should be.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# Copyright (C) 2007 Lukáš Lalinský <lalinsky@gmail.com>
 
4
# Copyright (C) 2007,2009 Alexander Belchenko <bialix@ukr.net>
 
5
# Copyright 2011 Canonical Ltd.
 
6
#
 
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.
 
11
#
 
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.
 
16
#
 
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.
 
20
 
 
21
# This code is bring from bzr-explorer and modified for bzr.
 
22
 
 
23
"""build_mo command for setup.py"""
 
24
 
 
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
 
30
import os
 
31
import re
 
32
 
 
33
import msgfmt
 
34
 
 
35
class build_mo(Command):
 
36
    """Subcommand of build command: build_mo"""
 
37
 
 
38
    description = 'compile po files to mo files'
 
39
 
 
40
    # List of options:
 
41
    #   - long name,
 
42
    #   - short name (None if no short name),
 
43
    #   - help string.
 
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 '
 
49
                                    'to process'),
 
50
                   ]
 
51
 
 
52
    boolean_options = ['force']
 
53
 
 
54
    def initialize_options(self):
 
55
        self.build_dir = None
 
56
        self.output_base = None
 
57
        self.source_dir = None
 
58
        self.force = None
 
59
        self.lang = None
 
60
 
 
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'
 
70
        if self.lang is None:
 
71
            if self.prj_name:
 
72
                re_po = re.compile(r'^(?:%s-)?([a-zA-Z_]+)\.po$' % self.prj_name)
 
73
            else:
 
74
                re_po = re.compile(r'^([a-zA-Z_]+)\.po$')
 
75
            self.lang = []
 
76
            for i in os.listdir(self.source_dir):
 
77
                mo = re_po.match(i)
 
78
                if mo:
 
79
                    self.lang.append(mo.group(1))
 
80
        else:
 
81
            self.lang = [i.strip() for i in self.lang.split(',') if i.strip()]
 
82
 
 
83
    def run(self):
 
84
        """Run msgfmt for each language"""
 
85
        if not self.lang:
 
86
            return
 
87
 
 
88
        basename = self.output_base
 
89
        if not basename.endswith('.mo'):
 
90
            basename += '.mo'
 
91
 
 
92
        po_prefix = ''
 
93
        if self.prj_name:
 
94
            po_prefix = self.prj_name + '-'
 
95
        for lang in self.lang:
 
96
            po = os.path.join('po', lang + '.po')
 
97
            if not os.path.isfile(po):
 
98
                po = os.path.join('po', po_prefix + lang + '.po')
 
99
            dir_ = os.path.join(self.build_dir, lang, 'LC_MESSAGES')
 
100
            self.mkpath(dir_)
 
101
            mo = os.path.join(dir_, basename)
 
102
            if self.force or newer(po, mo):
 
103
                log.info('Compile: %s -> %s' % (po, mo))
 
104
                msgfmt.make(po,  mo)
 
105
 
 
106
 
 
107
build.sub_commands.insert(0, ('build_mo', None))