# Path: quilt/utils.py
# class Process(object):
# 
#     def __init__(self, cmd):
#         self.cmd = cmd
# 
#     def run(self, suppress_output=False, inputdata=None, **kw):
#         """Run command as a subprocess and wait until it is finished.
# 
#         The command should be given as a list of strings to avoid problems
#         with shell quoting.  If the command exits with a return code other
#         than 0, a SubprocessError is raised.
#         """
#         if inputdata is not None:
#             kw["stdin"] = subprocess.PIPE
#         if suppress_output:
#             kw["stdout"] = open(os.devnull, "w")
#             kw["stderr"] = kw["stdout"]
#         try:
#             try:
#                 process = subprocess.Popen(self.cmd, **kw)
#             finally:
#                 if suppress_output:
#                     kw["stdout"].close()
#         except OSError as e:
#             msg = "Failed starting command {!r}: {}".format(self.cmd, e)
#             raise QuiltError(msg)
# 
#         if inputdata is not None:
#             process.stdin.write(inputdata)
#             process.stdin.close()
#         ret = process.wait()
#         if ret != 0:
#             raise SubprocessError(self.cmd, ret)
# 
# class DirectoryParam(Param):
#     """ Decorator class to change parameters of methods and functions to a
#     Directory class if it's not a Directory yet.
# 
#     Usage: @DirectoryParam(["paramname1", "paramname2"])
#     """
# 
#     cls = Directory
# 
# class _EqBase(object):
#     """ Helpers for defining __eq__ in Python < 3
# 
#     This class prevents inheriting the default Python 2 hashing
#     implementation, which would give different hashes for different but
#     equivalent objects. This avoids a DeprecationWarning and imitates how
#     Python 3 disables hashing whenever __eq__ is overridden.
#     """
#     __hash__ = None
# 
#     def __ne__(self, other):
#         """ Defers to __eq__, imitating Python 3 """
#         return not self.__eq__(other)
# 
# class File(object):
# 
#     def __init__(self, filename):
#         self.filename = filename
# 
#     def exists(self):
#         return os.path.exists(self.filename)
# 
#     def delete(self):
#         os.remove(self.filename)
# 
#     def delete_if_exists(self):
#         if self.exists():
#             self.delete()
# 
#     def touch(self):
#         """ 'Touch' a file. Creates an empty file. """
#         open(self.filename, "w").close()
# 
#     def link(self, link):
#         """ Create hard link as link to this file """
#         if isinstance(link, File):
#             link = link.filename
#         os.link(self.filename, link)
# 
#     def copy(self, dest):
#         """ Copy file to destination """
#         if isinstance(dest, File):
#             dest_dir = dest.get_directory()
#             dest_dir.create()
#             dest = dest.filename
#         elif isinstance(dest, Directory):
#             dest = dest.dirname
# 
#         shutil.copy2(self.filename, dest)
# 
#     def is_empty(self):
#         """ Returns True if the size of the file is 0 """
#         st = os.stat(self.filename)
#         return st.st_size == 0
# 
#     def is_link(self):
#         return os.path.islink(self.filename)
# 
#     def get_name(self):
#         """ Returns the name of this file """
#         return self.filename
# 
#     def get_basename(self):
#         """ Return the base name of the file e.g. file if filename is /foo/file.
#         If filename doesn't contain a directory get_name and get_basename
#         are equal.
#         """
#         return os.path.basename(self.filename)
# 
#     def get_directory(self):
#         """ Returns the directory where the file is placed in or None if the
#         path to the file doesn't contain a directory
#         """
#         dirname = os.path.dirname(self.filename)
#         if dirname:
#             return Directory(dirname)
#         else:
#             return None
# 
#     def get_mode(self):
#         return os.stat(self.filename).st_mode
# 
#     def get_absfile(self):
#         """ Returns the file with an absolute path
#         """
#         return File(os.path.abspath(self.filename))
# 
#     def get_basefile(self):
#         """ Returns the file without a path
#         """
#         return File(self.get_basename())
# 
#     def open(self, mode="r", buffering=None):
#         return open(self.filename, mode, buffering)
# 
#     def __str__(self):
#         return self.get_name()
# 
# class FileParam(Param):
#     """ Decorator class to change parameters of methods and functions to a
#     File class if it's not a File yet.
# 
#     Usage: @FileParam(["paramname1", "paramname2"])
#     """
# 
#     cls = File
# 
# class SubprocessError(QuiltError):
# 
#     def __init__(self, command, returncode, output=None):
#         self.command = command
#         self.returncode = returncode
#         self.output = output
# 
#     def get_returncode(self):
#         return self.returncode
# 
#     def __str__(self):
#         retval = "Command %s finished with return code %d" % (self.command,
#                                                               self.returncode)
#         if self.output:
#             retval += "; output was: '%s'" % self.output
#         return retval


import os
import os.path
from quilt.utils import Process, DirectoryParam, _EqBase, File, FileParam, \
                        SubprocessError

        if left_label:
            cmd.append("--label")
            cmd.append(left_label)

        if right_label:
            if not left_label:
                cmd.append("--label")
                cmd.append(self.right.get_name())
            cmd.append("--label")
            cmd.append(right_label)

        cmd.append(self.left.get_name())
        cmd.append(self.right.get_name())

        try:
            Process(cmd).run(cwd=cwd, stdout=fd)
        except SubprocessError as e:
            if e.get_returncode() > 1:
                raise e

    def equal(self, cwd):
        """ Returns True if left and right are equal
        """
        cmd = ["diff"]
        cmd.append("-q")
        cmd.append(self.left.get_name())
        cmd.append(self.right.get_name())

        try:

# Path: quilt/utils.py
# class Process(object):
# 
#     def __init__(self, cmd):
#         self.cmd = cmd
# 
#     def run(self, suppress_output=False, inputdata=None, **kw):
#         """Run command as a subprocess and wait until it is finished.
# 
#         The command should be given as a list of strings to avoid problems
#         with shell quoting.  If the command exits with a return code other
#         than 0, a SubprocessError is raised.
#         """
#         if inputdata is not None:
#             kw["stdin"] = subprocess.PIPE
#         if suppress_output:
#             kw["stdout"] = open(os.devnull, "w")
#             kw["stderr"] = kw["stdout"]
#         try:
#             try:
#                 process = subprocess.Popen(self.cmd, **kw)
#             finally:
#                 if suppress_output:
#                     kw["stdout"].close()
#         except OSError as e:
#             msg = "Failed starting command {!r}: {}".format(self.cmd, e)
#             raise QuiltError(msg)
# 
#         if inputdata is not None:
#             process.stdin.write(inputdata)
#             process.stdin.close()
#         ret = process.wait()
#         if ret != 0:
#             raise SubprocessError(self.cmd, ret)
# 
# class DirectoryParam(Param):
#     """ Decorator class to change parameters of methods and functions to a
#     Directory class if it's not a Directory yet.
# 
#     Usage: @DirectoryParam(["paramname1", "paramname2"])
#     """
# 
#     cls = Directory
# 
# class _EqBase(object):
#     """ Helpers for defining __eq__ in Python < 3
# 
#     This class prevents inheriting the default Python 2 hashing
#     implementation, which would give different hashes for different but
#     equivalent objects. This avoids a DeprecationWarning and imitates how
#     Python 3 disables hashing whenever __eq__ is overridden.
#     """
#     __hash__ = None
# 
#     def __ne__(self, other):
#         """ Defers to __eq__, imitating Python 3 """
#         return not self.__eq__(other)
# 
# class File(object):
# 
#     def __init__(self, filename):
#         self.filename = filename
# 
#     def exists(self):
#         return os.path.exists(self.filename)
# 
#     def delete(self):
#         os.remove(self.filename)
# 
#     def delete_if_exists(self):
#         if self.exists():
#             self.delete()
# 
#     def touch(self):
#         """ 'Touch' a file. Creates an empty file. """
#         open(self.filename, "w").close()
# 
#     def link(self, link):
#         """ Create hard link as link to this file """
#         if isinstance(link, File):
#             link = link.filename
#         os.link(self.filename, link)
# 
#     def copy(self, dest):
#         """ Copy file to destination """
#         if isinstance(dest, File):
#             dest_dir = dest.get_directory()
#             dest_dir.create()
#             dest = dest.filename
#         elif isinstance(dest, Directory):
#             dest = dest.dirname
# 
#         shutil.copy2(self.filename, dest)
# 
#     def is_empty(self):
#         """ Returns True if the size of the file is 0 """
#         st = os.stat(self.filename)
#         return st.st_size == 0
# 
#     def is_link(self):
#         return os.path.islink(self.filename)
# 
#     def get_name(self):
#         """ Returns the name of this file """
#         return self.filename
# 
#     def get_basename(self):
#         """ Return the base name of the file e.g. file if filename is /foo/file.
#         If filename doesn't contain a directory get_name and get_basename
#         are equal.
#         """
#         return os.path.basename(self.filename)
# 
#     def get_directory(self):
#         """ Returns the directory where the file is placed in or None if the
#         path to the file doesn't contain a directory
#         """
#         dirname = os.path.dirname(self.filename)
#         if dirname:
#             return Directory(dirname)
#         else:
#             return None
# 
#     def get_mode(self):
#         return os.stat(self.filename).st_mode
# 
#     def get_absfile(self):
#         """ Returns the file with an absolute path
#         """
#         return File(os.path.abspath(self.filename))
# 
#     def get_basefile(self):
#         """ Returns the file without a path
#         """
#         return File(self.get_basename())
# 
#     def open(self, mode="r", buffering=None):
#         return open(self.filename, mode, buffering)
# 
#     def __str__(self):
#         return self.get_name()
# 
# class FileParam(Param):
#     """ Decorator class to change parameters of methods and functions to a
#     File class if it's not a File yet.
# 
#     Usage: @FileParam(["paramname1", "paramname2"])
#     """
# 
#     cls = File
# 
# class SubprocessError(QuiltError):
# 
#     def __init__(self, command, returncode, output=None):
#         self.command = command
#         self.returncode = returncode
#         self.output = output
# 
#     def get_returncode(self):
#         return self.returncode
# 
#     def __str__(self):
#         retval = "Command %s finished with return code %d" % (self.command,
#                                                               self.returncode)
#         if self.output:
#             retval += "; output was: '%s'" % self.output
#         return retval


# Path: quilt/patch.py
import os
import os.path
from quilt.utils import Process, DirectoryParam, _EqBase, File, FileParam, \
                        SubprocessError


        if left_label:
            cmd.append("--label")
            cmd.append(left_label)

        if right_label:
            if not left_label:
                cmd.append("--label")
                cmd.append(self.right.get_name())
            cmd.append("--label")
            cmd.append(right_label)

        cmd.append(self.left.get_name())
        cmd.append(self.right.get_name())

        try:
            Process(cmd).run(cwd=cwd, stdout=fd)
        except SubprocessError as e:
            if e.get_returncode() > 1:
                raise e

    def equal(self, cwd):
        """ Returns True if left and right are equal
        """
        cmd = ["diff"]
        cmd.append("-q")
        cmd.append(self.left.get_name())
        cmd.append(self.right.get_name())

        try:

            Process(cmd).run(cwd=cwd, suppress_output=True)