##// END OF EJS Templates
test-commandserver: add connector for unix domain socket server...
Yuya Nishihara -
r22993:24c5fd28 default
parent child Browse files
Show More
@@ -1,72 +1,109 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 os, sys, signal, struct, socket, subprocess, time, cStringIO
4
4
5 def connectpipe(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 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 def writeblock(server, data):
52 def writeblock(server, data):
16 server.stdin.write(struct.pack('>I', len(data)))
53 server.stdin.write(struct.pack('>I', len(data)))
17 server.stdin.write(data)
54 server.stdin.write(data)
18 server.stdin.flush()
55 server.stdin.flush()
19
56
20 def readchannel(server):
57 def readchannel(server):
21 data = server.stdout.read(5)
58 data = server.stdout.read(5)
22 if not data:
59 if not data:
23 raise EOFError
60 raise EOFError
24 channel, length = struct.unpack('>cI', data)
61 channel, length = struct.unpack('>cI', data)
25 if channel in 'IL':
62 if channel in 'IL':
26 return channel, length
63 return channel, length
27 else:
64 else:
28 return channel, server.stdout.read(length)
65 return channel, server.stdout.read(length)
29
66
30 def sep(text):
67 def sep(text):
31 return text.replace('\\', '/')
68 return text.replace('\\', '/')
32
69
33 def runcommand(server, args, output=sys.stdout, error=sys.stderr, input=None,
70 def runcommand(server, args, output=sys.stdout, error=sys.stderr, input=None,
34 outfilter=lambda x: x):
71 outfilter=lambda x: x):
35 print '*** runcommand', ' '.join(args)
72 print '*** runcommand', ' '.join(args)
36 sys.stdout.flush()
73 sys.stdout.flush()
37 server.stdin.write('runcommand\n')
74 server.stdin.write('runcommand\n')
38 writeblock(server, '\0'.join(args))
75 writeblock(server, '\0'.join(args))
39
76
40 if not input:
77 if not input:
41 input = cStringIO.StringIO()
78 input = cStringIO.StringIO()
42
79
43 while True:
80 while True:
44 ch, data = readchannel(server)
81 ch, data = readchannel(server)
45 if ch == 'o':
82 if ch == 'o':
46 output.write(outfilter(data))
83 output.write(outfilter(data))
47 output.flush()
84 output.flush()
48 elif ch == 'e':
85 elif ch == 'e':
49 error.write(data)
86 error.write(data)
50 error.flush()
87 error.flush()
51 elif ch == 'I':
88 elif ch == 'I':
52 writeblock(server, input.read(data))
89 writeblock(server, input.read(data))
53 elif ch == 'L':
90 elif ch == 'L':
54 writeblock(server, input.readline(data))
91 writeblock(server, input.readline(data))
55 elif ch == 'r':
92 elif ch == 'r':
56 ret, = struct.unpack('>i', data)
93 ret, = struct.unpack('>i', data)
57 if ret != 0:
94 if ret != 0:
58 print ' [%d]' % ret
95 print ' [%d]' % ret
59 return ret
96 return ret
60 else:
97 else:
61 print "unexpected channel %c: %r" % (ch, data)
98 print "unexpected channel %c: %r" % (ch, data)
62 if ch.isupper():
99 if ch.isupper():
63 return
100 return
64
101
65 def check(func, connect=connectpipe):
102 def check(func, connect=connectpipe):
66 sys.stdout.flush()
103 sys.stdout.flush()
67 server = connect()
104 server = connect()
68 try:
105 try:
69 return func(server)
106 return func(server)
70 finally:
107 finally:
71 server.stdin.close()
108 server.stdin.close()
72 server.wait()
109 server.wait()
General Comments 0
You need to be logged in to leave comments. Login now