1
# Copyright (C) 2011 Canonical Ltd
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.
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.
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
17
"""Merge logic for po_merge plugin."""
26
from bzrlib.lazy_import import lazy_import
27
lazy_import(globals(), """
41
config.option_registry.register(config.Option(
43
default='msgmerge -N "{other}" "{pot_file}" -C "{this}" -o "{result}"',
45
Command used to create a conflict-free .po file during merge.
47
The following parameters are provided by the hook:
48
``this`` is the ``.po`` file content before the merge in the current branch,
49
``other`` is the ``.po`` file content in the branch merged from,
50
``pot_file`` is the path to the ``.pot`` file corresponding to the ``.po``
52
``result`` is the path where ``msgmerge`` will output its result. The hook will
53
use the content of this file to produce the resulting ``.po`` file.
55
The command is invoked at the root of the working tree so all paths are
60
config.option_registry.register(config.Option(
61
'po_merge.po_files', default=[],
62
from_unicode=config.list_from_store,
63
help='List of globs the po_merge hook applies to.'))
66
config.option_registry.register(config.Option(
67
'po_merge.pot_file', default=[],
68
from_unicode=config.list_from_store,
69
help='List of ``.pot`` filenames related to ``po_merge.po_files``.'))
72
class PoMerger(merge.PerFileMerger):
73
"""Merge .po files."""
75
def __init__(self, merger):
76
super(merge.PerFileMerger, self).__init__(merger)
77
# config options are cached locally until config files are (see
78
# http://pad.lv/832042)
80
# FIXME: We use the branch config as there is no tree config
82
self.conf = merger.this_branch.get_config_stack()
83
# Which files are targeted by the hook
84
self.po_files = self.conf.get('po_merge.po_files')
85
# Which .pot file should be used
86
self.pot_file = self.conf.get('po_merge.pot_file')
87
self.command = self.conf.get('po_merge.command', expand=False)
88
# file_matches() will set the following for merge_text()
89
self.selected_po_file = None
90
self.selected_pot_file = None
92
def file_matches(self, params):
93
"""Return True if merge_matching should be called on this file."""
94
if not self.po_files or not self.pot_file:
95
# Return early if there is no options defined
98
po_path = self.get_filepath(params, self.merger.this_tree)
99
# Does the merged file match one of the globs
100
for idx, glob in enumerate(self.po_files):
101
if fnmatch.fnmatch(po_path, glob):
106
# Do we have the corresponding .pot file
108
pot_path = self.pot_file[idx]
110
trace.note('po_merge.po_files and po_merge.pot_file mismatch'
113
if self.merger.this_tree.has_filename(pot_path):
114
self.selected_pot_file = pot_path
115
self.selected_po_file = po_path
116
# FIXME: I can't find a way to know if the .pot file has conflicts
117
# *during* the merge itself. So either the actual content on disk
118
# is fine and msgmerge will work OR it's not and it will
119
# fail. Conversely, either the result is ok for the user and he's
120
# happy OR the user needs to resolve the conflicts in the .pot file
121
# and use remerge. -- vila 2011-11-24
125
def _invoke(self, command):
126
proc = subprocess.Popen(cmdline.split(command),
127
# FIXME: cwd= ? -- vila 2011-11-24
128
stdout=subprocess.PIPE,
129
stderr=subprocess.PIPE,
130
stdin=subprocess.PIPE)
131
out, err = proc.communicate()
132
return proc.returncode, out, err
134
def merge_matching(self, params):
135
return self.merge_text(params)
137
def merge_text(self, params):
138
"""Calls msgmerge when .po files conflict.
140
This requires a valid .pot file to reconcile both sides.
142
# Create tmp files with the 'this' and 'other' content
143
tmpdir = tempfile.mkdtemp(prefix='po_merge')
145
env['this'] = osutils.pathjoin(tmpdir, 'this')
146
env['other'] = osutils.pathjoin(tmpdir, 'other')
147
env['result'] = osutils.pathjoin(tmpdir, 'result')
148
env['pot_file'] = self.selected_pot_file
150
with osutils.open_file(env['this'], 'wb') as f:
151
f.writelines(params.this_lines)
152
with osutils.open_file(env['other'], 'wb') as f:
153
f.writelines(params.other_lines)
154
command = self.conf.expand_options(self.command, env)
155
retcode, out, err = self._invoke(command)
156
with osutils.open_file(env['result']) as f:
157
# FIXME: To avoid the list() construct below which means the
158
# whole 'result' file is kept in memory, there may be a way to
159
# use an iterator that will close the file when it's done, but
160
# there is still the issue of removing the tmp dir...
162
return 'success', list(f.readlines())
164
osutils.rmtree(tmpdir)
165
return 'not applicable', []