1 """build_mo command for setup.py"""
2 # pylint: disable=attribute-defined-outside-init,import-error,no-name-in-module
3 from __future__
import absolute_import
, division
, print_function
, unicode_literals
6 from distutils
.core
import Command
7 from distutils
.dep_util
import newer
8 from distutils
.spawn
import find_executable
9 from distutils
import log
11 from . import build_util
14 class build_mo(Command
):
15 """Subcommand of build command: build_mo"""
17 description
= 'compile po files to mo files'
21 # - short name (None if no short name),
24 ('build-dir=', 'd', 'Directory to build locale files'),
25 ('output-base=', 'o', 'mo-files base name'),
26 ('source-dir=', None, 'Directory with sources po files'),
27 ('force', 'f', 'Force creation of mo files'),
28 ('lang=', None, 'Comma-separated list of languages to process'),
30 user_options
= build_util
.stringify_options(user_options
)
31 boolean_options
= build_util
.stringify_list(['force'])
33 def initialize_options(self
):
35 self
.output_base
= None
36 self
.source_dir
= None
40 def finalize_options(self
):
41 self
.set_undefined_options('build', ('force', 'force'))
42 self
.prj_name
= self
.distribution
.get_name()
43 if self
.build_dir
is None:
44 self
.build_dir
= os
.path
.join('share', 'locale')
45 if not self
.output_base
:
46 self
.output_base
= self
.prj_name
or 'messages'
47 if self
.source_dir
is None:
48 self
.source_dir
= 'po'
51 re_po
= re
.compile(r
'^(?:%s-)?([a-zA-Z_]+)\.po$' % self
.prj_name
)
53 re_po
= re
.compile(r
'^([a-zA-Z_]+)\.po$')
55 for i
in os
.listdir(self
.source_dir
):
58 self
.lang
.append(mo
.group(1))
60 self
.lang
= [i
.strip() for i
in self
.lang
.split(',') if i
.strip()]
63 """Run msgfmt for each language"""
67 if find_executable('msgfmt') is None:
68 log
.warn('GNU gettext msgfmt utility not found!')
69 log
.warn('Skip compiling po files.')
73 if find_executable('msginit') is None:
74 log
.warn('GNU gettext msginit utility not found!')
75 log
.warn('Skip creating English PO file.')
77 log
.info('Creating English PO file...')
78 pot
= (self
.prj_name
or 'messages') + '.pot'
80 en_po
= '%s-en.po' % self
.prj_name
91 os
.path
.join(self
.source_dir
, pot
),
93 os
.path
.join(self
.source_dir
, en_po
),
97 basename
= self
.output_base
98 if not basename
.endswith('.mo'):
103 po_prefix
= self
.prj_name
+ '-'
104 for lang
in self
.lang
:
105 po
= os
.path
.join(self
.source_dir
, lang
+ '.po')
106 if not os
.path
.isfile(po
):
107 po
= os
.path
.join(self
.source_dir
, po_prefix
+ lang
+ '.po')
108 dir_
= os
.path
.join(self
.build_dir
, lang
, 'LC_MESSAGES')
110 mo
= os
.path
.join(dir_
, basename
)
111 if self
.force
or newer(po
, mo
):
112 log
.info('Compile: %s -> %s' % (po
, mo
))
113 self
.spawn(['msgfmt', '--output-file', mo
, po
])