1 """build_pot 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
import log
8 from distutils
.core
import Command
9 from distutils
.errors
import DistutilsOptionError
11 from . import build_util
14 class build_pot(Command
):
15 """Distutils command build_pot"""
17 description
= 'extract strings from python sources for translation'
21 # - short name (None if no short name),
23 user_options
= [('build-dir=', 'd', 'Directory to put POT file'),
24 ('output=', 'o', 'POT filename'),
26 'Comma-separated list of languages to update po-files'),
27 ('no-lang', 'N', "Don't update po-files"),
28 ('english', 'E', 'Regenerate English PO file')]
29 user_options
= build_util
.stringify_options(user_options
)
30 boolean_options
= build_util
.stringify_list(['no-lang', 'english'])
32 def initialize_options(self
):
39 def finalize_options(self
):
40 if self
.build_dir
is None:
43 self
.output
= (self
.distribution
.get_name() or 'messages') + '.pot'
44 if self
.lang
is not None:
45 self
.lang
= [i
.strip() for i
in self
.lang
.split(',') if i
.strip()]
46 if self
.lang
and self
.no_lang
:
47 raise DistutilsOptionError(
48 "You can't use options "
49 "--lang=XXX and --no-lang in the same time.")
52 """Run xgettext for project sources"""
53 # project name based on `name` argument in setup() call
54 prj_name
= self
.distribution
.get_name()
56 if self
.build_dir
!= '.':
57 fullname
= os
.path
.join(self
.build_dir
, self
.output
)
59 fullname
= self
.output
60 log
.info('Generate POT file: ' + fullname
)
61 if not os
.path
.isdir(self
.build_dir
):
62 log
.info('Make directory: ' + self
.build_dir
)
63 os
.makedirs(self
.build_dir
)
73 '--output-dir', self
.build_dir
,
74 '--output', self
.output
,
76 cmd
.extend(glob
.glob('bin/git-*'))
77 cmd
.extend(glob
.glob('share/git-cola/bin/git-*'))
78 cmd
.extend(glob
.glob('cola/*.py'))
79 cmd
.extend(glob
.glob('cola/*/*.py'))
83 # regenerate english PO
85 log
.info('Regenerating English PO file...')
87 en_po
= prj_name
+ '-' + 'en.po'
94 '--input', os
.path
.join(self
.build_dir
, self
.output
),
95 '--output-file', os
.path
.join(self
.build_dir
, en_po
)])
96 # search and update all po-files
99 for po
in glob
.glob(os
.path
.join(self
.build_dir
, '*.po')):
100 if self
.lang
is not None:
101 po_lang
= os
.path
.splitext(os
.path
.basename(po
))[0]
102 if prj_name
and po_lang
.startswith(prj_name
+'-'):
103 po_lang
= po_lang
[5:]
104 if po_lang
not in self
.lang
:
112 '--output-file', new_po
,
114 # force LF line-endings
115 log
.info('%s --> %s' % (new_po
, po
))
116 _force_LF(new_po
, po
)
120 def _force_LF(src
, dst
=None):
130 f
.write(build_util
.encode(content
))