1 """build_mo command for setup.py"""
3 from distutils
import log
4 from distutils
.command
.build
import build
5 from distutils
.core
import Command
6 from distutils
.dep_util
import newer
7 from distutils
.spawn
import find_executable
12 class build_mo(Command
):
13 """Subcommand of build command: build_mo"""
15 description
= 'compile po files to mo files'
19 # - short name (None if no short name),
21 user_options
= [('build-dir=', 'd', 'Directory to build locale files'),
22 ('output-base=', 'o', 'mo-files base name'),
23 ('source-dir=', None, 'Directory with sources po files'),
24 ('force', 'f', 'Force creation of mo files'),
25 ('lang=', None, 'Comma-separated list of languages '
28 boolean_options
= ['force']
30 def initialize_options(self
):
32 self
.output_base
= None
33 self
.source_dir
= None
37 def finalize_options(self
):
38 self
.set_undefined_options('build', ('force', 'force'))
39 self
.prj_name
= self
.distribution
.get_name()
40 if self
.build_dir
is None:
41 self
.build_dir
= os
.path
.join('share', 'locale')
42 if not self
.output_base
:
43 self
.output_base
= self
.prj_name
or 'messages'
44 if self
.source_dir
is None:
45 self
.source_dir
= 'po'
48 re_po
= re
.compile(r
'^(?:%s-)?([a-zA-Z_]+)\.po$' % self
.prj_name
)
50 re_po
= re
.compile(r
'^([a-zA-Z_]+)\.po$')
52 for i
in os
.listdir(self
.source_dir
):
55 self
.lang
.append(mo
.group(1))
57 self
.lang
= [i
.strip() for i
in self
.lang
.split(',') if i
.strip()]
60 """Run msgfmt for each language"""
64 if find_executable('msgfmt') is None:
65 log
.warn("GNU gettext msgfmt utility not found!")
66 log
.warn("Skip compiling po files.")
70 if find_executable('msginit') is None:
71 log
.warn("GNU gettext msginit utility not found!")
72 log
.warn("Skip creating English PO file.")
74 log
.info('Creating English PO file...')
75 pot
= (self
.prj_name
or 'messages') + '.pot'
77 en_po
= '%s-en.po' % self
.prj_name
80 self
.spawn(['msginit',
83 '-i', os
.path
.join(self
.source_dir
, pot
),
84 '-o', os
.path
.join(self
.source_dir
, en_po
),
87 basename
= self
.output_base
88 if not basename
.endswith('.mo'):
93 po_prefix
= self
.prj_name
+ '-'
94 for lang
in self
.lang
:
95 po
= os
.path
.join(self
.source_dir
, lang
+ '.po')
96 if not os
.path
.isfile(po
):
97 po
= os
.path
.join(self
.source_dir
, po_prefix
+ lang
+ '.po')
98 dir_
= os
.path
.join(self
.build_dir
, lang
, 'LC_MESSAGES')
100 mo
= os
.path
.join(dir_
, basename
)
101 if self
.force
or newer(po
, mo
):
102 log
.info('Compile: %s -> %s' % (po
, mo
))
103 self
.spawn(['msgfmt', '-o', mo
, po
])
106 build
.sub_commands
.insert(0, ('build_mo', None))