当前位置: 首页>>代码示例>>Python>>正文


Python Repo.close方法代码示例

本文整理汇总了Python中git.Repo.close方法的典型用法代码示例。如果您正苦于以下问题:Python Repo.close方法的具体用法?Python Repo.close怎么用?Python Repo.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在git.Repo的用法示例。


在下文中一共展示了Repo.close方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from git import Repo [as 别名]
# 或者: from git.Repo import close [as 别名]
class DepositionRepo:
    """ A class to interface with git repos for depositions.

    You *MUST* use the 'with' statement when using this class to ensure that
    changes are committed."""

    def __init__(self, uuid, initialize=False):
        self._repo = None
        self._uuid = uuid
        self._initialize = initialize
        self._entry_dir = None
        self._modified_files = False
        self._live_metadata = None
        self._original_metadata = None
        self._lock_path = os.path.join(querymod.configuration['repo_path'], str(uuid), '.git', 'api.lock')

    def __enter__(self):
        """ Get a session cookie to use for future requests. """

        self._entry_dir = os.path.join(querymod.configuration['repo_path'], str(self._uuid))
        if not os.path.exists(self._entry_dir) and not self._initialize:
            raise querymod.RequestError('No deposition with that ID exists!')
        try:
            if self._initialize:
                self._repo = Repo.init(self._entry_dir)
                with open(self._lock_path, "w") as f:
                    f.write(str(os.getpid()))
                self._repo.config_writer().set_value("user", "name", "BMRBDep").release()
                self._repo.config_writer().set_value("user", "email", "[email protected]").release()
                os.mkdir(os.path.join(self._entry_dir, 'data_files'))
            else:
                counter = 100
                while os.path.exists(self._lock_path):
                    counter -= 1
                    time.sleep(random.random())
                    if counter <= 0:
                        raise querymod.ServerError('Could not acquire entry directory lock.')
                with open(self._lock_path, "w") as f:
                    f.write(str(os.getpid()))
                self._repo = Repo(self._entry_dir)
        except NoSuchPathError:
            raise querymod.RequestError("'%s' is not a valid deposition ID." % self._uuid,
                                        status_code=404)

        return self

    def __exit__(self, exc_type, exc_value, traceback):
        """ End the current session."""

        # If nothing changed the commit won't do anything
        self.commit("Repo closed with changed but without a manual commit... Potential software bug.")
        self._repo.close()
        self._repo.__del__()
        try:
            os.unlink(self._lock_path)
        except OSError:
            raise querymod.ServerError('Could not remove entry lock file.')

    @property
    def metadata(self):
        """ Return the metadata dictionary. """

        if not self._live_metadata:
            self._live_metadata = json.loads(self.get_file('submission_info.json'))
            self._original_metadata = self._live_metadata.copy()
        return self._live_metadata

    def get_entry(self):
        """ Return the NMR-STAR entry for this entry. """

        entry_location = os.path.join(self._entry_dir, 'entry.str')

        try:
            return querymod.pynmrstar.Entry.from_file(entry_location)
        except Exception as e:
            raise querymod.ServerError('Error loading an entry!\nError: %s\nEntry location:%s\nEntry text:\n%s' %
                                       (e.message, entry_location, open(entry_location, "r").read()))

    def write_entry(self, entry):
        """ Save an entry in the standard place. """

        try:
            self.metadata['last_ip'] = flask.request.environ['REMOTE_ADDR']
        except RuntimeError:
            pass
        self.write_file('entry.str', str(entry), root=True)

    def get_file(self, filename, raw_file=False, root=True):
        """ Returns the current version of a file from the repo. """

        filename = secure_filename(filename)
        if not root:
            filename = os.path.join('data_files', filename)
        try:
            file_obj = open(os.path.join(self._entry_dir, filename), "r")
            if raw_file:
                return file_obj
            else:
                return file_obj.read()
        except IOError:
#.........这里部分代码省略.........
开发者ID:uwbmrb,项目名称:BMRB-API,代码行数:103,代码来源:depositions.py


注:本文中的git.Repo.close方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。