/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: Jelmer Vernooij
  • Date: 2020-02-05 01:40:59 UTC
  • mto: This revision was merged to the branch mainline in revision 7480.
  • Revision ID: jelmer@jelmer.uk-20200205014059-1jrhjaphw5vh9i7s
Fix Python 2.7 build.

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
DEFAULT_PATCHES_DIR = 'patches'
 
36
DEFAULT_SERIES_FILE = 'series'
 
37
 
 
38
 
 
39
class QuiltError(errors.BzrError):
 
40
 
 
41
    _fmt = "An error (%(retcode)d) occurred running quilt: %(stderr)s%(extra)s"
 
42
 
 
43
    def __init__(self, retcode, stdout, stderr):
 
44
        self.retcode = retcode
 
45
        self.stderr = stderr
 
46
        if stdout is not None:
 
47
            self.extra = "\n\n%s" % stdout
 
48
        else:
 
49
            self.extra = ""
 
50
        self.stdout = stdout
 
51
 
 
52
 
 
53
class QuiltNotInstalled(errors.BzrError):
 
54
 
 
55
    _fmt = "Quilt is not installed."
 
56
 
 
57
 
 
58
def run_quilt(
 
59
        args, working_dir, series_file=None, patches_dir=None, quiet=None):
 
60
    """Run quilt.
 
61
 
 
62
    :param args: Arguments to quilt
 
63
    :param working_dir: Working dir
 
64
    :param series_file: Optional path to the series file
 
65
    :param patches_dir: Optional path to the patches
 
66
    :param quilt: Whether to be quiet (quilt stderr not to terminal)
 
67
    :raise QuiltError: When running quilt fails
 
68
    """
 
69
    def subprocess_setup():
 
70
        signal.signal(signal.SIGPIPE, signal.SIG_DFL)
 
71
    env = {}
 
72
    if patches_dir is not None:
 
73
        env["QUILT_PATCHES"] = patches_dir
 
74
    else:
 
75
        env["QUILT_PATCHES"] = os.path.join(working_dir, DEFAULT_PATCHES_DIR)
 
76
    if series_file is not None:
 
77
        env["QUILT_SERIES"] = series_file
 
78
    else:
 
79
        env["QUILT_SERIES"] = DEFAULT_SERIES_FILE
 
80
    # Hide output if -q is in use.
 
81
    if quiet is None:
 
82
        quiet = trace.is_quiet()
 
83
    if not quiet:
 
84
        stderr = subprocess.STDOUT
 
85
    else:
 
86
        stderr = subprocess.PIPE
 
87
    command = ["quilt"] + args
 
88
    trace.mutter("running: %r", command)
 
89
    if not os.path.isdir(working_dir):
 
90
        raise AssertionError("%s is not a valid directory" % working_dir)
 
91
    try:
 
92
        proc = subprocess.Popen(
 
93
            command, cwd=working_dir, env=env,
 
94
            stdin=subprocess.PIPE, preexec_fn=subprocess_setup,
 
95
            stdout=subprocess.PIPE, stderr=stderr)
 
96
    except OSError as e:
 
97
        if e.errno != errno.ENOENT:
 
98
            raise
 
99
        raise QuiltNotInstalled()
 
100
    (stdout, stderr) = proc.communicate()
 
101
    if proc.returncode not in (0, 2):
 
102
        if stdout is not None:
 
103
            stdout = stdout.decode()
 
104
        if stderr is not None:
 
105
            stderr = stderr.decode()
 
106
        raise QuiltError(proc.returncode, stdout, stderr)
 
107
    if stdout is None:
 
108
        return ""
 
109
    return stdout
 
110
 
 
111
 
 
112
def quilt_pop_all(
 
113
        working_dir, patches_dir=None, series_file=None, quiet=None,
 
114
        force=False, refresh=False):
 
115
    """Pop all patches.
 
116
 
 
117
    :param working_dir: Directory to work in
 
118
    :param patches_dir: Optional patches directory
 
119
    :param series_file: Optional series file
 
120
    """
 
121
    args = ["pop", "-a"]
 
122
    if force:
 
123
        args.append("-f")
 
124
    if refresh:
 
125
        args.append("--refresh")
 
126
    return run_quilt(
 
127
        args, working_dir=working_dir,
 
128
        patches_dir=patches_dir, series_file=series_file, quiet=quiet)
 
129
 
 
130
 
 
131
def quilt_pop(working_dir, patch, patches_dir=None, series_file=None, quiet=None):
 
132
    """Pop a patch.
 
133
 
 
134
    :param working_dir: Directory to work in
 
135
    :param patch: Patch to apply
 
136
    :param patches_dir: Optional patches directory
 
137
    :param series_file: Optional series file
 
138
    """
 
139
    return run_quilt(
 
140
        ["pop", patch], working_dir=working_dir,
 
141
        patches_dir=patches_dir, series_file=series_file, quiet=quiet)
 
142
 
 
143
 
 
144
def quilt_push_all(working_dir, patches_dir=None, series_file=None, quiet=None,
 
145
                   force=False, refresh=False):
 
146
    """Push all patches.
 
147
 
 
148
    :param working_dir: Directory to work in
 
149
    :param patches_dir: Optional patches directory
 
150
    :param series_file: Optional series file
 
151
    """
 
152
    args = ["push", "-a"]
 
153
    if force:
 
154
        args.append("-f")
 
155
    if refresh:
 
156
        args.append("--refresh")
 
157
    return run_quilt(
 
158
        args, working_dir=working_dir,
 
159
        patches_dir=patches_dir, series_file=series_file, quiet=quiet)
 
160
 
 
161
 
 
162
def quilt_push(working_dir, patch, patches_dir=None, series_file=None,
 
163
               quiet=None, force=False, refresh=False):
 
164
    """Push a patch.
 
165
 
 
166
    :param working_dir: Directory to work in
 
167
    :param patch: Patch to push
 
168
    :param patches_dir: Optional patches directory
 
169
    :param series_file: Optional series file
 
170
    :param force: Force push
 
171
    :param refresh: Refresh
 
172
    """
 
173
    args = []
 
174
    if force:
 
175
        args.append("-f")
 
176
    if refresh:
 
177
        args.append("--refresh")
 
178
    return run_quilt(
 
179
        ["push", patch] + args, working_dir=working_dir,
 
180
        patches_dir=patches_dir, series_file=series_file, quiet=quiet)
 
181
 
 
182
 
 
183
def quilt_delete(working_dir, patch, patches_dir=None, series_file=None,
 
184
                 remove=False):
 
185
    """Delete a patch.
 
186
 
 
187
    :param working_dir: Directory to work in
 
188
    :param patch: Patch to push
 
189
    :param patches_dir: Optional patches directory
 
190
    :param series_file: Optional series file
 
191
    :param remove: Remove the patch file as well
 
192
    """
 
193
    args = []
 
194
    if remove:
 
195
        args.append("-r")
 
196
    return run_quilt(
 
197
        ["delete", patch] + args, working_dir=working_dir,
 
198
        patches_dir=patches_dir, series_file=series_file)
 
199
 
 
200
 
 
201
def quilt_upgrade(working_dir):
 
202
    return run_quilt(["upgrade"], working_dir=working_dir)
 
203
 
 
204
 
 
205
def quilt_applied(tree):
 
206
    """Find the list of applied quilt patches.
 
207
 
 
208
    """
 
209
    try:
 
210
        return [patch.rstrip(b"\n").decode(osutils._fs_enc)
 
211
                for patch in tree.get_file_lines(".pc/applied-patches")
 
212
                if patch.strip() != b""]
 
213
    except errors.NoSuchFile:
 
214
        return []
 
215
    except (IOError, OSError) as e:
 
216
        if e.errno == errno.ENOENT:
 
217
            # File has already been removed
 
218
            return []
 
219
        raise
 
220
 
 
221
 
 
222
def quilt_unapplied(working_dir, patches_dir=None, series_file=None):
 
223
    """Find the list of unapplied quilt patches.
 
224
 
 
225
    :param working_dir: Directory to work in
 
226
    :param patches_dir: Optional patches directory
 
227
    :param series_file: Optional series file
 
228
    """
 
229
    working_dir = os.path.abspath(working_dir)
 
230
    if patches_dir is None:
 
231
        patches_dir = os.path.join(working_dir, DEFAULT_PATCHES_DIR)
 
232
    try:
 
233
        unapplied_patches = run_quilt(
 
234
            ["unapplied"],
 
235
            working_dir=working_dir, patches_dir=patches_dir,
 
236
            series_file=series_file).splitlines()
 
237
        patch_names = []
 
238
        for patch in unapplied_patches:
 
239
            patch = patch.decode(osutils._fs_enc)
 
240
            patch_names.append(os.path.relpath(patch, patches_dir))
 
241
        return patch_names
 
242
    except QuiltError as e:
 
243
        if e.retcode == 1:
 
244
            return []
 
245
        raise
 
246
 
 
247
 
 
248
def quilt_series(tree, series_path):
 
249
    """Find the list of patches.
 
250
 
 
251
    :param tree: Tree to read from
 
252
    """
 
253
    try:
 
254
        return [patch.rstrip(b"\n").decode(osutils._fs_enc) for patch in
 
255
                tree.get_file_lines(series_path)
 
256
                if patch.strip() != b""]
 
257
    except (IOError, OSError) as e:
 
258
        if e.errno == errno.ENOENT:
 
259
            # File has already been removed
 
260
            return []
 
261
        raise
 
262
    except errors.NoSuchFile:
 
263
        return []