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."""
33
DEFAULT_PATCHES_DIR = 'patches'
34
DEFAULT_SERIES_FILE = 'series'
37
class QuiltError(errors.BzrError):
39
_fmt = "An error (%(retcode)d) occurred running quilt: %(stderr)s%(extra)s"
41
def __init__(self, retcode, stdout, stderr):
42
self.retcode = retcode
44
if stdout is not None:
45
self.extra = "\n\n%s" % stdout
51
class QuiltNotInstalled(errors.BzrError):
53
_fmt = "Quilt is not installed."
57
args, working_dir, series_file=None, patches_dir=None, quiet=None):
60
:param args: Arguments to quilt
61
:param working_dir: Working dir
62
:param series_file: Optional path to the series file
63
:param patches_dir: Optional path to the patches
64
:param quilt: Whether to be quiet (quilt stderr not to terminal)
65
:raise QuiltError: When running quilt fails
67
def subprocess_setup():
68
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
70
if patches_dir is not None:
71
env["QUILT_PATCHES"] = patches_dir
73
env["QUILT_PATCHES"] = os.path.join(working_dir, DEFAULT_PATCHES_DIR)
74
if series_file is not None:
75
env["QUILT_SERIES"] = series_file
77
env["QUILT_SERIES"] = DEFAULT_SERIES_FILE
78
# Hide output if -q is in use.
80
quiet = trace.is_quiet()
82
stderr = subprocess.STDOUT
84
stderr = subprocess.PIPE
85
command = ["quilt"] + args
86
trace.mutter("running: %r", command)
87
if not os.path.isdir(working_dir):
88
raise AssertionError("%s is not a valid directory" % working_dir)
90
proc = subprocess.Popen(
91
command, cwd=working_dir, env=env,
92
stdin=subprocess.PIPE, preexec_fn=subprocess_setup,
93
stdout=subprocess.PIPE, stderr=stderr)
95
if e.errno != errno.ENOENT:
97
raise QuiltNotInstalled()
98
(stdout, stderr) = proc.communicate()
99
if proc.returncode not in (0, 2):
100
if stdout is not None:
101
stdout = stdout.decode()
102
if stderr is not None:
103
stderr = stderr.decode()
104
raise QuiltError(proc.returncode, stdout, stderr)
111
working_dir, patches_dir=None, series_file=None, quiet=None,
112
force=False, refresh=False):
115
:param working_dir: Directory to work in
116
:param patches_dir: Optional patches directory
117
:param series_file: Optional series file
123
args.append("--refresh")
125
args, working_dir=working_dir,
126
patches_dir=patches_dir, series_file=series_file, quiet=quiet)
129
def quilt_pop(working_dir, patch, patches_dir=None, series_file=None, quiet=None):
132
:param working_dir: Directory to work in
133
:param patch: Patch to apply
134
:param patches_dir: Optional patches directory
135
:param series_file: Optional series file
138
["pop", patch], working_dir=working_dir,
139
patches_dir=patches_dir, series_file=series_file, quiet=quiet)
142
def quilt_push_all(working_dir, patches_dir=None, series_file=None, quiet=None,
143
force=False, refresh=False):
146
:param working_dir: Directory to work in
147
:param patches_dir: Optional patches directory
148
:param series_file: Optional series file
150
args = ["push", "-a"]
154
args.append("--refresh")
156
args, working_dir=working_dir,
157
patches_dir=patches_dir, series_file=series_file, quiet=quiet)
160
def quilt_push(working_dir, patch, patches_dir=None, series_file=None,
161
quiet=None, force=False, refresh=False):
164
:param working_dir: Directory to work in
165
:param patch: Patch to push
166
:param patches_dir: Optional patches directory
167
:param series_file: Optional series file
168
:param force: Force push
169
:param refresh: Refresh
175
args.append("--refresh")
177
["push", patch] + args, working_dir=working_dir,
178
patches_dir=patches_dir, series_file=series_file, quiet=quiet)
181
def quilt_delete(working_dir, patch, patches_dir=None, series_file=None,
185
:param working_dir: Directory to work in
186
:param patch: Patch to push
187
:param patches_dir: Optional patches directory
188
:param series_file: Optional series file
189
:param remove: Remove the patch file as well
195
["delete", patch] + args, working_dir=working_dir,
196
patches_dir=patches_dir, series_file=series_file)
199
def quilt_upgrade(working_dir):
200
return run_quilt(["upgrade"], working_dir=working_dir)
203
def quilt_applied(tree):
204
"""Find the list of applied quilt patches.
208
return [patch.rstrip(b"\n").decode(osutils._fs_enc)
209
for patch in tree.get_file_lines(".pc/applied-patches")
210
if patch.strip() != b""]
211
except errors.NoSuchFile:
213
except (IOError, OSError) as e:
214
if e.errno == errno.ENOENT:
215
# File has already been removed
220
def quilt_unapplied(working_dir, patches_dir=None, series_file=None):
221
"""Find the list of unapplied quilt patches.
223
:param working_dir: Directory to work in
224
:param patches_dir: Optional patches directory
225
:param series_file: Optional series file
227
working_dir = os.path.abspath(working_dir)
228
if patches_dir is None:
229
patches_dir = os.path.join(working_dir, DEFAULT_PATCHES_DIR)
231
unapplied_patches = run_quilt(
233
working_dir=working_dir, patches_dir=patches_dir,
234
series_file=series_file).splitlines()
236
for patch in unapplied_patches:
237
patch = patch.decode(osutils._fs_enc)
238
patch_names.append(os.path.relpath(patch, patches_dir))
240
except QuiltError as e:
246
def quilt_series(tree, series_path):
247
"""Find the list of patches.
249
:param tree: Tree to read from
252
return [patch.rstrip(b"\n").decode(osutils._fs_enc) for patch in
253
tree.get_file_lines(series_path)
254
if patch.strip() != b""]
255
except (IOError, OSError) as e:
256
if e.errno == errno.ENOENT:
257
# File has already been removed
260
except errors.NoSuchFile: