1 """build_pot command for setup.py"""
5 from distutils
import log
6 from distutils
.core
import Command
7 from distutils
.errors
import DistutilsOptionError
10 class build_pot(Command
):
11 """Distutils command build_pot"""
13 description
= 'extract strings from python sources for translation'
17 # - short name (None if no short name),
19 user_options
= [('build-dir=', 'd', 'Directory to put POT file'),
20 ('output=', 'o', 'POT filename'),
21 ('lang=', None, 'Comma-separated list of languages '
22 'to update po-files'),
23 ('no-lang', 'N', "Don't update po-files"),
24 ('english', 'E', 'Regenerate English PO file'),
26 boolean_options
= ['no-lang', 'english']
28 def initialize_options(self
):
35 def finalize_options(self
):
36 if self
.build_dir
is None:
39 self
.output
= (self
.distribution
.get_name() or 'messages')+'.pot'
40 if self
.lang
is not None:
41 self
.lang
= [i
.strip() for i
in self
.lang
.split(',') if i
.strip()]
42 if self
.lang
and self
.no_lang
:
43 raise DistutilsOptionError("You can't use options "
44 "--lang=XXX and --no-lang in the same time.")
46 def _force_LF(self
, src
, dst
=None):
61 """Run xgettext for project sources"""
62 # project name based on `name` argument in setup() call
63 prj_name
= self
.distribution
.get_name()
65 if self
.build_dir
!= '.':
66 fullname
= os
.path
.join(self
.build_dir
, self
.output
)
68 fullname
= self
.output
69 log
.info('Generate POT file: ' + fullname
)
70 if not os
.path
.isdir(self
.build_dir
):
71 log
.info('Make directory: ' + self
.build_dir
)
72 os
.makedirs(self
.build_dir
)
73 self
.spawn(['xgettext',
77 glob
.glob('cola/*.py') +
78 glob
.glob('cola/*/*.py'))
79 self
._force
_LF
(fullname
)
80 # regenerate english PO
82 log
.info('Regenerating English PO file...')
84 en_po
= prj_name
+ '-' + 'en.po'
87 self
.spawn(['msginit',
90 '-i', os
.path
.join(self
.build_dir
, self
.output
),
91 '-o', os
.path
.join(self
.build_dir
, en_po
),
93 # search and update all po-files
96 for po
in glob
.glob(os
.path
.join(self
.build_dir
,'*.po')):
97 if self
.lang
is not None:
98 po_lang
= os
.path
.splitext(os
.path
.basename(po
))[0]
99 if prj_name
and po_lang
.startswith(prj_name
+'-'):
100 po_lang
= po_lang
[5:]
101 if po_lang
not in self
.lang
:
104 cmd
= "msgmerge %s %s -o %s" % (po
, fullname
, new_po
)
105 self
.spawn(cmd
.split())
106 # force LF line-endings
107 log
.info("%s --> %s" % (new_po
, po
))
108 self
._force
_LF
(new_po
, po
)