Mercurial > python-hglib
comparison hglib/client.py @ 179:c4c0efb37187
protocol: add the abilty to trace the protocol between the client and server
This is useful when debugging issues with driving hg via hglib
where output and error messages can be lost.
Call setprotocoltrace with the name of a trace function or None.
If the trace function is None no tracing is done.
The trace function is called with the direction, the channel-identified
and its data.
author | Barry A. Scott <barry@barrys-emacs.org> |
---|---|
date | Tue, 18 Oct 2016 17:45:17 +0100 |
parents | 77a5fded9f45 |
children | ff6efc1ab9e4 |
comparison
equal
deleted
inserted
replaced
178:77a5fded9f45 | 179:c4c0efb37187 |
---|---|
59 # include the hidden changesets if True | 59 # include the hidden changesets if True |
60 self.hidden = None | 60 self.hidden = None |
61 | 61 |
62 if connect: | 62 if connect: |
63 self.open() | 63 self.open() |
64 | |
65 self._protocoltracefn = None | |
66 | |
67 def setprotocoltrace(self, tracefn=None): | |
68 """ | |
69 if tracefn is None no trace calls will be made. | |
70 Otherwise tracefn is call as tracefn( direction, channel, data ) | |
71 direction is 'r' for read from server and 'w' for write to server | |
72 channel is always None when direction is 'w' | |
73 and the channel-identified when the direction is 'r' | |
74 """ | |
75 self._protocoltracefn = tracefn | |
64 | 76 |
65 def __enter__(self): | 77 def __enter__(self): |
66 if self.server is None: | 78 if self.server is None: |
67 self.open() | 79 self.open() |
68 return self | 80 return self |
117 rev[4], rev[5], dt)) | 129 rev[4], rev[5], dt)) |
118 return revs | 130 return revs |
119 | 131 |
120 def runcommand(self, args, inchannels, outchannels): | 132 def runcommand(self, args, inchannels, outchannels): |
121 def writeblock(data): | 133 def writeblock(data): |
134 if self._protocoltracefn is not None: | |
135 self._protocoltracefn('w', None, data) | |
122 self.server.stdin.write(struct.pack(self.inputfmt, len(data))) | 136 self.server.stdin.write(struct.pack(self.inputfmt, len(data))) |
123 self.server.stdin.write(data) | 137 self.server.stdin.write(data) |
124 self.server.stdin.flush() | 138 self.server.stdin.flush() |
125 | 139 |
126 if not self.server: | 140 if not self.server: |
129 self.server.stdin.write(b('runcommand\n')) | 143 self.server.stdin.write(b('runcommand\n')) |
130 writeblock(b('\0').join(args)) | 144 writeblock(b('\0').join(args)) |
131 | 145 |
132 while True: | 146 while True: |
133 channel, data = self._readchannel() | 147 channel, data = self._readchannel() |
148 if self._protocoltracefn is not None: | |
149 self._protocoltracefn('r', channel, data) | |
134 | 150 |
135 # input channels | 151 # input channels |
136 if channel in inchannels: | 152 if channel in inchannels: |
137 writeblock(inchannels[channel](data)) | 153 writeblock(inchannels[channel](data)) |
138 # output channels | 154 # output channels |