Debian Codegraph @

extras/build_pot.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # QBrz - Qt frontend to Breezy commands
4 # Copyright (C) 2007 Lukáš Lalinský <[email protected]om>
5 # Copyright (C) 2007,2009 Alexander Belchenko <[email protected]>
6 #
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation; either version 2
10 # of the License, or (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
21 """build_pot command for setup.py"""
22
23 import glob
24 from distutils import log
25 from distutils.core import Command
26 from distutils.errors import DistutilsOptionError
27
28 from .en_po import regenerate_en
29
30
31 class build_pot(Command):
32 """Distutils command build_pot"""
33
34 description = 'extract strings from python sources for translation'
35
36 # List of options:
37 # - long name,
38 # - short name (None if no short name),
39 # - help string.
40 user_options = [('build-dir=', 'd', 'Directory to put POT file'),
41 ('output=', 'o', 'POT filename'),
42 ('lang=', None, 'Comma-separated list of languages to update po-files'),
43 ('no-lang', 'N', "Don't update po-files"),
44 ('english', 'E', 'Regenerate English PO file'),
45 ]
46 boolean_options = ['no-lang', 'english']
47
48 def initialize_options(self):
49 self.build_dir = None
50 self.output = None
51 self.lang = None
52 self.no_lang = False
53 self.english = False
54
55 def finalize_options(self):
56 if self.build_dir is None:
57 self.build_dir = 'po'
58 if not self.output:
59 self.output = (self.distribution.get_name() or 'messages')+ '.pot'
60 if self.lang is not None:
61 self.lang = [i.strip() for i in self.lang.split(',') if i.strip()]
62 if self.lang and self.no_lang:
63 raise DistutilsOptionError("You can't use options --lang=XXX and --no-lang in the same time.")
64
65 def _force_LF(self, src, dst=None):
66 with open(src, 'rU') as f:
67 content = f.read()
68 if dst is None:
69 dst = src
70 with open(dst, 'wb') as f:
71 f.write(content)
72
73 def run(self):
74 """Run xgettext for project sources"""
75 import glob
76 import os
77 # project name based on `name` argument in setup() call
78 prj_name = self.distribution.get_name()
79 # output file
80 if self.build_dir != '.':
81 fullname = os.path.join(self.build_dir, self.output)
82 else:
83 fullname = self.output
84 log.info('Generate POT file: ' + fullname)
85 if not os.path.isdir(self.build_dir):
86 log.info('Make directory: ' + self.build_dir)
87 os.makedirs(self.build_dir)
88
89
90 # RJLRJL: TODO: bypassed for now, to not shred what we have
91 # self.spawn(['xgettext',
92 # '--keyword=N_',
93 # '-p', self.build_dir,
94 # '-o', self.output,
95 # '__init__.py',
96 # ] + glob.glob('lib/*.py') + glob.glob('lib/widgets/*.py')
97 # )
98 # self._force_LF(fullname)
99
100 # regenerate english PO
101 print('self.english is ', self.english, 'project is ', prj_name)
102 if self.english:
103 log.info('Regenerating English PO file...')
104 log.info(' project: {0}, build_dir {1}, output {2}'.format(prj_name, self.build_dir, self.output))
105 print('Regenerating English PO file...')
106 print(' project: {0}, build_dir {1}, output {2}'.format(prj_name, self.build_dir, self.output))
107 regenerate_en(prj_name, self.build_dir, self.output, self.spawn)
108 # search and update all po-files
109 if self.no_lang:
110 print('No language: stopping')
111 return
112 for po in glob.glob(os.path.join(self.build_dir,'*.po')):
113 print('\tworking on ', 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 cmd = "msgmerge %s %s -o %s" % (po, fullname, new_po)
122 self.spawn(cmd.split())
123 # force LF line-endings
124 log.info("%s --> %s" % (new_po, po))
125 self._force_LF(new_po, po)
126 os.unlink(new_po)