git-cola 1.7.1.1
[git-cola.git] / extras / build_mo.py
blobb0c95abb4b525ed11c973aa5f9316565fb3b5b90
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
8 import os
9 import re
12 class build_mo(Command):
13 """Subcommand of build command: build_mo"""
15 description = 'compile po files to mo files'
17 # List of options:
18 # - long name,
19 # - short name (None if no short name),
20 # - help string.
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 '
26 'to process')]
28 boolean_options = ['force']
30 def initialize_options(self):
31 self.build_dir = None
32 self.output_base = None
33 self.source_dir = None
34 self.force = None
35 self.lang = 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'
46 if self.lang is None:
47 if self.prj_name:
48 re_po = re.compile(r'^(?:%s-)?([a-zA-Z_]+)\.po$' % self.prj_name)
49 else:
50 re_po = re.compile(r'^([a-zA-Z_]+)\.po$')
51 self.lang = []
52 for i in os.listdir(self.source_dir):
53 mo = re_po.match(i)
54 if mo:
55 self.lang.append(mo.group(1))
56 else:
57 self.lang = [i.strip() for i in self.lang.split(',') if i.strip()]
59 def run(self):
60 """Run msgfmt for each language"""
61 if not self.lang:
62 return
64 if find_executable('msgfmt') is None:
65 log.warn("GNU gettext msgfmt utility not found!")
66 log.warn("Skip compiling po files.")
67 return
69 if 'en' in self.lang:
70 if find_executable('msginit') is None:
71 log.warn("GNU gettext msginit utility not found!")
72 log.warn("Skip creating English PO file.")
73 else:
74 log.info('Creating English PO file...')
75 pot = (self.prj_name or 'messages') + '.pot'
76 if self.prj_name:
77 en_po = '%s-en.po' % self.prj_name
78 else:
79 en_po = 'en.po'
80 self.spawn(['msginit',
81 '--no-translator',
82 '-l', 'en',
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'):
89 basename += '.mo'
91 po_prefix = ''
92 if self.prj_name:
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')
99 self.mkpath(dir_)
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))