Merge branch 'merge-no-commit'
[git-cola.git] / extras / build_pot.py
blob054858bd6d50d754fb848f72cc78c5e0653a087e
1 """build_pot command for setup.py"""
3 import os
4 import glob
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'
15 # List of options:
16 # - long name,
17 # - short name (None if no short name),
18 # - help string.
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):
29 self.build_dir = None
30 self.output = None
31 self.lang = None
32 self.no_lang = False
33 self.english = False
35 def finalize_options(self):
36 if self.build_dir is None:
37 self.build_dir = 'po'
38 if not self.output:
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):
47 f = open(src, 'rU')
48 try:
49 content = f.read()
50 finally:
51 f.close()
52 if dst is None:
53 dst = src
54 f = open(dst, 'wb')
55 try:
56 f.write(content)
57 finally:
58 f.close()
60 def run(self):
61 """Run xgettext for project sources"""
62 # project name based on `name` argument in setup() call
63 prj_name = self.distribution.get_name()
64 # output file
65 if self.build_dir != '.':
66 fullname = os.path.join(self.build_dir, self.output)
67 else:
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',
74 '--keyword=N_',
75 '-p', self.build_dir,
76 '-o', self.output] +
77 glob.glob('cola/*.py') +
78 glob.glob('cola/*/*.py'))
79 self._force_LF(fullname)
80 # regenerate english PO
81 if self.english:
82 log.info('Regenerating English PO file...')
83 if prj_name:
84 en_po = prj_name + '-' + 'en.po'
85 else:
86 en_po = 'en.po'
87 self.spawn(['msginit',
88 '--no-translator',
89 '-l', 'en',
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
94 if self.no_lang:
95 return
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:
102 continue
103 new_po = po + ".new"
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)
109 os.unlink(new_po)