cmds: allow integers values for `line_numbers` in Edit
[git-cola.git] / extras / build_mo.py
blob691b191c59dead21fd5a5b5c277a91d5cb7f01c4
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
5 import os
6 import re
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'
21 # List of options:
22 # - long name,
23 # - short name (None if no short name),
24 # - help string.
25 user_options = [
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):
36 self.build_dir = None
37 self.output_base = None
38 self.source_dir = None
39 self.force = None
40 self.lang = 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'
51 if self.lang is None:
52 if self.prj_name:
53 re_po = re.compile(
54 r'^(?:%s-)?([a-zA-Z_]+)\.po$' % self.prj_name)
55 else:
56 re_po = re.compile(r'^([a-zA-Z_]+)\.po$')
57 self.lang = []
58 for i in os.listdir(self.source_dir):
59 mo = re_po.match(i)
60 if mo:
61 self.lang.append(mo.group(1))
62 else:
63 self.lang = [i.strip() for i in self.lang.split(',') if i.strip()]
65 def run(self):
66 """Run msgfmt for each language"""
67 if not self.lang:
68 return
70 if find_executable('msgfmt') is None:
71 log.warn('GNU gettext msgfmt utility not found!')
72 log.warn('Skip compiling po files.')
73 return
75 if 'en' in self.lang:
76 if find_executable('msginit') is None:
77 log.warn('GNU gettext msginit utility not found!')
78 log.warn('Skip creating English PO file.')
79 else:
80 log.info('Creating English PO file...')
81 pot = (self.prj_name or 'messages') + '.pot'
82 if self.prj_name:
83 en_po = '%s-en.po' % self.prj_name
84 else:
85 en_po = 'en.po'
86 self.spawn([
87 'msginit',
88 '--no-translator',
89 '--no-wrap',
90 '--locale', 'en',
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'):
97 basename += '.mo'
99 po_prefix = ''
100 if self.prj_name:
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')
107 self.mkpath(dir_)
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))