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."""
19
from __future__ import absolute_import
27
from bzrlib.lazy_import import lazy_import
28
lazy_import(globals(), """
42
command_option = config.Option(
44
default='msgmerge -N "{other}" "{pot_file}" -C "{this}" -o "{result}"',
46
Command used to create a conflict-free .po file during merge.
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``
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.
56
All paths are absolute.
60
po_dirs_option = config.ListOption(
61
'po_merge.po_dirs', default='po,debian/po',
62
help='List of dirs containing .po files that the hook applies to.')
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``.')
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``.')
74
class PoMerger(merge.PerFileMerger):
75
"""Merge .po files."""
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)
82
# FIXME: We use the branch config as there is no tree config
84
self.conf = merger.this_branch.get_config_stack()
85
# Which dirs are targeted by the hook
86
self.po_dirs = self.conf.get('po_merge.po_dirs')
87
# Which files are targeted by the hook
88
self.po_glob = self.conf.get('po_merge.po_glob')
89
# Which .pot file should be used
90
self.pot_glob = self.conf.get('po_merge.pot_glob')
91
self.command = self.conf.get('po_merge.command', expand=False)
92
# file_matches() will set the following for merge_text()
93
self.pot_file_abspath = None
94
trace.mutter('PoMerger created')
96
def file_matches(self, params):
97
"""Return True if merge_matching should be called on this file."""
98
if not self.po_dirs or not self.command:
99
# Return early if there is no options defined
102
po_path = self.get_filepath(params, self.merger.this_tree)
103
for po_dir in self.po_dirs:
104
glob = osutils.pathjoin(po_dir, self.po_glob)
105
if fnmatch.fnmatch(po_path, glob):
106
trace.mutter('po %s matches: %s' % (po_path, glob))
109
trace.mutter('PoMerger did not match for %s and %s'
110
% (self.po_dirs, self.po_glob))
112
# Do we have the corresponding .pot file
113
for inv_entry in self.merger.this_tree.list_files(from_dir=po_dir,
115
trace.mutter('inv_entry: %r' % (inv_entry,))
116
pot_name, pot_file_id = inv_entry[0], inv_entry[3]
117
if fnmatch.fnmatch(pot_name, self.pot_glob):
118
relpath = osutils.pathjoin(po_dir, pot_name)
119
self.pot_file_abspath = self.merger.this_tree.abspath(relpath)
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.
127
trace.mutter('will msgmerge %s using %s'
128
% (po_path, self.pot_file_abspath))
133
def _invoke(self, command):
134
trace.mutter('Will msgmerge: %s' % (command,))
135
# We use only absolute paths so we don't care about the cwd
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
143
def merge_matching(self, params):
144
return self.merge_text(params)
146
def merge_text(self, params):
147
"""Calls msgmerge when .po files conflict.
149
This requires a valid .pot file to reconcile both sides.
151
# Create tmp files with the 'this' and 'other' content
152
tmpdir = tempfile.mkdtemp(prefix='po_merge')
154
env['this'] = osutils.pathjoin(tmpdir, 'this')
155
env['other'] = osutils.pathjoin(tmpdir, 'other')
156
env['result'] = osutils.pathjoin(tmpdir, 'result')
157
env['pot_file'] = self.pot_file_abspath
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...
171
return 'success', list(f.readlines())
173
osutils.rmtree(tmpdir)
174
return 'not applicable', []