~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/plugins/po_merge/po_merge.py

  • Committer: John Arbash Meinel
  • Author(s): Mark Hammond
  • Date: 2008-09-09 17:02:21 UTC
  • mto: This revision was merged to the branch mainline in revision 3697.
  • Revision ID: john@arbash-meinel.com-20080909170221-svim3jw2mrz0amp3
An updated transparent icon for bzr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
17
 
"""Merge logic for po_merge plugin."""
18
 
 
19
 
from __future__ import absolute_import
20
 
 
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
 
 
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
 
 
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.')
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
 
 
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()
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')
95
 
 
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
100
 
            return False
101
 
        po_dir = None
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))
107
 
                break
108
 
        else:
109
 
            trace.mutter('PoMerger did not match for %s and %s'
110
 
                         % (self.po_dirs, self.po_glob))
111
 
            return False
112
 
        # Do we have the corresponding .pot file
113
 
        for inv_entry in self.merger.this_tree.list_files(from_dir=po_dir,
114
 
                                                          recursive=False):
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.
126
 
                # -- vila 2011-11-24
127
 
                trace.mutter('will msgmerge %s using %s'
128
 
                             % (po_path, self.pot_file_abspath))
129
 
                return True
130
 
        else:
131
 
            return False
132
 
 
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
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')
157
 
        env['pot_file'] = self.pot_file_abspath
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', []