comparison contrib/hgclient.py @ 40315:431a831342d2

py3: work around unicode stdio streams in contrib/hgclient.py
author Yuya Nishihara <yuya@tcha.org>
date Tue, 16 Oct 2018 07:08:12 +0200
parents 73c2b9c9cd3c
children 09540a5f0a15
comparison
equal deleted inserted replaced
40314:73c2b9c9cd3c 40315:431a831342d2
13 import cStringIO as io 13 import cStringIO as io
14 stringio = io.StringIO 14 stringio = io.StringIO
15 except ImportError: 15 except ImportError:
16 import io 16 import io
17 stringio = io.StringIO 17 stringio = io.StringIO
18
19 if sys.version_info[0] >= 3:
20 stdout = sys.stdout.buffer
21 stderr = sys.stderr.buffer
22 else:
23 stdout = sys.stdout
24 stderr = sys.stderr
18 25
19 def connectpipe(path=None): 26 def connectpipe(path=None):
20 cmdline = [b'hg', b'serve', b'--cmdserver', b'pipe'] 27 cmdline = [b'hg', b'serve', b'--cmdserver', b'pipe']
21 if path: 28 if path:
22 cmdline += [b'-R', path] 29 cmdline += [b'-R', path]
79 return channel, server.stdout.read(length) 86 return channel, server.stdout.read(length)
80 87
81 def sep(text): 88 def sep(text):
82 return text.replace(b'\\', b'/') 89 return text.replace(b'\\', b'/')
83 90
84 def runcommand(server, args, output=sys.stdout, error=sys.stderr, input=None, 91 def runcommand(server, args, output=stdout, error=stderr, input=None,
85 outfilter=lambda x: x): 92 outfilter=lambda x: x):
86 print(b'*** runcommand', b' '.join(args)) 93 print(b'*** runcommand', b' '.join(args))
87 sys.stdout.flush() 94 stdout.flush()
88 server.stdin.write(b'runcommand\n') 95 server.stdin.write(b'runcommand\n')
89 writeblock(server, b'\0'.join(args)) 96 writeblock(server, b'\0'.join(args))
90 97
91 if not input: 98 if not input:
92 input = stringio() 99 input = stringio()
112 print(b"unexpected channel %c: %r" % (ch, data)) 119 print(b"unexpected channel %c: %r" % (ch, data))
113 if ch.isupper(): 120 if ch.isupper():
114 return 121 return
115 122
116 def check(func, connect=connectpipe): 123 def check(func, connect=connectpipe):
117 sys.stdout.flush() 124 stdout.flush()
118 server = connect() 125 server = connect()
119 try: 126 try:
120 return func(server) 127 return func(server)
121 finally: 128 finally:
122 server.stdin.close() 129 server.stdin.close()