Show More
@@ -1,72 +1,109 b'' | |||
|
1 | 1 | # A minimal client for Mercurial's command server |
|
2 | 2 | |
|
3 | import sys, struct, subprocess, cStringIO | |
|
3 | import os, sys, signal, struct, socket, subprocess, time, cStringIO | |
|
4 | 4 | |
|
5 | 5 | def connectpipe(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 | class unixconnection(object): | |
|
16 | def __init__(self, sockpath): | |
|
17 | self.sock = sock = socket.socket(socket.AF_UNIX) | |
|
18 | sock.connect(sockpath) | |
|
19 | self.stdin = sock.makefile('wb') | |
|
20 | self.stdout = sock.makefile('rb') | |
|
21 | ||
|
22 | def wait(self): | |
|
23 | self.stdin.close() | |
|
24 | self.stdout.close() | |
|
25 | self.sock.close() | |
|
26 | ||
|
27 | class unixserver(object): | |
|
28 | def __init__(self, sockpath, logpath=None, repopath=None): | |
|
29 | self.sockpath = sockpath | |
|
30 | cmdline = ['hg', 'serve', '--cmdserver', 'unix', '-a', sockpath] | |
|
31 | if repopath: | |
|
32 | cmdline += ['-R', repopath] | |
|
33 | if logpath: | |
|
34 | stdout = open(logpath, 'a') | |
|
35 | stderr = subprocess.STDOUT | |
|
36 | else: | |
|
37 | stdout = stderr = None | |
|
38 | self.server = subprocess.Popen(cmdline, stdout=stdout, stderr=stderr) | |
|
39 | # wait for listen() | |
|
40 | while self.server.poll() is None: | |
|
41 | if os.path.exists(sockpath): | |
|
42 | break | |
|
43 | time.sleep(0.1) | |
|
44 | ||
|
45 | def connect(self): | |
|
46 | return unixconnection(self.sockpath) | |
|
47 | ||
|
48 | def shutdown(self): | |
|
49 | os.kill(self.server.pid, signal.SIGTERM) | |
|
50 | self.server.wait() | |
|
51 | ||
|
15 | 52 | def writeblock(server, data): |
|
16 | 53 | server.stdin.write(struct.pack('>I', len(data))) |
|
17 | 54 | server.stdin.write(data) |
|
18 | 55 | server.stdin.flush() |
|
19 | 56 | |
|
20 | 57 | def readchannel(server): |
|
21 | 58 | data = server.stdout.read(5) |
|
22 | 59 | if not data: |
|
23 | 60 | raise EOFError |
|
24 | 61 | channel, length = struct.unpack('>cI', data) |
|
25 | 62 | if channel in 'IL': |
|
26 | 63 | return channel, length |
|
27 | 64 | else: |
|
28 | 65 | return channel, server.stdout.read(length) |
|
29 | 66 | |
|
30 | 67 | def sep(text): |
|
31 | 68 | return text.replace('\\', '/') |
|
32 | 69 | |
|
33 | 70 | def runcommand(server, args, output=sys.stdout, error=sys.stderr, input=None, |
|
34 | 71 | outfilter=lambda x: x): |
|
35 | 72 | print '*** runcommand', ' '.join(args) |
|
36 | 73 | sys.stdout.flush() |
|
37 | 74 | server.stdin.write('runcommand\n') |
|
38 | 75 | writeblock(server, '\0'.join(args)) |
|
39 | 76 | |
|
40 | 77 | if not input: |
|
41 | 78 | input = cStringIO.StringIO() |
|
42 | 79 | |
|
43 | 80 | while True: |
|
44 | 81 | ch, data = readchannel(server) |
|
45 | 82 | if ch == 'o': |
|
46 | 83 | output.write(outfilter(data)) |
|
47 | 84 | output.flush() |
|
48 | 85 | elif ch == 'e': |
|
49 | 86 | error.write(data) |
|
50 | 87 | error.flush() |
|
51 | 88 | elif ch == 'I': |
|
52 | 89 | writeblock(server, input.read(data)) |
|
53 | 90 | elif ch == 'L': |
|
54 | 91 | writeblock(server, input.readline(data)) |
|
55 | 92 | elif ch == 'r': |
|
56 | 93 | ret, = struct.unpack('>i', data) |
|
57 | 94 | if ret != 0: |
|
58 | 95 | print ' [%d]' % ret |
|
59 | 96 | return ret |
|
60 | 97 | else: |
|
61 | 98 | print "unexpected channel %c: %r" % (ch, data) |
|
62 | 99 | if ch.isupper(): |
|
63 | 100 | return |
|
64 | 101 | |
|
65 | 102 | def check(func, connect=connectpipe): |
|
66 | 103 | sys.stdout.flush() |
|
67 | 104 | server = connect() |
|
68 | 105 | try: |
|
69 | 106 | return func(server) |
|
70 | 107 | finally: |
|
71 | 108 | server.stdin.close() |
|
72 | 109 | server.wait() |
General Comments 0
You need to be logged in to leave comments.
Login now