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
DEFAULT_PATCHES_DIR = 'patches'
36
DEFAULT_SERIES_FILE = 'series'
39
class QuiltError(errors.BzrError):
41
_fmt = "An error (%(retcode)d) occurred running quilt: %(stderr)s%(extra)s"
43
def __init__(self, retcode, stdout, stderr):
44
self.retcode = retcode
46
if stdout is not None:
47
self.extra = "\n\n%s" % stdout
53
class QuiltNotInstalled(errors.BzrError):
55
_fmt = "Quilt is not installed."
59
args, working_dir, series_file=None, patches_dir=None, quiet=None):
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
69
def subprocess_setup():
70
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
72
if patches_dir is not None:
73
env["QUILT_PATCHES"] = patches_dir
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
79
env["QUILT_SERIES"] = DEFAULT_SERIES_FILE
80
# Hide output if -q is in use.
82
quiet = trace.is_quiet()
84
stderr = subprocess.STDOUT
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)
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)
97
if e.errno != errno.ENOENT:
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)
113
working_dir, patches_dir=None, series_file=None, quiet=None,
114
force=False, refresh=False):
117
:param working_dir: Directory to work in
118
:param patches_dir: Optional patches directory
119
:param series_file: Optional series file
125
args.append("--refresh")
127
args, working_dir=working_dir,
128
patches_dir=patches_dir, series_file=series_file, quiet=quiet)
131
def quilt_pop(working_dir, patch, patches_dir=None, series_file=None, quiet=None):
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
140
["pop", patch], working_dir=working_dir,
141
patches_dir=patches_dir, series_file=series_file, quiet=quiet)
144
def quilt_push_all(working_dir, patches_dir=None, series_file=None, quiet=None,
145
force=False, refresh=False):
148
:param working_dir: Directory to work in
149
:param patches_dir: Optional patches directory
150
:param series_file: Optional series file
152
args = ["push", "-a"]
156
args.append("--refresh")
158
args, working_dir=working_dir,
159
patches_dir=patches_dir, series_file=series_file, quiet=quiet)
162
def quilt_push(working_dir, patch, patches_dir=None, series_file=None,
163
quiet=None, force=False, refresh=False):
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
177
args.append("--refresh")
179
["push", patch] + args, working_dir=working_dir,
180
patches_dir=patches_dir, series_file=series_file, quiet=quiet)
183
def quilt_delete(working_dir, patch, patches_dir=None, series_file=None,
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
197
["delete", patch] + args, working_dir=working_dir,
198
patches_dir=patches_dir, series_file=series_file)
201
def quilt_upgrade(working_dir):
202
return run_quilt(["upgrade"], working_dir=working_dir)
205
def quilt_applied(tree):
206
"""Find the list of applied quilt patches.
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:
215
except (IOError, OSError) as e:
216
if e.errno == errno.ENOENT:
217
# File has already been removed
222
def quilt_unapplied(working_dir, patches_dir=None, series_file=None):
223
"""Find the list of unapplied quilt patches.
225
:param working_dir: Directory to work in
226
:param patches_dir: Optional patches directory
227
:param series_file: Optional series file
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)
233
unapplied_patches = run_quilt(
235
working_dir=working_dir, patches_dir=patches_dir,
236
series_file=series_file).splitlines()
238
for patch in unapplied_patches:
239
patch = patch.decode(osutils._fs_enc)
240
patch_names.append(os.path.relpath(patch, patches_dir))
242
except QuiltError as e:
248
def quilt_series(tree, series_path):
249
"""Find the list of patches.
251
:param tree: Tree to read from
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
262
except errors.NoSuchFile: