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


Python Protocol.read_pkt_line方法代码示例

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


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

示例1: GitClient

# 需要导入模块: from protocol import Protocol [as 别名]
# 或者: from protocol.Protocol import read_pkt_line [as 别名]
class GitClient(object):
    """Git smart server client.

    """

    def __init__(self, can_read, read, write, thin_packs=True, 
        report_activity=None):
        """Create a new GitClient instance.

        :param can_read: Function that returns True if there is data available
            to be read.
        :param read: Callback for reading data, takes number of bytes to read
        :param write: Callback for writing data
        :param thin_packs: Whether or not thin packs should be retrieved
        :param report_activity: Optional callback for reporting transport
            activity.
        """
        self.proto = Protocol(read, write, report_activity)
        self._can_read = can_read
        self._capabilities = list(CAPABILITIES)
        #if thin_packs:
        #    self._capabilities.append("thin-pack")

    def capabilities(self):
        return " ".join(self._capabilities)

    def read_refs(self):
        server_capabilities = None
        refs = {}
        # Receive refs from server
        for pkt in self.proto.read_pkt_seq():
            (sha, ref) = pkt.rstrip("\n").split(" ", 1)
            if server_capabilities is None:
                (ref, server_capabilities) = extract_capabilities(ref)
            refs[ref] = sha
        return refs, server_capabilities

    def send_pack(self, path, determine_wants, generate_pack_contents):
        """Upload a pack to a remote repository.

        :param path: Repository path
        :param generate_pack_contents: Function that can return the shas of the 
            objects to upload.
        """
        refs, server_capabilities = self.read_refs()
        changed_refs = determine_wants(refs)
        if not changed_refs:
            self.proto.write_pkt_line(None)
            return {}
        want = []
        have = []
        sent_capabilities = False
        for changed_ref, new_sha1 in changed_refs.iteritems():
            old_sha1 = refs.get(changed_ref, "0" * 40)
            if sent_capabilities:
                self.proto.write_pkt_line("%s %s %s" % (old_sha1, new_sha1, changed_ref))
            else:
                self.proto.write_pkt_line("%s %s %s\0%s" % (old_sha1, new_sha1, changed_ref, self.capabilities()))
                sent_capabilities = True
            want.append(new_sha1)
            if old_sha1 != "0"*40:
                have.append(old_sha1)
        self.proto.write_pkt_line(None)
        objects = generate_pack_contents(want, have)
        (entries, sha) = write_pack_data(self.proto.write_file(), objects, len(objects))
        self.proto.write(sha)
        
        # read the final confirmation sha
        client_sha = self.proto.read(20)
        # TODO : do something here that doesn't break
        #if not client_sha in (None, sha):
        # print "warning: local %s and server %s differ" % (sha_to_hex(sha), sha_to_hex(client_sha))
            
        return changed_refs

    def fetch_pack(self, path, determine_wants, graph_walker, pack_data,
                   progress):
        """Retrieve a pack from a git smart server.

        :param determine_wants: Callback that returns list of commits to fetch
        :param graph_walker: Object with next() and ack().
        :param pack_data: Callback called for each bit of data in the pack
        :param progress: Callback for progress reports (strings)
        """
        (refs, server_capabilities) = self.read_refs()
        wants = determine_wants(refs)
        if not wants:
            self.proto.write_pkt_line(None)
            return refs
        self.proto.write_pkt_line("want %s %s\n" % (wants[0], self.capabilities()))
        for want in wants[1:]:
            self.proto.write_pkt_line("want %s\n" % want)
        self.proto.write_pkt_line(None)
        have = graph_walker.next()
        while have:
            self.proto.write_pkt_line("have %s\n" % have)
            if self._can_read():
                pkt = self.proto.read_pkt_line()
                parts = pkt.rstrip("\n").split(" ")
                if parts[0] == "ACK":
#.........这里部分代码省略.........
开发者ID:SRabbelier,项目名称:hg-git,代码行数:103,代码来源:client.py


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