/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: 2019-06-29 13:16:26 UTC
  • mto: This revision was merged to the branch mainline in revision 7376.
  • Revision ID: jelmer@jelmer.uk-20190629131626-qioafloyemhdbm4w
Remove Tree.get_root_id() in favour of Tree.path2id('').

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, quiet=None):
 
163
    """Push a patch.
 
164
 
 
165
    :param working_dir: Directory to work in
 
166
    :param patch: Patch to push
 
167
    :param patches_dir: Optional patches directory
 
168
    :param series_file: Optional series file
 
169
    """
 
170
    return run_quilt(
 
171
        ["push", patch], working_dir=working_dir,
 
172
        patches_dir=patches_dir, series_file=series_file, quiet=quiet)
 
173
 
 
174
 
 
175
def quilt_upgrade(working_dir):
 
176
    return run_quilt(["upgrade"], working_dir=working_dir)
 
177
 
 
178
 
 
179
def quilt_applied(tree):
 
180
    """Find the list of applied quilt patches.
 
181
 
 
182
    """
 
183
    try:
 
184
        return [patch.rstrip(b"\n").decode(osutils._fs_enc)
 
185
                for patch in tree.get_file_lines(".pc/applied-patches")
 
186
                if patch.strip() != b""]
 
187
    except errors.NoSuchFile:
 
188
        return []
 
189
    except (IOError, OSError) as e:
 
190
        if e.errno == errno.ENOENT:
 
191
            # File has already been removed
 
192
            return []
 
193
        raise
 
194
 
 
195
 
 
196
def quilt_unapplied(working_dir, patches_dir=None, series_file=None):
 
197
    """Find the list of unapplied quilt patches.
 
198
 
 
199
    :param working_dir: Directory to work in
 
200
    :param patches_dir: Optional patches directory
 
201
    :param series_file: Optional series file
 
202
    """
 
203
    try:
 
204
        unapplied_patches = run_quilt(
 
205
            ["unapplied"],
 
206
            working_dir=working_dir, patches_dir=patches_dir,
 
207
            series_file=series_file).splitlines()
 
208
        patch_basenames = []
 
209
        for patch in unapplied_patches:
 
210
            patch = os.path.basename(patch)
 
211
            patch_basenames.append(patch.decode(osutils._fs_enc))
 
212
        return patch_basenames
 
213
    except QuiltError as e:
 
214
        if e.retcode == 1:
 
215
            return []
 
216
        raise
 
217
 
 
218
 
 
219
def quilt_series(tree, series_path):
 
220
    """Find the list of patches.
 
221
 
 
222
    :param tree: Tree to read from
 
223
    """
 
224
    try:
 
225
        return [patch.rstrip(b"\n").decode(osutils._fs_enc) for patch in
 
226
                tree.get_file_lines(series_path)
 
227
                if patch.strip() != b""]
 
228
    except (IOError, OSError) as e:
 
229
        if e.errno == errno.ENOENT:
 
230
            # File has already been removed
 
231
            return []
 
232
        raise
 
233
    except errors.NoSuchFile:
 
234
        return []