Mercurial > hg
comparison contrib/hgclient.py @ 40316:09540a5f0a15
py3: reinvent print() function for contrib/hgclient.py
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Tue, 16 Oct 2018 07:04:07 +0200 |
parents | 431a831342d2 |
children | 6958eb9bdcd6 |
comparison
equal
deleted
inserted
replaced
40315:431a831342d2 | 40316:09540a5f0a15 |
---|---|
1 # A minimal client for Mercurial's command server | 1 # A minimal client for Mercurial's command server |
2 | 2 |
3 from __future__ import absolute_import, print_function | 3 from __future__ import absolute_import, print_function |
4 import os | 4 import os |
5 import re | |
5 import signal | 6 import signal |
6 import socket | 7 import socket |
7 import struct | 8 import struct |
8 import subprocess | 9 import subprocess |
9 import sys | 10 import sys |
17 stringio = io.StringIO | 18 stringio = io.StringIO |
18 | 19 |
19 if sys.version_info[0] >= 3: | 20 if sys.version_info[0] >= 3: |
20 stdout = sys.stdout.buffer | 21 stdout = sys.stdout.buffer |
21 stderr = sys.stderr.buffer | 22 stderr = sys.stderr.buffer |
23 def bprint(*args): | |
24 # remove b'' as well for ease of test migration | |
25 pargs = [re.sub(br'''\bb(['"])''', br'\1', b'%s' % a) for a in args] | |
26 stdout.write(b' '.join(pargs) + b'\n') | |
22 else: | 27 else: |
23 stdout = sys.stdout | 28 stdout = sys.stdout |
24 stderr = sys.stderr | 29 stderr = sys.stderr |
30 bprint = print | |
25 | 31 |
26 def connectpipe(path=None): | 32 def connectpipe(path=None): |
27 cmdline = [b'hg', b'serve', b'--cmdserver', b'pipe'] | 33 cmdline = [b'hg', b'serve', b'--cmdserver', b'pipe'] |
28 if path: | 34 if path: |
29 cmdline += [b'-R', path] | 35 cmdline += [b'-R', path] |
88 def sep(text): | 94 def sep(text): |
89 return text.replace(b'\\', b'/') | 95 return text.replace(b'\\', b'/') |
90 | 96 |
91 def runcommand(server, args, output=stdout, error=stderr, input=None, | 97 def runcommand(server, args, output=stdout, error=stderr, input=None, |
92 outfilter=lambda x: x): | 98 outfilter=lambda x: x): |
93 print(b'*** runcommand', b' '.join(args)) | 99 bprint(b'*** runcommand', b' '.join(args)) |
94 stdout.flush() | 100 stdout.flush() |
95 server.stdin.write(b'runcommand\n') | 101 server.stdin.write(b'runcommand\n') |
96 writeblock(server, b'\0'.join(args)) | 102 writeblock(server, b'\0'.join(args)) |
97 | 103 |
98 if not input: | 104 if not input: |
111 elif ch == b'L': | 117 elif ch == b'L': |
112 writeblock(server, input.readline(data)) | 118 writeblock(server, input.readline(data)) |
113 elif ch == b'r': | 119 elif ch == b'r': |
114 ret, = struct.unpack('>i', data) | 120 ret, = struct.unpack('>i', data) |
115 if ret != 0: | 121 if ret != 0: |
116 print(b' [%d]' % ret) | 122 bprint(b' [%d]' % ret) |
117 return ret | 123 return ret |
118 else: | 124 else: |
119 print(b"unexpected channel %c: %r" % (ch, data)) | 125 bprint(b"unexpected channel %c: %r" % (ch, data)) |
120 if ch.isupper(): | 126 if ch.isupper(): |
121 return | 127 return |
122 | 128 |
123 def check(func, connect=connectpipe): | 129 def check(func, connect=connectpipe): |
124 stdout.flush() | 130 stdout.flush() |