author | Pierre-Yves David <pierre-yves.david@fb.com> |
Thu, 18 Sep 2014 15:52:45 -0700 | |
changeset 22669 | 00c8abe64cf3 |
parent 22572 | cc3d9f776632 |
child 22991 | a94594f5d52f |
permissions | -rw-r--r-- |
22566
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
1 |
# A minimal client for Mercurial's command server |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
2 |
|
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
3 |
import sys, struct, subprocess, cStringIO |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
4 |
|
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
5 |
def connect(path=None): |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
6 |
cmdline = ['hg', 'serve', '--cmdserver', 'pipe'] |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
7 |
if path: |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
8 |
cmdline += ['-R', path] |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
9 |
|
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
10 |
server = subprocess.Popen(cmdline, stdin=subprocess.PIPE, |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
11 |
stdout=subprocess.PIPE) |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
12 |
|
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
13 |
return server |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
14 |
|
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
15 |
def writeblock(server, data): |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
16 |
server.stdin.write(struct.pack('>I', len(data))) |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
17 |
server.stdin.write(data) |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
18 |
server.stdin.flush() |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
19 |
|
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
20 |
def readchannel(server): |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
21 |
data = server.stdout.read(5) |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
22 |
if not data: |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
23 |
raise EOFError |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
24 |
channel, length = struct.unpack('>cI', data) |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
25 |
if channel in 'IL': |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
26 |
return channel, length |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
27 |
else: |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
28 |
return channel, server.stdout.read(length) |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
29 |
|
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
30 |
def sep(text): |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
31 |
return text.replace('\\', '/') |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
32 |
|
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
33 |
def runcommand(server, args, output=sys.stdout, error=sys.stderr, input=None, |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
34 |
outfilter=lambda x: x): |
22572
cc3d9f776632
test-commandserver: make runcommand message bolder
Yuya Nishihara <yuya@tcha.org>
parents:
22570
diff
changeset
|
35 |
print '*** runcommand', ' '.join(args) |
22566
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
36 |
sys.stdout.flush() |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
37 |
server.stdin.write('runcommand\n') |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
38 |
writeblock(server, '\0'.join(args)) |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
39 |
|
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
40 |
if not input: |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
41 |
input = cStringIO.StringIO() |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
42 |
|
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
43 |
while True: |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
44 |
ch, data = readchannel(server) |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
45 |
if ch == 'o': |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
46 |
output.write(outfilter(data)) |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
47 |
output.flush() |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
48 |
elif ch == 'e': |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
49 |
error.write(data) |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
50 |
error.flush() |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
51 |
elif ch == 'I': |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
52 |
writeblock(server, input.read(data)) |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
53 |
elif ch == 'L': |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
54 |
writeblock(server, input.readline(data)) |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
55 |
elif ch == 'r': |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
56 |
ret, = struct.unpack('>i', data) |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
57 |
if ret != 0: |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
58 |
print ' [%d]' % ret |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
59 |
return ret |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
60 |
else: |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
61 |
print "unexpected channel %c: %r" % (ch, data) |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
62 |
if ch.isupper(): |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
63 |
return |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
64 |
|
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
65 |
def check(func, repopath=None): |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
66 |
sys.stdout.flush() |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
67 |
server = connect(repopath) |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
68 |
try: |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
69 |
return func(server) |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
70 |
finally: |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
71 |
server.stdin.close() |
480b7fefbb08
test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
72 |
server.wait() |