1
# quilt.py -- Quilt patch handling
2
# Copyright (C) 2011 Canonical Ltd.
4
# This file is part of bzr-builddeb.
6
# bzr-builddeb is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
11
# bzr-builddeb is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with bzr-builddeb; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
"""Quilt patch handling."""
23
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
def run_quilt(args, working_dir, series_file=None, patches_dir=None, quiet=None):
52
:param args: Arguments to quilt
53
:param working_dir: Working dir
54
:param series_file: Optional path to the series file
55
:param patches_dir: Optional path to the patches
56
:param quilt: Whether to be quiet (quilt stderr not to terminal)
57
:raise QuiltError: When running quilt fails
59
def subprocess_setup():
60
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
62
if patches_dir is not None:
63
env["QUILT_PATCHES"] = patches_dir
65
env["QUILT_PATCHES"] = os.path.join(working_dir, "debian", "patches")
66
if series_file is not None:
67
env["QUILT_SERIES"] = series_file
69
env["QUILT_SERIES"] = os.path.join(env["QUILT_PATCHES"], "series")
70
# Hide output if -q is in use.
72
quiet = trace.is_quiet()
74
stderr = subprocess.STDOUT
76
stderr = subprocess.PIPE
77
command = ["quilt"] + args
78
trace.mutter("running: %r", command)
79
if not os.path.isdir(working_dir):
80
raise AssertionError("%s is not a valid directory" % working_dir)
82
proc = subprocess.Popen(command, cwd=working_dir, env=env,
83
stdin=subprocess.PIPE, preexec_fn=subprocess_setup,
84
stdout=subprocess.PIPE, stderr=stderr)
86
if e.errno != errno.ENOENT:
88
raise errors.BzrError("quilt is not installed, please install it")
89
output = proc.communicate()
90
if proc.returncode not in (0, 2):
91
raise QuiltError(proc.returncode, output[0], output[1])
97
def quilt_pop_all(working_dir, patches_dir=None, series_file=None, quiet=None):
100
:param working_dir: Directory to work in
101
:param patches_dir: Optional patches directory
102
:param series_file: Optional series file
104
return run_quilt(["pop", "-a"], working_dir=working_dir, patches_dir=patches_dir, series_file=series_file, quiet=quiet)
107
def quilt_push_all(working_dir, patches_dir=None, series_file=None, quiet=None):
110
:param working_dir: Directory to work in
111
:param patches_dir: Optional patches directory
112
:param series_file: Optional series file
114
return run_quilt(["push", "-a"], working_dir=working_dir, patches_dir=patches_dir, series_file=series_file, quiet=quiet)
117
def quilt_applied(working_dir, patches_dir=None, series_file=None):
118
"""Find the list of applied quilt patches.
120
:param working_dir: Directory to work in
121
:param patches_dir: Optional patches directory
122
:param series_file: Optional series file
125
return run_quilt(["applied"], working_dir=working_dir, patches_dir=patches_dir, series_file=series_file).splitlines()
126
except QuiltError, e:
132
def quilt_unapplied(working_dir, patches_dir=None, series_file=None):
133
"""Find the list of unapplied quilt patches.
135
:param working_dir: Directory to work in
136
:param patches_dir: Optional patches directory
137
:param series_file: Optional series file
140
return run_quilt(["unapplied"], working_dir=working_dir,
141
patches_dir=patches_dir, series_file=series_file).splitlines()
142
except QuiltError, e:
148
def quilt_series(working_dir, patches_dir=None, series_file=None):
149
"""Find the list of patches.
151
:param working_dir: Directory to work in
152
:param patches_dir: Optional patches directory
153
:param series_file: Optional series file
155
return run_quilt(["series"], working_dir=working_dir, patches_dir=patches_dir, series_file=series_file).splitlines()