\_SB_.PCI0.SFB_
      \_SB_.PCI0.S13_
      \_SB_.PCI0.S10_.S00_.SF0_
  A   ˆl–Ü4ý( ©”u( 	•íÀà™S�ÿ�þC(Æ³rˆ�†A„% ?ŸÂƒx sÁ_ŠI|¥�+Wé3ž¯ÁÀ ª    #    quilt.py -- Quilt patch handling
#    Copyright (C) 2011 Canonical Ltd.
#    Copyright (C) 2019 Jelmer Verooij <jelmer@jelmer.uk>
#
#    Breezy is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    Breezy is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Breezy; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#

"""Quilt patch handling."""

import errno
import os
import signal
import subprocess
from ... import (
    errors,
    osutils,
    trace,
    transport as _mod_transport,
    )


DEFAULT_PATCHES_DIR = 'patches'
DEFAULT_SERIES_FILE = 'series'


class QuiltError(errors.BzrError):

    _fmt = "An error (%(retcode)d) occurred running quilt: %(stderr)s%(extra)s"

    def __init__(self, retcode, stdout, stderr):
        self.retcode = retcode
        self.stderr = stderr
        if stdout is not None:
            self.extra = "\n\n%s" % stdout
        else:
            self.extra = ""
        self.stdout = stdout


class QuiltNotInstalled(errors.BzrError):

    _fmt = "Quilt is not installed."


def run_quilt(
        args, working_dir, series_file=None, patches_dir=None, quiet=None):
    """Run quilt.

    :param args: Arguments to quilt
    :param working_dir: Working dir
    :param series_file: Optional path to the series file
    :param patches_dir: Optional path to the patches
    :param quilt: Whether to be quiet (quilt stderr not to terminal)
    :raise QuiltError: When running quilt fails
    """
    def subprocess_setup():
        signal.signal(signal.SIGPIPE, signal.SIG_DFL)
    env = {}
    if patches_dir is not None:
        env["QUILT_PATCHES"] = patches_dir
    else:
        env["QUILT_PATCHES"] = os.path.join(working_dir, DEFAULT_PATCHES_DIR)
    if series_file is not None:
        env["QUILT_SERIES"] = series_file
    else:
        env["QUILT_SERIES"] = DEFAULT_SERIES_FILE
    # Hide output if -q is in use.
    if quiet is None:
        quiet = trace.is_quiet()
    if not quiet:
        stderr = subprocess.STDOUT
    else:
        stderr = subprocess.PIPE
    quilt_path = osutils.find_executable_on_path("quilt")
    if quilt_path is None:
        raise QuiltNotInstalled()
    command = [quilt_path] + args
    trace.mutter("running: %r", command)
    if not os.path.isdir(working_dir):
        raise AssertionError("%s is not a valid directory" % working_dir)
    try:
        proc = subprocess.Popen(
            command, cwd=working_dir, env=env,
            stdin=subprocess.PIPE, preexec_fn=subprocess_setup,
            stdout=subprocess.PIPE, stderr=stderr)
    except FileNotFoundError as e:
        raise QuiltNotInstalled() from e
    (stdout, stderr) = proc.communicate()
    if proc.returncode not in (0, 2):
        if stdout is not None:
            stdout = stdout.decode()
        if stderr is not None:
            stderr = stderr.decode()
        raise QuiltError(proc.returncode, stdout, stderr)
    if stdout is None:
        return ""
    return stdout


def quilt_pop_all(
        working_dir, patches_dir=None, series_file=None, quiet=None,
        force=False, refresh=False):
    """Pop all patches.

    :param working_dir: Directory to work in
    :param patches_dir: Optional patches directory
    :param series_file: Optional series file
    """
    args = ["pop", "-a"]
    if force:
        args.append("-f")
    if refresh:
        args.append("--refresh")
    return run_quilt(
        args, working_dir=work