當前位置: 首頁>>代碼示例>>Python>>正文


Python Repository.apply_patch方法代碼示例

本文整理匯總了Python中repository.Repository.apply_patch方法的典型用法代碼示例。如果您正苦於以下問題:Python Repository.apply_patch方法的具體用法?Python Repository.apply_patch怎麽用?Python Repository.apply_patch使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在repository.Repository的用法示例。


在下文中一共展示了Repository.apply_patch方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: Stash

# 需要導入模塊: from repository import Repository [as 別名]
# 或者: from repository.Repository import apply_patch [as 別名]
class Stash(object):
    """This class manages the collection of patches that have been stashed from
    various repositories. It provides functionality to list all available
    patches, to add, remove, and show individual patches, as well as applying
    stashed patches on a repository.
    """

    STASH_PATH = os.path.expanduser('~/.stash')

    def __init__(self, path):
        """To instantiantate a stash, provide a path that points to a location
        somewhere in a repository.
        """
        # Check if the patches path exists, and in case it does not, create it.
        if not os.path.exists(self.STASH_PATH):
            os.mkdir(self.STASH_PATH)

        self.repository = Repository(path)

        super(Stash, self).__init__()

    @classmethod
    def _get_patch_path(cls, patch_name):
        """Returns the absolute path for patch *patch_name*."""
        return os.path.join(cls.STASH_PATH, patch_name) if patch_name else None

    @classmethod
    def get_patches(cls):
        """Returns the names of all stashed patches."""
        return sorted(os.listdir(cls.STASH_PATH))

    @classmethod
    def remove_patch(cls, patch_name):
        """Removes patch *patch_name* from the stash (in case it exists).

        :raises: :py:exc:`~stash.exception.StashException` in case *patch_name* does not exist.
        """
        try:
            os.unlink(cls._get_patch_path(patch_name))
        except:
            raise StashException("patch '%s' does not exist" % patch_name)

    @classmethod
    def get_patch(cls, patch_name):
        """Returns the contents of the specified patch *patch_name*.

        :raises: :py:exc:`~stash.exception.StashException` in case *patch_name* does not exist.
        """
        try:
            return open(cls._get_patch_path(patch_name), 'r').read()
        except:
            raise StashException("patch '%s' does not exist" % patch_name)

    def apply_patch(self, patch_name):
        """Applies the patch *patch_name* on to the current working directory in
        case the patch exists. In case applying the patch was successful, the
        patch is automatically removed from the stash. Returns ``True`` in case
        applying the patch was successful, otherwise ``False`` is returned.

        :raises: :py:exc:`~stash.exception.StashException` in case *patch_name* does not exist.
        """
        if patch_name in self.get_patches():
            patch_path = self._get_patch_path(patch_name)

            # Apply the patch, and determine the files that have been added and
            # removed.
            pre_file_status = self.repository.status()
            patch_return_code = self.repository.apply_patch(patch_path)
            changed_file_status = self.repository.status().difference(pre_file_status)

            # Determine all files that have been added.
            for status, file_name in changed_file_status:
                if status == FileStatus.Added:
                    self.repository.add([file_name])
                elif status == FileStatus.Removed:
                    self.repository.remove([file_name])

            if patch_return_code == 0:
                # Applying the patch succeeded, remove stashed patch.
                os.unlink(patch_path)

            return patch_return_code == 0
        else:
            raise StashException("patch '%s' does not exist" % patch_name)

    def create_patch(self, patch_name):
        """Creates a patch based on the changes in the current repository. In
        case the specified patch *patch_name* already exists, ask the user to
        overwrite the patch. In case creating the patch was successful, all
        changes in the current repository are reverted. Returns ``True`` in case
        a patch was created, and ``False`` otherwise.

        :raises: :py:exc:`~stash.exception.StashException` in case *patch_name* already exists.
        """
        # Raise an exception in case the specified patch already exists.
        patch_path = self._get_patch_path(patch_name)
        if os.path.exists(patch_path):
            raise StashException("patch '%s' already exists" % patch_name)

        # Determine the contents for the new patch.
#.........這裏部分代碼省略.........
開發者ID:ton,項目名稱:stash,代碼行數:103,代碼來源:stash.py


注:本文中的repository.Repository.apply_patch方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。