Debian Codegraph @

contrib/debugcmdserver.py

raw
1 #!/usr/bin/env python3
2 #
3 # Dumps output generated by Mercurial's command server in a formatted style to a
4 # given file or stderr if '-' is specified. Output is also written in its raw
5 # format to stdout.
6 #
7 # $ ./hg serve --cmds pipe | ./contrib/debugcmdserver.py -
8 # o, 52 -> 'capabilities: getencoding runcommand\nencoding: UTF-8'
9
10 import struct
11 import sys
12
13 if len(sys.argv) != 2:
14 print('usage: debugcmdserver.py FILE')
15 sys.exit(1)
16
17 outputfmt = '>cI'
18 outputfmtsize = struct.calcsize(outputfmt)
19
20 if sys.argv[1] == '-':
21 log = sys.stderr
22 else:
23 log = open(sys.argv[1], 'a')
24
25
26 def read(size):
27 data = sys.stdin.read(size)
28 if not data:
29 raise EOFError
30 sys.stdout.write(data)
31 sys.stdout.flush()
32 return data
33
34
35 try:
36 while True:
37 header = read(outputfmtsize)
38 channel, length = struct.unpack(outputfmt, header)
39 log.write('%s, %-4d' % (channel, length))
40 if channel in 'IL':
41 log.write(' -> waiting for input\n')
42 else:
43 data = read(length)
44 log.write(' -> %r\n' % data)
45 log.flush()
46 except EOFError:
47 pass
48 finally:
49 if log != sys.stderr:
50 log.close()