/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
19
from ... import (
6282.3.1 by Vincent Ladeuil
First cut at a working plugin to avoid conflicts in .po files by shelling out to msgmerge.
20
    config,
21
    merge,
22
    )
23
24
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
25
from breezy.lazy_import import lazy_import
6282.3.1 by Vincent Ladeuil
First cut at a working plugin to avoid conflicts in .po files by shelling out to msgmerge.
26
lazy_import(globals(), """
27
import fnmatch
28
import subprocess
29
import tempfile
30
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
31
from breezy import (
6282.3.1 by Vincent Ladeuil
First cut at a working plugin to avoid conflicts in .po files by shelling out to msgmerge.
32
    cmdline,
33
    osutils,
34
    trace,
35
    )
36
""")
37
38
6282.3.14 by Vincent Ladeuil
Use lazy registration for plugin options to set a good example.
39
command_option = config.Option(
7143.15.2 by Jelmer Vernooij
Run autopep8.
40
    'po_merge.command',
41
    default='msgmerge -N "{other}" "{pot_file}" -C "{this}" -o "{result}"',
42
    help='''\
6282.3.14 by Vincent Ladeuil
Use lazy registration for plugin options to set a good example.
43
Command used to create a conflict-free .po file during merge.
44
45
The following parameters are provided by the hook:
46
``this`` is the ``.po`` file content before the merge in the current branch,
47
``other`` is the ``.po`` file content in the branch merged from,
48
``pot_file`` is the path to the ``.pot`` file corresponding to the ``.po``
49
file being merged.
50
``result`` is the path where ``msgmerge`` will output its result. The hook will
51
use the content of this file to produce the resulting ``.po`` file.
52
53
All paths are absolute.
54
''')
55
56
6385.1.1 by Vincent Ladeuil
Stores allow Stacks to control when values are quoted/unquoted
57
po_dirs_option = config.ListOption(
7143.15.2 by Jelmer Vernooij
Run autopep8.
58
    'po_merge.po_dirs', default='po,debian/po',
59
    help='List of dirs containing .po files that the hook applies to.')
6282.3.14 by Vincent Ladeuil
Use lazy registration for plugin options to set a good example.
60
61
62
po_glob_option = config.Option(
7143.15.2 by Jelmer Vernooij
Run autopep8.
63
    'po_merge.po_glob', default='*.po',
64
    help='Glob matching all ``.po`` files in one of ``po_merge.po_dirs``.')
6282.3.14 by Vincent Ladeuil
Use lazy registration for plugin options to set a good example.
65
66
pot_glob_option = config.Option(
7143.15.2 by Jelmer Vernooij
Run autopep8.
67
    'po_merge.pot_glob', default='*.pot',
68
    help='Glob matching the ``.pot`` file in one of ``po_merge.po_dirs``.')
6282.3.14 by Vincent Ladeuil
Use lazy registration for plugin options to set a good example.
69
70
6282.3.1 by Vincent Ladeuil
First cut at a working plugin to avoid conflicts in .po files by shelling out to msgmerge.
71
class PoMerger(merge.PerFileMerger):
72
    """Merge .po files."""
73
74
    def __init__(self, merger):
75
        super(merge.PerFileMerger, self).__init__(merger)
76
        # config options are cached locally until config files are (see
77
        # http://pad.lv/832042)
78
79
        # FIXME: We use the branch config as there is no tree config
80
        # -- vila 2011-11-23
81
        self.conf = merger.this_branch.get_config_stack()
7143.15.2 by Jelmer Vernooij
Run autopep8.
82
        # Which dirs 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.
83
        self.po_dirs = self.conf.get('po_merge.po_dirs')
7143.15.2 by Jelmer Vernooij
Run autopep8.
84
        # 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.
85
        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.
86
        # 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.
87
        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.
88
        self.command = self.conf.get('po_merge.command', expand=False)
89
        # 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.
90
        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.
91
        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.
92
93
    def file_matches(self, params):
94
        """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.
95
        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.
96
            # Return early if there is no options defined
97
            return False
6282.3.3 by Vincent Ladeuil
Change option scheme so users mainly have to deal with po_merge.po_dirs only.
98
        po_dir = None
6883.10.1 by Jelmer Vernooij
Fix more tets.
99
        po_path = params.this_path
6282.3.3 by Vincent Ladeuil
Change option scheme so users mainly have to deal with po_merge.po_dirs only.
100
        for po_dir in self.po_dirs:
101
            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.
102
            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.
103
                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.
104
                break
6282.3.3 by Vincent Ladeuil
Change option scheme so users mainly have to deal with po_merge.po_dirs only.
105
        else:
106
            trace.mutter('PoMerger did not match for %s and %s'
107
                         % (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.
108
            return False
109
        # Do we have the corresponding .pot file
7143.19.5 by Jelmer Vernooij
Undo removal of kind.
110
        for path, file_class, kind, entry in self.merger.this_tree.list_files(
7143.19.1 by Jelmer Vernooij
Drop superfluous kind and file_id return values from Tree.list_files.
111
                from_dir=po_dir, recursive=False):
112
            pot_name, pot_file_id = path, entry
6282.3.3 by Vincent Ladeuil
Change option scheme so users mainly have to deal with po_merge.po_dirs only.
113
            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.
114
                relpath = osutils.pathjoin(po_dir, pot_name)
115
                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.
116
                # FIXME: I can't find an easy way to know if the .pot file has
117
                # conflicts *during* the merge itself. So either the actual
118
                # content on disk is fine and msgmerge will work OR it's not
119
                # and it will fail. Conversely, either the result is ok for the
120
                # user and he's happy OR the user needs to resolve the
121
                # conflicts in the .pot file and use remerge.
122
                # -- 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}.
123
                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.
124
                             % (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.
125
                return True
126
        else:
6282.3.1 by Vincent Ladeuil
First cut at a working plugin to avoid conflicts in .po files by shelling out to msgmerge.
127
            return False
128
129
    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.
130
        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.
131
        # 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.
132
        proc = subprocess.Popen(cmdline.split(command),
133
                                stdout=subprocess.PIPE,
134
                                stderr=subprocess.PIPE,
135
                                stdin=subprocess.PIPE)
136
        out, err = proc.communicate()
137
        return proc.returncode, out, err
138
139
    def merge_matching(self, params):
140
        return self.merge_text(params)
141
142
    def merge_text(self, params):
143
        """Calls msgmerge when .po files conflict.
144
145
        This requires a valid .pot file to reconcile both sides.
146
        """
147
        # Create tmp files with the 'this' and 'other' content
148
        tmpdir = tempfile.mkdtemp(prefix='po_merge')
149
        env = {}
150
        env['this'] = osutils.pathjoin(tmpdir, 'this')
151
        env['other'] = osutils.pathjoin(tmpdir, 'other')
152
        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.
153
        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.
154
        try:
155
            with osutils.open_file(env['this'], 'wb') as f:
156
                f.writelines(params.this_lines)
157
            with osutils.open_file(env['other'], 'wb') as f:
158
                f.writelines(params.other_lines)
159
            command = self.conf.expand_options(self.command, env)
160
            retcode, out, err = self._invoke(command)
7045.2.5 by Jelmer Vernooij
Fix po_merge and upstream_import tests.
161
            with osutils.open_file(env['result'], 'rb') as f:
6282.3.1 by Vincent Ladeuil
First cut at a working plugin to avoid conflicts in .po files by shelling out to msgmerge.
162
                # FIXME: To avoid the list() construct below which means the
163
                # whole 'result' file is kept in memory, there may be a way to
164
                # use an iterator that will close the file when it's done, but
165
                # there is still the issue of removing the tmp dir...
166
                # -- vila 2011-11-24
167
                return 'success', list(f.readlines())
168
        finally:
169
            osutils.rmtree(tmpdir)
170
        return 'not applicable', []