~bzr-builddeb-hackers/bzr-builddeb/trunk

« back to all changes in this revision

Viewing changes to quilt.py

  • Committer: Jelmer Vernooij
  • Date: 2012-01-04 23:28:23 UTC
  • mfrom: (678.1.3 quilt)
  • Revision ID: jelmer@samba.org-20120104232823-jwtn925z8awhsn9n
Merge support for running quilt.

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
#
 
4
#    This file is part of bzr-builddeb.
 
5
#
 
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.
 
10
#
 
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.
 
15
#
 
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
 
19
#
 
20
 
 
21
"""Quilt patch handling."""
 
22
 
 
23
from __future__ import absolute_import
 
24
 
 
25
import errno
 
26
import os
 
27
import signal
 
28
import subprocess
 
29
from bzrlib import (
 
30
    errors,
 
31
    trace,
 
32
    )
 
33
 
 
34
 
 
35
class QuiltError(errors.BzrError):
 
36
 
 
37
    _fmt = "An error (%(retcode)d) occurred running quilt: %(stderr)s%(extra)s"
 
38
 
 
39
    def __init__(self, retcode, stdout, stderr):
 
40
        self.retcode = retcode
 
41
        self.stderr = stderr
 
42
        if stdout is not None:
 
43
            self.extra = "\n\n%s" % stdout
 
44
        else:
 
45
            self.extra = ""
 
46
        self.stdout = stdout
 
47
 
 
48
 
 
49
def run_quilt(args, working_dir, series_file=None, patches_dir=None, quiet=None):
 
50
    """Run quilt.
 
51
 
 
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
 
58
    """
 
59
    def subprocess_setup():
 
60
        signal.signal(signal.SIGPIPE, signal.SIG_DFL)
 
61
    env = {}
 
62
    if patches_dir is not None:
 
63
        env["QUILT_PATCHES"] = patches_dir
 
64
    else:
 
65
        env["QUILT_PATCHES"] = os.path.join(working_dir, "debian", "patches")
 
66
    if series_file is not None:
 
67
        env["QUILT_SERIES"] = series_file
 
68
    else:
 
69
        env["QUILT_SERIES"] = os.path.join(env["QUILT_PATCHES"], "series")
 
70
    # Hide output if -q is in use.
 
71
    if quiet is None:
 
72
        quiet = trace.is_quiet()
 
73
    if quiet:
 
74
        stderr =  subprocess.STDOUT
 
75
    else:
 
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)
 
81
    try:
 
82
        proc = subprocess.Popen(command, cwd=working_dir, env=env,
 
83
                stdin=subprocess.PIPE, preexec_fn=subprocess_setup,
 
84
                stdout=subprocess.PIPE, stderr=stderr)
 
85
    except OSError, e:
 
86
        if e.errno != errno.ENOENT:
 
87
            raise
 
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])
 
92
    if output[0] is None:
 
93
        return ""
 
94
    return output[0]
 
95
 
 
96
 
 
97
def quilt_pop_all(working_dir, patches_dir=None, series_file=None, quiet=None):
 
98
    """Pop all patches.
 
99
 
 
100
    :param working_dir: Directory to work in
 
101
    :param patches_dir: Optional patches directory
 
102
    :param series_file: Optional series file
 
103
    """
 
104
    return run_quilt(["pop", "-a"], working_dir=working_dir, patches_dir=patches_dir, series_file=series_file, quiet=quiet)
 
105
 
 
106
 
 
107
def quilt_push_all(working_dir, patches_dir=None, series_file=None, quiet=None):
 
108
    """Push all patches.
 
109
 
 
110
    :param working_dir: Directory to work in
 
111
    :param patches_dir: Optional patches directory
 
112
    :param series_file: Optional series file
 
113
    """
 
114
    return run_quilt(["push", "-a"], working_dir=working_dir, patches_dir=patches_dir, series_file=series_file, quiet=quiet)
 
115
 
 
116
 
 
117
def quilt_applied(working_dir, patches_dir=None, series_file=None):
 
118
    """Find the list of applied quilt patches.
 
119
 
 
120
    :param working_dir: Directory to work in
 
121
    :param patches_dir: Optional patches directory
 
122
    :param series_file: Optional series file
 
123
    """
 
124
    try:
 
125
        return run_quilt(["applied"], working_dir=working_dir, patches_dir=patches_dir, series_file=series_file).splitlines()
 
126
    except QuiltError, e:
 
127
        if e.retcode == 1:
 
128
            return []
 
129
        raise
 
130
 
 
131
 
 
132
def quilt_unapplied(working_dir, patches_dir=None, series_file=None):
 
133
    """Find the list of unapplied quilt patches.
 
134
 
 
135
    :param working_dir: Directory to work in
 
136
    :param patches_dir: Optional patches directory
 
137
    :param series_file: Optional series file
 
138
    """
 
139
    try:
 
140
        return run_quilt(["unapplied"], working_dir=working_dir,
 
141
                patches_dir=patches_dir, series_file=series_file).splitlines()
 
142
    except QuiltError, e:
 
143
        if e.retcode == 1:
 
144
            return []
 
145
        raise
 
146
 
 
147
 
 
148
def quilt_series(working_dir, patches_dir=None, series_file=None):
 
149
    """Find the list of patches.
 
150
 
 
151
    :param working_dir: Directory to work in
 
152
    :param patches_dir: Optional patches directory
 
153
    :param series_file: Optional series file
 
154
    """
 
155
    return run_quilt(["series"], working_dir=working_dir, patches_dir=patches_dir, series_file=series_file).splitlines()
 
156