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