本文整理汇总了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.
#.........这里部分代码省略.........