1
# quilt.py -- Quilt patch handling
2
# Copyright (C) 2011 Canonical Ltd.
3
# Copyright (C) 2019 Jelmer Verooij <jelmer@jelmer.uk>
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.
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.
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
20
"""Quilt patch handling."""
22
from __future__ import absolute_import
35
class QuiltError(errors.BzrError):
37
_fmt = "An error (%(retcode)d) occurred running quilt: %(stderr)s%(extra)s"
39
def __init__(self, retcode, stdout, stderr):
40
self.retcode = retcode
42
if stdout is not None:
43
self.extra = "\n\n%s" % stdout
49
class QuiltNotInstalled(errors.BzrError):
51
_fmt = "Quilt is not installed."
55
args, working_dir, series_file=None, patches_dir=None, quiet=None):
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
65
def subprocess_setup():
66
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
68
if patches_dir is not None:
69
env["QUILT_PATCHES"] = patches_dir
71
env["QUILT_PATCHES"] = os.path.join(working_dir, "patches")
72
if series_file is not None:
73
env["QUILT_SERIES"] = series_file
75
env["QUILT_SERIES"] = os.path.join(env["QUILT_PATCHES"], "series")
76
# Hide output if -q is in use.
78
quiet = trace.is_quiet()
80
stderr = subprocess.STDOUT
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)
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)
93
if e.errno != errno.ENOENT:
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)
109
working_dir, patches_dir=None, series_file=None, quiet=None,
110
force=False, refresh=False):
113
:param working_dir: Directory to work in
114
:param patches_dir: Optional patches directory
115
:param series_file: Optional series file
121
args.append("--refresh")
123
args, working_dir=working_dir,
124
patches_dir=patches_dir, series_file=series_file, quiet=quiet)
127
def quilt_pop(working_dir, patch, patches_dir=None, series_file=None, quiet=None):
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
136
["pop", patch], working_dir=working_dir,
137
patches_dir=patches_dir, series_file=series_file, quiet=quiet)
140
def quilt_push_all(working_dir, patches_dir=None, series_file=None, quiet=None,
141
force=False, refresh=False):
144
:param working_dir: Directory to work in
145
:param patches_dir: Optional patches directory
146
:param series_file: Optional series file
148
args = ["push", "-a"]
152
args.append("--refresh")
154
args, working_dir=working_dir,
155
patches_dir=patches_dir, series_file=series_file, quiet=quiet)
158
def quilt_push(working_dir, patch, patches_dir=None, series_file=None, quiet=None):
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
167
["push", patch], working_dir=working_dir,
168
patches_dir=patches_dir, series_file=series_file, quiet=quiet)
171
def quilt_upgrade(working_dir):
172
return run_quilt(["upgrade"], working_dir=working_dir)
175
def quilt_applied(tree):
176
"""Find the list of applied quilt patches.
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:
185
except (IOError, OSError) as e:
186
if e.errno == errno.ENOENT:
187
# File has already been removed
192
def quilt_unapplied(working_dir, patches_dir=None, series_file=None):
193
"""Find the list of unapplied quilt patches.
195
:param working_dir: Directory to work in
196
:param patches_dir: Optional patches directory
197
:param series_file: Optional series file
200
unapplied_patches = run_quilt(
202
working_dir=working_dir, patches_dir=patches_dir,
203
series_file=series_file).splitlines()
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:
215
def quilt_series(tree, series_file):
216
"""Find the list of patches.
218
:param tree: Tree to read from
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
229
except errors.NoSuchFile: