protocol.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. from errors import HangupException
  20. TCP_GIT_PORT = 9418
  21. class ProtocolFile(object):
  22. """
  23. Some network ops are like file ops. The file ops expect to operate on
  24. file objects, so provide them with a dummy file.
  25. """
  26. def __init__(self, read, write):
  27. self.read = read
  28. self.write = write
  29. def tell(self):
  30. pass
  31. def close(self):
  32. pass
  33. class Protocol(object):
  34. def __init__(self, read, write):
  35. self.read = read
  36. self.write = write
  37. def read_pkt_line(self):
  38. """
  39. Reads a 'pkt line' from the remote git process
  40. :return: The next string from the stream
  41. """
  42. sizestr = self.read(4)
  43. if not sizestr:
  44. raise HangupException()
  45. size = int(sizestr, 16)
  46. if size == 0:
  47. return None
  48. return self.read(size-4)
  49. def read_pkt_seq(self):
  50. pkt = self.read_pkt_line()
  51. while pkt:
  52. yield pkt
  53. pkt = self.read_pkt_line()
  54. def write_pkt_line(self, line):
  55. """
  56. Sends a 'pkt line' to the remote git process
  57. :param line: A string containing the data to send
  58. """
  59. if line is None:
  60. self.write("0000")
  61. else:
  62. self.write("%04x%s" % (len(line)+4, line))
  63. def write_sideband(self, channel, blob):
  64. """
  65. Write data to the sideband (a git multiplexing method)
  66. :param channel: int specifying which channel to write to
  67. :param blob: a blob of data (as a string) to send on this channel
  68. """
  69. # a pktline can be a max of 65520. a sideband line can therefore be
  70. # 65520-5 = 65515
  71. # WTF: Why have the len in ASCII, but the channel in binary.
  72. while blob:
  73. self.write_pkt_line("%s%s" % (chr(channel), blob[:65515]))
  74. blob = blob[65515:]
  75. def send_cmd(self, cmd, *args):
  76. """
  77. Send a command and some arguments to a git server
  78. Only used for git://
  79. :param cmd: The remote service to access
  80. :param args: List of arguments to send to remove service
  81. """
  82. self.write_pkt_line("%s %s" % (cmd, "".join(["%s\0" % a for a in args])))
  83. def read_cmd(self):
  84. """
  85. Read a command and some arguments from the git client
  86. Only used for git://
  87. :return: A tuple of (command, [list of arguments])
  88. """
  89. line = self.read_pkt_line()
  90. splice_at = line.find(" ")
  91. cmd, args = line[:splice_at], line[splice_at+1:]
  92. return cmd, args.split(chr(0))
  93. def extract_capabilities(text):
  94. if not "\0" in text:
  95. return text, None
  96. capabilities = text.split("\0")
  97. return (capabilities[0], capabilities[1:])