| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
om> |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 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 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 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 |
|
| 78 |
prj_name = self.distribution.get_name() |
| 79 |
|
| 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 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 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 |
|
| 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 |
|
| 124 |
log.info("%s --> %s" % (new_po, po)) |
| 125 |
self._force_LF(new_po, po) |
| 126 |
os.unlink(new_po) |