/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/plugins/quilt/wrapper.py

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2019-06-15 13:52:00 UTC
  • mfrom: (7321.2.5 bundle-quilt)
  • Revision ID: breezy.the.bot@gmail.com-20190615135200-dd5zif8ub3xi179t
Add a quilt plugin that can do smart things with quilt patches.

Merged from https://code.launchpad.net/~jelmer/brz/bundle-quilt/+merge/368585

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#    quilt.py -- Quilt patch handling
 
2
#    Copyright (C) 2011 Canonical Ltd.
 
3
#    Copyright (C) 2019 Jelmer Verooij <jelmer@jelmer.uk>
 
4
#
 
5
#    Breezy is free software; you can redistribute it and/or modify
 
6
#    it under the terms of the GNU General Public License as published by
 
7
#    the Free Software Foundation; either version 2 of the License, or
 
8
#    (at your option) any later version.
 
9
#
 
10
#    Breezy is distributed in the hope that it will be useful,
 
11
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
#    GNU General Public License for more details.
 
14
#
 
15
#    You should have received a copy of the GNU General Public License
 
16
#    along with Breezy; if not, write to the Free Software
 
17
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
#
 
19
 
 
20
"""Quilt patch handling."""
 
21
 
 
22
from __future__ import absolute_import
 
23
 
 
24
import errno
 
25
import os
 
26
import signal
 
27
import subprocess
 
28
from ... import (
 
29
    errors,
 
30
    osutils,
 
31
    trace,
 
32
    )
 
33
 
 
34
 
 
35
class QuiltError(errors.BzrError):
 
36
 
 
37
    _fmt = "An error (%(retcode)d) occurred running quilt: %(stderr)s%(extra)s"
 
38
 
 
39
    def __init__(self, retcode, stdout, stderr):
 
40
        self.retcode = retcode
 
41
        self.stderr = stderr
 
42
        if stdout is not None:
 
43
            self.extra = "\n\n%s" % stdout
 
44
        else:
 
45
            self.extra = ""
 
46
        self.stdout = stdout
 
47
 
 
48
 
 
49
class QuiltNotInstalled(errors.BzrError):
 
50
 
 
51
    _fmt = "Quilt is not installed."
 
52
 
 
53
 
 
54
def run_quilt(
 
55
        args, working_dir, series_file=None, patches_dir=None, quiet=None):
 
56
    """Run quilt.
 
57
 
 
58
    :param args: Arguments to quilt
 
59
    :param working_dir: Working dir
 
60
    :param series_file: Optional path to the series file
 
61
    :param patches_dir: Optional path to the patches
 
62
    :param quilt: Whether to be quiet (quilt stderr not to terminal)
 
63
    :raise QuiltError: When running quilt fails
 
64
    """
 
65
    def subprocess_setup():
 
66
        signal.signal(signal.SIGPIPE, signal.SIG_DFL)
 
67
    env = {}
 
68
    if patches_dir is not None:
 
69
        env["QUILT_PATCHES"] = patches_dir
 
70
    else:
 
71
        env["QUILT_PATCHES"] = os.path.join(working_dir, "patches")
 
72
    if series_file is not None:
 
73
        env["QUILT_SERIES"] = series_file
 
74
    else:
 
75
        env["QUILT_SERIES"] = os.path.join(env["QUILT_PATCHES"], "series")
 
76
    # Hide output if -q is in use.
 
77
    if quiet is None:
 
78
        quiet = trace.is_quiet()
 
79
    if not quiet:
 
80
        stderr = subprocess.STDOUT
 
81
    else:
 
82
        stderr = subprocess.PIPE
 
83
    command = ["quilt"] + args
 
84
    trace.mutter("running: %r", command)
 
85
    if not os.path.isdir(working_dir):
 
86
        raise AssertionError("%s is not a valid directory" % working_dir)
 
87
    try:
 
88
        proc = subprocess.Popen(
 
89
            command, cwd=working_dir, env=env,
 
90
            stdin=subprocess.PIPE, preexec_fn=subprocess_setup,
 
91
            stdout=subprocess.PIPE, stderr=stderr)
 
92
    except OSError as e:
 
93
        if e.errno != errno.ENOENT:
 
94
            raise
 
95
        raise QuiltNotInstalled()
 
96
    (stdout, stderr) = proc.communicate()
 
97
    if proc.returncode not in (0, 2):
 
98
        if stdout is not None:
 
99
            stdout = stdout.decode()
 
100
        if stderr is not None:
 
101
            stderr = stderr.decode()
 
102
        raise QuiltError(proc.returncode, stdout, stderr)
 
103
    if stdout is None:
 
104
        return ""
 
105
    return stdout
 
106
 
 
107
 
 
108
def quilt_pop_all(
 
109
        working_dir, patches_dir=None, series_file=None, quiet=None,
 
110
        force=False, refresh=False):
 
111
    """Pop all patches.
 
112
 
 
113
    :param working_dir: Directory to work in
 
114
    :param patches_dir: Optional patches directory
 
115
    :param series_file: Optional series file
 
116
    """
 
117
    args = ["pop", "-a"]
 
118
    if force:
 
119
        args.append("-f")
 
120
    if refresh:
 
121
        args.append("--refresh")
 
122
    return run_quilt(
 
123
        args, working_dir=working_dir,
 
124
        patches_dir=patches_dir, series_file=series_file, quiet=quiet)
 
125
 
 
126
 
 
127
def quilt_pop(working_dir, patch, patches_dir=None, series_file=None, quiet=None):
 
128
    """Pop a patch.
 
129
 
 
130
    :param working_dir: Directory to work in
 
131
    :param patch: Patch to apply
 
132
    :param patches_dir: Optional patches directory
 
133
    :param series_file: Optional series file
 
134
    """
 
135
    return run_quilt(
 
136
        ["pop", patch], working_dir=working_dir,
 
137
        patches_dir=patches_dir, series_file=series_file, quiet=quiet)
 
138
 
 
139
 
 
140
def quilt_push_all(working_dir, patches_dir=None, series_file=None, quiet=None,
 
141
                   force=False, refresh=False):
 
142
    """Push all patches.
 
143
 
 
144
    :param working_dir: Directory to work in
 
145
    :param patches_dir: Optional patches directory
 
146
    :param series_file: Optional series file
 
147
    """
 
148
    args = ["push", "-a"]
 
149
    if force:
 
150
        args.append("-f")
 
151
    if refresh:
 
152
        args.append("--refresh")
 
153
    return run_quilt(
 
154
        args, working_dir=working_dir,
 
155
        patches_dir=patches_dir, series_file=series_file, quiet=quiet)
 
156
 
 
157
 
 
158
def quilt_push(working_dir, patch, patches_dir=None, series_file=None, quiet=None):
 
159
    """Push a patch.
 
160
 
 
161
    :param working_dir: Directory to work in
 
162
    :param patch: Patch to push
 
163
    :param patches_dir: Optional patches directory
 
164
    :param series_file: Optional series file
 
165
    """
 
166
    return run_quilt(
 
167
        ["push", patch], working_dir=working_dir,
 
168
        patches_dir=patches_dir, series_file=series_file, quiet=quiet)
 
169
 
 
170
 
 
171
def quilt_upgrade(working_dir):
 
172
    return run_quilt(["upgrade"], working_dir=working_dir)
 
173
 
 
174
 
 
175
def quilt_applied(tree):
 
176
    """Find the list of applied quilt patches.
 
177
 
 
178
    """
 
179
    try:
 
180
        return [patch.rstrip(b"\n").decode(osutils._fs_enc)
 
181
                for patch in tree.get_file_lines(".pc/applied-patches")
 
182
                if patch.strip() != b""]
 
183
    except errors.NoSuchFile:
 
184
        return []
 
185
    except (IOError, OSError) as e:
 
186
        if e.errno == errno.ENOENT:
 
187
            # File has already been removed
 
188
            return []
 
189
        raise
 
190
 
 
191
 
 
192
def quilt_unapplied(working_dir, patches_dir=None, series_file=None):
 
193
    """Find the list of unapplied quilt patches.
 
194
 
 
195
    :param working_dir: Directory to work in
 
196
    :param patches_dir: Optional patches directory
 
197
    :param series_file: Optional series file
 
198
    """
 
199
    try:
 
200
        unapplied_patches = run_quilt(
 
201
            ["unapplied"],
 
202
            working_dir=working_dir, patches_dir=patches_dir,
 
203
            series_file=series_file).splitlines()
 
204
        patch_basenames = []
 
205
        for patch in unapplied_patches:
 
206
            patch = os.path.basename(patch)
 
207
            patch_basenames.append(patch.decode(osutils._fs_enc))
 
208
        return patch_basenames
 
209
    except QuiltError as e:
 
210
        if e.retcode == 1:
 
211
            return []
 
212
        raise
 
213
 
 
214
 
 
215
def quilt_series(tree, series_file):
 
216
    """Find the list of patches.
 
217
 
 
218
    :param tree: Tree to read from
 
219
    """
 
220
    try:
 
221
        return [patch.rstrip(b"\n").decode(osutils._fs_enc) for patch in
 
222
                tree.get_file_lines(series_file)
 
223
                if patch.strip() != b""]
 
224
    except (IOError, OSError) as e:
 
225
        if e.errno == errno.ENOENT:
 
226
            # File has already been removed
 
227
            return []
 
228
        raise
 
229
    except errors.NoSuchFile:
 
230
        return []