2
0

protocol.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # protocol.py -- Shared parts of the git protocols
  2. # Copryight (C) 2008 John Carr <john.carr@unrouted.co.uk>
  3. # Copyright (C) 2008 Jelmer Vernooij <jelmer@samba.org>
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; version 2
  8. # or (at your option) any later version of the License.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. # MA 02110-1301, USA.
  19. """Generic functions for talking the git smart server protocol."""
  20. import socket
  21. from dulwich.errors import (
  22. HangupException,
  23. GitProtocolError,
  24. )
  25. TCP_GIT_PORT = 9418
  26. class ProtocolFile(object):
  27. """
  28. Some network ops are like file ops. The file ops expect to operate on
  29. file objects, so provide them with a dummy file.
  30. """
  31. def __init__(self, read, write):
  32. self.read = read
  33. self.write = write
  34. def tell(self):
  35. pass
  36. def close(self):
  37. pass
  38. class Protocol(object):
  39. def __init__(self, read, write, report_activity=None):
  40. self.read = read
  41. self.write = write
  42. self.report_activity = report_activity
  43. def read_pkt_line(self):
  44. """
  45. Reads a 'pkt line' from the remote git process
  46. :return: The next string from the stream
  47. """
  48. try:
  49. sizestr = self.read(4)
  50. if not sizestr:
  51. raise HangupException()
  52. size = int(sizestr, 16)
  53. if size == 0:
  54. if self.report_activity:
  55. self.report_activity(4, 'read')
  56. return None
  57. if self.report_activity:
  58. self.report_activity(size, 'read')
  59. return self.read(size-4)
  60. except socket.error, e:
  61. raise GitProtocolError(e)
  62. def read_pkt_seq(self):
  63. pkt = self.read_pkt_line()
  64. while pkt:
  65. yield pkt
  66. pkt = self.read_pkt_line()
  67. def write_pkt_line(self, line):
  68. """
  69. Sends a 'pkt line' to the remote git process
  70. :param line: A string containing the data to send
  71. """
  72. try:
  73. if line is None:
  74. self.write("0000")
  75. if self.report_activity:
  76. self.report_activity(4, 'write')
  77. else:
  78. self.write("%04x%s" % (len(line)+4, line))
  79. if self.report_activity:
  80. self.report_activity(4+len(line), 'write')
  81. except socket.error, e:
  82. raise GitProtocolError(e)
  83. def write_file(self):
  84. class ProtocolFile(object):
  85. def __init__(self, proto):
  86. self._proto = proto
  87. self._offset = 0
  88. def write(self, data):
  89. self._proto.write(data)
  90. self._offset += len(data)
  91. def tell(self):
  92. return self._offset
  93. def close(self):
  94. pass
  95. return ProtocolFile(self)
  96. def write_sideband(self, channel, blob):
  97. """
  98. Write data to the sideband (a git multiplexing method)
  99. :param channel: int specifying which channel to write to
  100. :param blob: a blob of data (as a string) to send on this channel
  101. """
  102. # a pktline can be a max of 65520. a sideband line can therefore be
  103. # 65520-5 = 65515
  104. # WTF: Why have the len in ASCII, but the channel in binary.
  105. while blob:
  106. self.write_pkt_line("%s%s" % (chr(channel), blob[:65515]))
  107. blob = blob[65515:]
  108. def send_cmd(self, cmd, *args):
  109. """
  110. Send a command and some arguments to a git server
  111. Only used for git://
  112. :param cmd: The remote service to access
  113. :param args: List of arguments to send to remove service
  114. """
  115. self.write_pkt_line("%s %s" % (cmd, "".join(["%s\0" % a for a in args])))
  116. def read_cmd(self):
  117. """
  118. Read a command and some arguments from the git client
  119. Only used for git://
  120. :return: A tuple of (command, [list of arguments])
  121. """
  122. line = self.read_pkt_line()
  123. splice_at = line.find(" ")
  124. cmd, args = line[:splice_at], line[splice_at+1:]
  125. assert args[-1] == "\x00"
  126. return cmd, args[:-1].split(chr(0))
  127. def extract_capabilities(text):
  128. """Extract a capabilities list from a string, if present.
  129. :param text: String to extract from
  130. :return: Tuple with text with capabilities removed and list of
  131. capabilities or None (if no capabilities were present.
  132. """
  133. if not "\0" in text:
  134. return text, None
  135. capabilities = text.split("\0")
  136. return (capabilities[0], capabilities[1:])