Debian Codegraph @

extras/build_mo.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # QBrz - Qt frontend to Breezy commands
4 # Copyright (C) 2007 Lukáš Lalinský <[email protected]om>
5 # Copyright (C) 2007,2009 Alexander Belchenko <[email protected]>
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 # RJLRJL: breezy converted
21
22 """build_mo command for setup.py"""
23
24 from distutils import log
25 from distutils.command.build import build
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 from .en_po import regenerate_en
33
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 to process'),
49 ]
50
51 boolean_options = ['force']
52
53 def initialize_options(self):
54 self.build_dir = None
55 self.output_base = None
56 self.source_dir = None
57 self.force = None
58 self.lang = None
59
60 def finalize_options(self):
61 self.set_undefined_options('build', ('force', 'force'))
62 self.prj_name = self.distribution.get_name()
63 if self.build_dir is None:
64 self.build_dir = 'locale'
65 if not self.output_base:
66 self.output_base = self.prj_name or 'messages'
67 if self.source_dir is None:
68 self.source_dir = 'po'
69 if self.lang is None:
70 if self.prj_name:
71 re_po = re.compile(r'^(?:%s-)?([a-zA-Z_]+)\.po$' % self.prj_name)
72 else:
73 re_po = re.compile(r'^([a-zA-Z_]+)\.po$')
74 self.lang = []
75 for i in os.listdir(self.source_dir):
76 mo = re_po.match(i)
77 if mo:
78 self.lang.append(mo.group(1))
79 else:
80 self.lang = [i.strip() for i in self.lang.split(',') if i.strip()]
81
82 def run(self):
83 """Run msgfmt for each language"""
84 if not self.lang:
85 return
86
87 if find_executable('msgfmt') is None:
88 log.warn("GNU gettext msgfmt utility not found!")
89 log.warn("Skip compiling po files.")
90 return
91
92 # RJLRJL: TODO: by-passed for now: we don't want to shred what we have
93 # if 'en' in self.lang:
94 # if find_executable('msginit') is None:
95 # log.warn("GNU gettext msginit utility not found!")
96 # log.warn("Skip creating English PO file.")
97 # else:
98 # log.info('Creating English PO file...')
99 # pot = (self.prj_name or 'messages') + '.pot'
100 # regenerate_en(self.prj_name, self.source_dir, pot, self.spawn)
101
102 basename = self.output_base
103 if not basename.endswith('.mo'):
104 basename += '.mo'
105
106 po_prefix = ''
107 if self.prj_name:
108 po_prefix = self.prj_name + '-'
109 for lang in self.lang:
110 po = os.path.join('po', lang + '.po')
111 if not os.path.isfile(po):
112 po = os.path.join('po', po_prefix + lang + '.po')
113 dir_ = os.path.join(self.build_dir, lang, 'LC_MESSAGES')
114 self.mkpath(dir_)
115 mo = os.path.join(dir_, basename)
116 if self.force or newer(po, mo):
117 log.info('Compile: %s -> %s' % (po, mo))
118 self.spawn(['msgfmt', '-o', mo, po])
119
120
121 build.sub_commands.insert(0, ('build_mo', None))