6282.3.1
by Vincent Ladeuil
First cut at a working plugin to avoid conflicts in .po files by shelling out to msgmerge. |
1 |
# Copyright (C) 2011 Canonical Ltd
|
2 |
#
|
|
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
#
|
|
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
#
|
|
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
16 |
||
6379.6.7
by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear. |
17 |
"""Merge logic for po_merge plugin."""
|
18 |
||
6379.6.3
by Jelmer Vernooij
Use absolute_import. |
19 |
from __future__ import absolute_import |
20 |
||
6282.3.1
by Vincent Ladeuil
First cut at a working plugin to avoid conflicts in .po files by shelling out to msgmerge. |
21 |
from bzrlib import ( |
22 |
config, |
|
23 |
merge, |
|
24 |
)
|
|
25 |
||
26 |
||
27 |
from bzrlib.lazy_import import lazy_import |
|
28 |
lazy_import(globals(), """ |
|
29 |
import fnmatch
|
|
30 |
import subprocess
|
|
31 |
import tempfile
|
|
32 |
import sys
|
|
33 |
||
34 |
from bzrlib import (
|
|
35 |
cmdline,
|
|
36 |
osutils,
|
|
37 |
trace,
|
|
38 |
)
|
|
39 |
""") |
|
40 |
||
41 |
||
6282.3.14
by Vincent Ladeuil
Use lazy registration for plugin options to set a good example. |
42 |
command_option = config.Option( |
43 |
'po_merge.command', |
|
44 |
default='msgmerge -N "{other}" "{pot_file}" -C "{this}" -o "{result}"', |
|
45 |
help='''\ |
|
46 |
Command used to create a conflict-free .po file during merge.
|
|
47 |
||
48 |
The following parameters are provided by the hook:
|
|
49 |
``this`` is the ``.po`` file content before the merge in the current branch,
|
|
50 |
``other`` is the ``.po`` file content in the branch merged from,
|
|
51 |
``pot_file`` is the path to the ``.pot`` file corresponding to the ``.po``
|
|
52 |
file being merged.
|
|
53 |
``result`` is the path where ``msgmerge`` will output its result. The hook will
|
|
54 |
use the content of this file to produce the resulting ``.po`` file.
|
|
55 |
||
56 |
All paths are absolute.
|
|
57 |
''') |
|
58 |
||
59 |
||
6385.1.1
by Vincent Ladeuil
Stores allow Stacks to control when values are quoted/unquoted |
60 |
po_dirs_option = config.ListOption( |
6282.3.14
by Vincent Ladeuil
Use lazy registration for plugin options to set a good example. |
61 |
'po_merge.po_dirs', default='po,debian/po', |
62 |
help='List of dirs containing .po files that the hook applies to.') |
|
63 |
||
64 |
||
65 |
po_glob_option = config.Option( |
|
66 |
'po_merge.po_glob', default='*.po', |
|
67 |
help='Glob matching all ``.po`` files in one of ``po_merge.po_dirs``.') |
|
68 |
||
69 |
pot_glob_option = config.Option( |
|
70 |
'po_merge.pot_glob', default='*.pot', |
|
71 |
help='Glob matching the ``.pot`` file in one of ``po_merge.po_dirs``.') |
|
72 |
||
73 |
||
6282.3.1
by Vincent Ladeuil
First cut at a working plugin to avoid conflicts in .po files by shelling out to msgmerge. |
74 |
class PoMerger(merge.PerFileMerger): |
75 |
"""Merge .po files."""
|
|
76 |
||
77 |
def __init__(self, merger): |
|
78 |
super(merge.PerFileMerger, self).__init__(merger) |
|
79 |
# config options are cached locally until config files are (see
|
|
80 |
# http://pad.lv/832042)
|
|
81 |
||
82 |
# FIXME: We use the branch config as there is no tree config
|
|
83 |
# -- vila 2011-11-23
|
|
84 |
self.conf = merger.this_branch.get_config_stack() |
|
6282.3.3
by Vincent Ladeuil
Change option scheme so users mainly have to deal with po_merge.po_dirs only. |
85 |
# Which dirs are targeted by the hook
|
86 |
self.po_dirs = self.conf.get('po_merge.po_dirs') |
|
6282.3.1
by Vincent Ladeuil
First cut at a working plugin to avoid conflicts in .po files by shelling out to msgmerge. |
87 |
# Which files are targeted by the hook
|
6282.3.3
by Vincent Ladeuil
Change option scheme so users mainly have to deal with po_merge.po_dirs only. |
88 |
self.po_glob = self.conf.get('po_merge.po_glob') |
6282.3.1
by Vincent Ladeuil
First cut at a working plugin to avoid conflicts in .po files by shelling out to msgmerge. |
89 |
# Which .pot file should be used
|
6282.3.3
by Vincent Ladeuil
Change option scheme so users mainly have to deal with po_merge.po_dirs only. |
90 |
self.pot_glob = self.conf.get('po_merge.pot_glob') |
6282.3.1
by Vincent Ladeuil
First cut at a working plugin to avoid conflicts in .po files by shelling out to msgmerge. |
91 |
self.command = self.conf.get('po_merge.command', expand=False) |
92 |
# file_matches() will set the following for merge_text()
|
|
6282.3.10
by Vincent Ladeuil
Use an absolute path for {pot_file} so we don't have to care about the cwd for msgmerge. |
93 |
self.pot_file_abspath = None |
6282.3.3
by Vincent Ladeuil
Change option scheme so users mainly have to deal with po_merge.po_dirs only. |
94 |
trace.mutter('PoMerger created') |
6282.3.1
by Vincent Ladeuil
First cut at a working plugin to avoid conflicts in .po files by shelling out to msgmerge. |
95 |
|
96 |
def file_matches(self, params): |
|
97 |
"""Return True if merge_matching should be called on this file."""
|
|
6282.3.3
by Vincent Ladeuil
Change option scheme so users mainly have to deal with po_merge.po_dirs only. |
98 |
if not self.po_dirs or not self.command: |
6282.3.1
by Vincent Ladeuil
First cut at a working plugin to avoid conflicts in .po files by shelling out to msgmerge. |
99 |
# Return early if there is no options defined
|
100 |
return False |
|
6282.3.3
by Vincent Ladeuil
Change option scheme so users mainly have to deal with po_merge.po_dirs only. |
101 |
po_dir = None |
6282.3.1
by Vincent Ladeuil
First cut at a working plugin to avoid conflicts in .po files by shelling out to msgmerge. |
102 |
po_path = self.get_filepath(params, self.merger.this_tree) |
6282.3.3
by Vincent Ladeuil
Change option scheme so users mainly have to deal with po_merge.po_dirs only. |
103 |
for po_dir in self.po_dirs: |
104 |
glob = osutils.pathjoin(po_dir, self.po_glob) |
|
6282.3.1
by Vincent Ladeuil
First cut at a working plugin to avoid conflicts in .po files by shelling out to msgmerge. |
105 |
if fnmatch.fnmatch(po_path, glob): |
6282.3.3
by Vincent Ladeuil
Change option scheme so users mainly have to deal with po_merge.po_dirs only. |
106 |
trace.mutter('po %s matches: %s' % (po_path, glob)) |
6282.3.1
by Vincent Ladeuil
First cut at a working plugin to avoid conflicts in .po files by shelling out to msgmerge. |
107 |
break
|
6282.3.3
by Vincent Ladeuil
Change option scheme so users mainly have to deal with po_merge.po_dirs only. |
108 |
else: |
109 |
trace.mutter('PoMerger did not match for %s and %s' |
|
110 |
% (self.po_dirs, self.po_glob)) |
|
6282.3.1
by Vincent Ladeuil
First cut at a working plugin to avoid conflicts in .po files by shelling out to msgmerge. |
111 |
return False |
112 |
# Do we have the corresponding .pot file
|
|
6282.3.3
by Vincent Ladeuil
Change option scheme so users mainly have to deal with po_merge.po_dirs only. |
113 |
for inv_entry in self.merger.this_tree.list_files(from_dir=po_dir, |
114 |
recursive=False): |
|
6282.3.10
by Vincent Ladeuil
Use an absolute path for {pot_file} so we don't have to care about the cwd for msgmerge. |
115 |
trace.mutter('inv_entry: %r' % (inv_entry,)) |
116 |
pot_name, pot_file_id = inv_entry[0], inv_entry[3] |
|
6282.3.3
by Vincent Ladeuil
Change option scheme so users mainly have to deal with po_merge.po_dirs only. |
117 |
if fnmatch.fnmatch(pot_name, self.pot_glob): |
6282.3.10
by Vincent Ladeuil
Use an absolute path for {pot_file} so we don't have to care about the cwd for msgmerge. |
118 |
relpath = osutils.pathjoin(po_dir, pot_name) |
119 |
self.pot_file_abspath = self.merger.this_tree.abspath(relpath) |
|
6282.3.3
by Vincent Ladeuil
Change option scheme so users mainly have to deal with po_merge.po_dirs only. |
120 |
# FIXME: I can't find an easy way to know if the .pot file has
|
121 |
# conflicts *during* the merge itself. So either the actual
|
|
122 |
# content on disk is fine and msgmerge will work OR it's not
|
|
123 |
# and it will fail. Conversely, either the result is ok for the
|
|
124 |
# user and he's happy OR the user needs to resolve the
|
|
125 |
# conflicts in the .pot file and use remerge.
|
|
126 |
# -- vila 2011-11-24
|
|
6282.3.8
by Vincent Ladeuil
We don't need select_po_file, we directly use the contents provided by merge for {this} and {other}. |
127 |
trace.mutter('will msgmerge %s using %s' |
6282.3.10
by Vincent Ladeuil
Use an absolute path for {pot_file} so we don't have to care about the cwd for msgmerge. |
128 |
% (po_path, self.pot_file_abspath)) |
6282.3.3
by Vincent Ladeuil
Change option scheme so users mainly have to deal with po_merge.po_dirs only. |
129 |
return True |
130 |
else: |
|
6282.3.1
by Vincent Ladeuil
First cut at a working plugin to avoid conflicts in .po files by shelling out to msgmerge. |
131 |
return False |
132 |
||
133 |
def _invoke(self, command): |
|
6282.3.3
by Vincent Ladeuil
Change option scheme so users mainly have to deal with po_merge.po_dirs only. |
134 |
trace.mutter('Will msgmerge: %s' % (command,)) |
6282.3.10
by Vincent Ladeuil
Use an absolute path for {pot_file} so we don't have to care about the cwd for msgmerge. |
135 |
# We use only absolute paths so we don't care about the cwd
|
6282.3.1
by Vincent Ladeuil
First cut at a working plugin to avoid conflicts in .po files by shelling out to msgmerge. |
136 |
proc = subprocess.Popen(cmdline.split(command), |
137 |
stdout=subprocess.PIPE, |
|
138 |
stderr=subprocess.PIPE, |
|
139 |
stdin=subprocess.PIPE) |
|
140 |
out, err = proc.communicate() |
|
141 |
return proc.returncode, out, err |
|
142 |
||
143 |
def merge_matching(self, params): |
|
144 |
return self.merge_text(params) |
|
145 |
||
146 |
def merge_text(self, params): |
|
147 |
"""Calls msgmerge when .po files conflict.
|
|
148 |
||
149 |
This requires a valid .pot file to reconcile both sides.
|
|
150 |
"""
|
|
151 |
# Create tmp files with the 'this' and 'other' content
|
|
152 |
tmpdir = tempfile.mkdtemp(prefix='po_merge') |
|
153 |
env = {} |
|
154 |
env['this'] = osutils.pathjoin(tmpdir, 'this') |
|
155 |
env['other'] = osutils.pathjoin(tmpdir, 'other') |
|
156 |
env['result'] = osutils.pathjoin(tmpdir, 'result') |
|
6282.3.10
by Vincent Ladeuil
Use an absolute path for {pot_file} so we don't have to care about the cwd for msgmerge. |
157 |
env['pot_file'] = self.pot_file_abspath |
6282.3.1
by Vincent Ladeuil
First cut at a working plugin to avoid conflicts in .po files by shelling out to msgmerge. |
158 |
try: |
159 |
with osutils.open_file(env['this'], 'wb') as f: |
|
160 |
f.writelines(params.this_lines) |
|
161 |
with osutils.open_file(env['other'], 'wb') as f: |
|
162 |
f.writelines(params.other_lines) |
|
163 |
command = self.conf.expand_options(self.command, env) |
|
164 |
retcode, out, err = self._invoke(command) |
|
165 |
with osutils.open_file(env['result']) as f: |
|
166 |
# FIXME: To avoid the list() construct below which means the
|
|
167 |
# whole 'result' file is kept in memory, there may be a way to
|
|
168 |
# use an iterator that will close the file when it's done, but
|
|
169 |
# there is still the issue of removing the tmp dir...
|
|
170 |
# -- vila 2011-11-24
|
|
171 |
return 'success', list(f.readlines()) |
|
172 |
finally: |
|
173 |
osutils.rmtree(tmpdir) |
|
174 |
return 'not applicable', [] |