Support activity reporting.
[jelmer/dulwich-libgit2.git] / dulwich / protocol.py
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
20 """Generic functions for talking the git smart server protocol."""
21
22 import socket
23
24 from dulwich.errors import (
25     HangupException,
26     GitProtocolError,
27     )
28
29 TCP_GIT_PORT = 9418
30
31 class ProtocolFile(object):
32     """
33     Some network ops are like file ops. The file ops expect to operate on
34     file objects, so provide them with a dummy file.
35     """
36
37     def __init__(self, read, write):
38         self.read = read
39         self.write = write
40
41     def tell(self):
42         pass
43
44     def close(self):
45         pass
46
47
48 class Protocol(object):
49
50     def __init__(self, read, write, report_activity=None):
51         self.read = read
52         self.write = write
53         self.report_activity = report_activity
54
55     def read_pkt_line(self):
56         """
57         Reads a 'pkt line' from the remote git process
58
59         :return: The next string from the stream
60         """
61         try:
62             sizestr = self.read(4)
63             if not sizestr:
64                 raise HangupException()
65             size = int(sizestr, 16)
66             if size == 0:
67                 self.report_activity(4, 'read')
68                 return None
69             if self.report_activity:
70                 self.report_activity(size, 'read')
71             return self.read(size-4)
72         except socket.error, e:
73             raise GitProtocolError(e)
74
75     def read_pkt_seq(self):
76         pkt = self.read_pkt_line()
77         while pkt:
78             yield pkt
79             pkt = self.read_pkt_line()
80
81     def write_pkt_line(self, line):
82         """
83         Sends a 'pkt line' to the remote git process
84
85         :param line: A string containing the data to send
86         """
87         try:
88             if line is None:
89                 self.write("0000")
90                 self.report_activity(4, 'write')
91             else:
92                 self.write("%04x%s" % (len(line)+4, line))
93                 self.report_activity(4+len(line), 'write')
94         except socket.error, e:
95             raise GitProtocolError(e)
96
97     def write_sideband(self, channel, blob):
98         """
99         Write data to the sideband (a git multiplexing method)
100
101         :param channel: int specifying which channel to write to
102         :param blob: a blob of data (as a string) to send on this channel
103         """
104         # a pktline can be a max of 65520. a sideband line can therefore be
105         # 65520-5 = 65515
106         # WTF: Why have the len in ASCII, but the channel in binary.
107         while blob:
108             self.write_pkt_line("%s%s" % (chr(channel), blob[:65515]))
109             blob = blob[65515:]
110
111     def send_cmd(self, cmd, *args):
112         """
113         Send a command and some arguments to a git server
114
115         Only used for git://
116
117         :param cmd: The remote service to access
118         :param args: List of arguments to send to remove service
119         """
120         self.write_pkt_line("%s %s" % (cmd, "".join(["%s\0" % a for a in args])))
121
122     def read_cmd(self):
123         """
124         Read a command and some arguments from the git client
125
126         Only used for git://
127
128         :return: A tuple of (command, [list of arguments])
129         """
130         line = self.read_pkt_line()
131         splice_at = line.find(" ")
132         cmd, args = line[:splice_at], line[splice_at+1:]
133         assert args[-1] == "\x00"
134         return cmd, args[:-1].split(chr(0))
135
136
137 def extract_capabilities(text):
138     """Extract a capabilities list from a string, if present.
139
140     :param text: String to extract from
141     :return: Tuple with text with capabilities removed and list of 
142         capabilities or None (if no capabilities were present.
143     """
144     if not "\0" in text:
145         return text, None
146     capabilities = text.split("\0")
147     return (capabilities[0], capabilities[1:])
148