contrib/debugcmdserver.py
changeset 15259 1d1f6dff9364
child 16687 e34106fa0dc3
equal deleted inserted replaced
15258:fe9677449331 15259:1d1f6dff9364
       
     1 #!/usr/bin/env python
       
     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 sys, struct
       
    11 
       
    12 if len(sys.argv) != 2:
       
    13     print 'usage: debugcmdserver.py FILE'
       
    14     sys.exit(1)
       
    15 
       
    16 outputfmt = '>cI'
       
    17 outputfmtsize = struct.calcsize(outputfmt)
       
    18 
       
    19 if sys.argv[1] == '-':
       
    20     log = sys.stderr
       
    21 else:
       
    22     log = open(sys.argv[1], 'a')
       
    23 
       
    24 def read(size):
       
    25     data = sys.stdin.read(size)
       
    26     if not data:
       
    27         raise EOFError()
       
    28     sys.stdout.write(data)
       
    29     sys.stdout.flush()
       
    30     return data
       
    31 
       
    32 try:
       
    33     while True:
       
    34         header = read(outputfmtsize)
       
    35         channel, length = struct.unpack(outputfmt, header)
       
    36         log.write('%s, %-4d' % (channel, length))
       
    37         if channel in 'IL':
       
    38             log.write(' -> waiting for input\n')
       
    39         else:
       
    40             data = read(length)
       
    41             log.write(' -> %r\n' % data)
       
    42         log.flush()
       
    43 except EOFError:
       
    44     pass
       
    45 finally:
       
    46     if log != sys.stderr:
       
    47         log.close()