本文整理汇总了Python中dulwich.protocol.Protocol.read方法的典型用法代码示例。如果您正苦于以下问题:Python Protocol.read方法的具体用法?Python Protocol.read怎么用?Python Protocol.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dulwich.protocol.Protocol
的用法示例。
在下文中一共展示了Protocol.read方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: GitClient
# 需要导入模块: from dulwich.protocol import Protocol [as 别名]
# 或者: from dulwich.protocol.Protocol import read [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.
"""
old_refs, server_capabilities = self.read_refs()
new_refs = determine_wants(old_refs)
if not new_refs:
self.proto.write_pkt_line(None)
return {}
want = []
have = [x for x in old_refs.values() if not x == "0" * 40]
sent_capabilities = False
for refname in set(new_refs.keys() + old_refs.keys()):
old_sha1 = old_refs.get(refname, "0" * 40)
new_sha1 = new_refs.get(refname, "0" * 40)
if old_sha1 != new_sha1:
if sent_capabilities:
self.proto.write_pkt_line("%s %s %s" % (old_sha1, new_sha1, refname))
else:
self.proto.write_pkt_line("%s %s %s\0%s" % (old_sha1, new_sha1, refname, self.capabilities()))
sent_capabilities = True
if not new_sha1 in (have, "0" * 40):
want.append(new_sha1)
self.proto.write_pkt_line(None)
if not want:
return new_refs
objects = generate_pack_contents(have, want)
(entries, sha) = write_pack_data(self.proto.write_file(), objects, len(objects))
# read the final confirmation sha
client_sha = self.proto.read(20)
if not client_sha in (None, "", sha):
raise ChecksumMismatch(sha, client_sha)
return new_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
assert isinstance(wants, list) and type(wants[0]) == str
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":
#.........这里部分代码省略.........