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 |
|
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() |
14 |
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() |