1 """build_mo command for setup.py"""
2 # pylint: disable=attribute-defined-outside-init
3 # pylint: disable=no-name-in-module,import-error
4 from __future__
import absolute_import
, division
, unicode_literals
7 from distutils
.command
.build
import build
8 from distutils
.core
import Command
9 from distutils
.dep_util
import newer
10 from distutils
.spawn
import find_executable
11 from distutils
import log
13 from . import build_util
16 class build_mo(Command
):
17 """Subcommand of build command: build_mo"""
19 description
= 'compile po files to mo files'
23 # - short name (None if no short name),
26 ('build-dir=', 'd', 'Directory to build locale files'),
27 ('output-base=', 'o', 'mo-files base name'),
28 ('source-dir=', None, 'Directory with sources po files'),
29 ('force', 'f', 'Force creation of mo files'),
30 ('lang=', None, 'Comma-separated list of languages to process'),
32 user_options
= build_util
.stringify_options(user_options
)
33 boolean_options
= build_util
.stringify_list(['force'])
35 def initialize_options(self
):
37 self
.output_base
= None
38 self
.source_dir
= None
42 def finalize_options(self
):
43 self
.set_undefined_options('build', ('force', 'force'))
44 self
.prj_name
= self
.distribution
.get_name()
45 if self
.build_dir
is None:
46 self
.build_dir
= os
.path
.join('share', 'locale')
47 if not self
.output_base
:
48 self
.output_base
= self
.prj_name
or 'messages'
49 if self
.source_dir
is None:
50 self
.source_dir
= 'po'
54 r
'^(?:%s-)?([a-zA-Z_]+)\.po$' % self
.prj_name
)
56 re_po
= re
.compile(r
'^([a-zA-Z_]+)\.po$')
58 for i
in os
.listdir(self
.source_dir
):
61 self
.lang
.append(mo
.group(1))
63 self
.lang
= [i
.strip() for i
in self
.lang
.split(',') if i
.strip()]
66 """Run msgfmt for each language"""
70 if find_executable('msgfmt') is None:
71 log
.warn('GNU gettext msgfmt utility not found!')
72 log
.warn('Skip compiling po files.')
76 if find_executable('msginit') is None:
77 log
.warn('GNU gettext msginit utility not found!')
78 log
.warn('Skip creating English PO file.')
80 log
.info('Creating English PO file...')
81 pot
= (self
.prj_name
or 'messages') + '.pot'
83 en_po
= '%s-en.po' % self
.prj_name
91 '--input', os
.path
.join(self
.source_dir
, pot
),
92 '--output-file', os
.path
.join(self
.source_dir
, en_po
),
95 basename
= self
.output_base
96 if not basename
.endswith('.mo'):
101 po_prefix
= self
.prj_name
+ '-'
102 for lang
in self
.lang
:
103 po
= os
.path
.join(self
.source_dir
, lang
+ '.po')
104 if not os
.path
.isfile(po
):
105 po
= os
.path
.join(self
.source_dir
, po_prefix
+ lang
+ '.po')
106 dir_
= os
.path
.join(self
.build_dir
, lang
, 'LC_MESSAGES')
108 mo
= os
.path
.join(dir_
, basename
)
109 if self
.force
or newer(po
, mo
):
110 log
.info('Compile: %s -> %s' % (po
, mo
))
111 self
.spawn(['msgfmt', '--output-file', mo
, po
])
114 build
.sub_commands
.insert(0, ('build_mo', None))