cmds: word-wrap the list of files when removing files
[git-cola.git] / extras / build_pot.py
blobcd5c1d13191e48afa58673325875d0671709fa88
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
5 import os
6 import glob
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'
19 # List of options:
20 # - long name,
21 # - short name (None if no short name),
22 # - help string.
23 user_options = [('build-dir=', 'd', 'Directory to put POT file'),
24 ('output=', 'o', 'POT filename'),
25 ('lang=', None,
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):
33 self.build_dir = None
34 self.output = None
35 self.lang = None
36 self.no_lang = False
37 self.english = False
39 def finalize_options(self):
40 if self.build_dir is None:
41 self.build_dir = 'po'
42 if not self.output:
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.")
51 def _force_LF(self, src, dst=None):
52 f = open(src, 'rU')
53 try:
54 content = f.read()
55 finally:
56 f.close()
57 if dst is None:
58 dst = src
59 f = open(dst, 'wb')
60 try:
61 f.write(build_util.encode(content))
62 finally:
63 f.close()
65 def run(self):
66 """Run xgettext for project sources"""
67 # project name based on `name` argument in setup() call
68 prj_name = self.distribution.get_name()
69 # output file
70 if self.build_dir != '.':
71 fullname = os.path.join(self.build_dir, self.output)
72 else:
73 fullname = self.output
74 log.info('Generate POT file: ' + fullname)
75 if not os.path.isdir(self.build_dir):
76 log.info('Make directory: ' + self.build_dir)
77 os.makedirs(self.build_dir)
79 cmd = [
80 'xgettext',
81 '--language=Python',
82 '--keyword=N_',
83 '--no-wrap',
84 '--no-location',
85 '--omit-header',
86 '--sort-output',
87 '--output-dir', self.build_dir,
88 '--output', self.output,
90 cmd.extend(glob.glob('bin/git-*'))
91 cmd.extend(glob.glob('share/git-cola/bin/git-*'))
92 cmd.extend(glob.glob('cola/*.py'))
93 cmd.extend(glob.glob('cola/*/*.py'))
94 self.spawn(cmd)
96 self._force_LF(fullname)
97 # regenerate english PO
98 if self.english:
99 log.info('Regenerating English PO file...')
100 if prj_name:
101 en_po = prj_name + '-' + 'en.po'
102 else:
103 en_po = 'en.po'
104 self.spawn([
105 'msginit',
106 '--no-translator',
107 '--locale', 'en',
108 '--input', os.path.join(self.build_dir, self.output),
109 '--output-file', os.path.join(self.build_dir, en_po)])
110 # search and update all po-files
111 if self.no_lang:
112 return
113 for po in glob.glob(os.path.join(self.build_dir, '*.po')):
114 if self.lang is not None:
115 po_lang = os.path.splitext(os.path.basename(po))[0]
116 if prj_name and po_lang.startswith(prj_name+'-'):
117 po_lang = po_lang[5:]
118 if po_lang not in self.lang:
119 continue
120 new_po = po + '.new'
121 self.spawn([
122 'msgmerge',
123 '--no-location',
124 '--no-wrap',
125 '--sort-output',
126 '--output-file', new_po,
127 po, fullname])
128 # force LF line-endings
129 log.info('%s --> %s' % (new_po, po))
130 self._force_LF(new_po, po)
131 os.unlink(new_po)