contrib/debugcmdserver.py
author Manuel Jacob <me@manueljacob.de>
Tue, 30 Jun 2020 07:23:29 +0200
branchstable
changeset 45027 0ea9c86fac89
parent 43076 2372284d9457
child 45830 c102b704edb5
permissions -rwxr-xr-x
convert: handle percent-encoded bytes in file URLs like Subversion 75b59d221aa3 added most of the code that gets removed by this patch. It helped making progress on Python 3, but the reasoning was wrong in many ways. I tried to retract it while it was queued, but it was too late. Back then, I was asssuming that what happened on Python 2 (preserving bytes) is correct and my Python 3 change is a hack. However it turned out that Subversion interprets percent-encoded bytes as UTF-8. Accepting the same format as Subversion is a good idea. Consistency with urlreq.pathname2url() (as described in the removed comment) doesn’t matter because that function is only used for passing paths to urllib. This is not a backwards-incompatible change because before 5c0d5b48e58c, non-ASCII filenames didn’t work at all on Python 2. When the locale encoding is ISO-8859-15, `svn` accepts `file:///tmp/a%E2%82%AC` for `/tmp/a€`. Before this patch, this was the case for this extension on Python 3, but not on Python 2. This patch makes it work like with `svn` on both Python 2 and Python 3.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15259
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/env python
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
     2
#
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
     3
# Dumps output generated by Mercurial's command server in a formatted style to a
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
     4
# given file or stderr if '-' is specified. Output is also written in its raw
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
     5
# format to stdout.
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
     6
#
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
     7
# $ ./hg serve --cmds pipe | ./contrib/debugcmdserver.py -
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
     8
# o, 52   -> 'capabilities: getencoding runcommand\nencoding: UTF-8'
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
     9
28353
cd03fbd5ab57 debugcmdserver: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 16687
diff changeset
    10
from __future__ import absolute_import, print_function
cd03fbd5ab57 debugcmdserver: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 16687
diff changeset
    11
import struct
cd03fbd5ab57 debugcmdserver: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 16687
diff changeset
    12
import sys
15259
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    13
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    14
if len(sys.argv) != 2:
28353
cd03fbd5ab57 debugcmdserver: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 16687
diff changeset
    15
    print('usage: debugcmdserver.py FILE')
15259
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    16
    sys.exit(1)
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    17
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    18
outputfmt = '>cI'
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    19
outputfmtsize = struct.calcsize(outputfmt)
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    20
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    21
if sys.argv[1] == '-':
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    22
    log = sys.stderr
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    23
else:
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    24
    log = open(sys.argv[1], 'a')
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    25
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 28353
diff changeset
    26
15259
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    27
def read(size):
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    28
    data = sys.stdin.read(size)
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    29
    if not data:
16687
e34106fa0dc3 cleanup: "raise SomeException()" -> "raise SomeException"
Brodie Rao <brodie@sf.io>
parents: 15259
diff changeset
    30
        raise EOFError
15259
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    31
    sys.stdout.write(data)
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    32
    sys.stdout.flush()
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    33
    return data
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    34
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 28353
diff changeset
    35
15259
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    36
try:
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    37
    while True:
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    38
        header = read(outputfmtsize)
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    39
        channel, length = struct.unpack(outputfmt, header)
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    40
        log.write('%s, %-4d' % (channel, length))
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    41
        if channel in 'IL':
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    42
            log.write(' -> waiting for input\n')
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    43
        else:
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    44
            data = read(length)
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    45
            log.write(' -> %r\n' % data)
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    46
        log.flush()
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    47
except EOFError:
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    48
    pass
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    49
finally:
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    50
    if log != sys.stderr:
1d1f6dff9364 contrib: add a script to help diagnose raw output of the cmdserver
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
    51
        log.close()