Show More
@@ -1,72 +1,72 | |||
|
1 | 1 | # A minimal client for Mercurial's command server |
|
2 | 2 | |
|
3 | 3 | import sys, struct, subprocess, cStringIO |
|
4 | 4 | |
|
5 | 5 | def connect(path=None): |
|
6 | 6 | cmdline = ['hg', 'serve', '--cmdserver', 'pipe'] |
|
7 | 7 | if path: |
|
8 | 8 | cmdline += ['-R', path] |
|
9 | 9 | |
|
10 | 10 | server = subprocess.Popen(cmdline, stdin=subprocess.PIPE, |
|
11 | 11 | stdout=subprocess.PIPE) |
|
12 | 12 | |
|
13 | 13 | return server |
|
14 | 14 | |
|
15 | 15 | def writeblock(server, data): |
|
16 | 16 | server.stdin.write(struct.pack('>I', len(data))) |
|
17 | 17 | server.stdin.write(data) |
|
18 | 18 | server.stdin.flush() |
|
19 | 19 | |
|
20 | 20 | def readchannel(server): |
|
21 | 21 | data = server.stdout.read(5) |
|
22 | 22 | if not data: |
|
23 | 23 | raise EOFError |
|
24 | 24 | channel, length = struct.unpack('>cI', data) |
|
25 | 25 | if channel in 'IL': |
|
26 | 26 | return channel, length |
|
27 | 27 | else: |
|
28 | 28 | return channel, server.stdout.read(length) |
|
29 | 29 | |
|
30 | 30 | def sep(text): |
|
31 | 31 | return text.replace('\\', '/') |
|
32 | 32 | |
|
33 | 33 | def runcommand(server, args, output=sys.stdout, error=sys.stderr, input=None, |
|
34 | 34 | outfilter=lambda x: x): |
|
35 | 35 | print '*** runcommand', ' '.join(args) |
|
36 | 36 | sys.stdout.flush() |
|
37 | 37 | server.stdin.write('runcommand\n') |
|
38 | 38 | writeblock(server, '\0'.join(args)) |
|
39 | 39 | |
|
40 | 40 | if not input: |
|
41 | 41 | input = cStringIO.StringIO() |
|
42 | 42 | |
|
43 | 43 | while True: |
|
44 | 44 | ch, data = readchannel(server) |
|
45 | 45 | if ch == 'o': |
|
46 | 46 | output.write(outfilter(data)) |
|
47 | 47 | output.flush() |
|
48 | 48 | elif ch == 'e': |
|
49 | 49 | error.write(data) |
|
50 | 50 | error.flush() |
|
51 | 51 | elif ch == 'I': |
|
52 | 52 | writeblock(server, input.read(data)) |
|
53 | 53 | elif ch == 'L': |
|
54 | 54 | writeblock(server, input.readline(data)) |
|
55 | 55 | elif ch == 'r': |
|
56 | 56 | ret, = struct.unpack('>i', data) |
|
57 | 57 | if ret != 0: |
|
58 | 58 | print ' [%d]' % ret |
|
59 | 59 | return ret |
|
60 | 60 | else: |
|
61 | 61 | print "unexpected channel %c: %r" % (ch, data) |
|
62 | 62 | if ch.isupper(): |
|
63 | 63 | return |
|
64 | 64 | |
|
65 |
def check(func |
|
|
65 | def check(func): | |
|
66 | 66 | sys.stdout.flush() |
|
67 |
server = connect( |
|
|
67 | server = connect() | |
|
68 | 68 | try: |
|
69 | 69 | return func(server) |
|
70 | 70 | finally: |
|
71 | 71 | server.stdin.close() |
|
72 | 72 | server.wait() |
General Comments 0
You need to be logged in to leave comments.
Login now