Debian Codegraph @

extras/import_po.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # QBrz - Qt frontend to Breezy commands
4 # Copyright (C) 2009 Alexander Belchenko <[email protected]>
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 """import PO-files from launchpad-export.tar.gz (extra command for setup.py)"""
21
22 from distutils import log
23 from distutils.core import Command
24 from distutils.spawn import find_executable
25
26 from .en_po import regenerate_en
27
28
29 class import_po(Command):
30 """Distutils command for import PO files."""
31
32 description = 'import PO-files from launchpad-export.tar.gz'
33
34 # List of options:
35 # - long name,
36 # - short name (None if no short name),
37 # - help string.
38 user_options = [('tarball=', 'F', 'path to launchpad-export.tar.gz file'),
39 ('output-dir=', 'O', 'output directory (default: po)'),
40 ]
41 boolean_options = []
42
43 def initialize_options(self):
44 self.tarball = None
45 self.output_dir = None
46
47 def finalize_options(self):
48 if self.tarball is None:
49 self.tarball = 'launchpad-export.tar.gz'
50 if self.output_dir is None:
51 self.output_dir = 'po'
52
53 def run(self):
54 """Unpack tarball with PO-files."""
55 import os
56 import tarfile
57
58 log.info('Inspecting tarball...')
59 t = tarfile.open(self.tarball)
60 names = t.getnames()
61 # filter names
62 # names could have '$PROJECT/' or '/' prefix,
63 # also there is some strange entries as './' (wtf lp?)
64 # see https://bugs.launchpad.net/rosetta/+bug/148271
65 entries = [] # 2-tuple (archive name, output file name)
66 pot_file = None
67 for n in names:
68 if n == './':
69 continue
70 parts = os.path.split(n)
71 fn = parts[-1]
72 if not fn:
73 continue
74 if t.getmember(n).isfile():
75 entries.append((n, os.path.join(self.output_dir, fn)))
76 if fn.endswith('.pot'):
77 assert pot_file is None, "There is 2 POT files in archive!"
78 pot_file = fn
79 log.info('Extracting...')
80 for n, fn in entries:
81 log.info(' %s -> %s' % (n, fn))
82 ft = t.extractfile(n)
83 with open(fn, 'wb') as fd:
84 fd.write(ft.read())
85 if pot_file:
86 if find_executable('msginit') is None:
87 log.warn("GNU gettext msginit utility not found!")
88 log.warn("Skip creating English PO file.")
89 else:
90 log.info('Re-creating English PO file...')
91 prj_name = os.path.splitext(pot_file)[0]
92 regenerate_en(prj_name, self.output_dir, pot_file, self.spawn)
93 log.info('Done.')