2
0

protocol.py 3.5 KB

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