# HG changeset patch # User Yuya Nishihara # Date 1411813100 -32400 # Node ID 24c5fd2894f8c38e5bfa39df90e9d271730c60df # Parent 892b2b8c1b500f454187584a28f42f9918a393e3 test-commandserver: add connector for unix domain socket server The next patch will introduce --cmdserver unix. diff -r 892b2b8c1b50 -r 24c5fd2894f8 contrib/hgclient.py --- a/contrib/hgclient.py Sat Sep 27 23:14:26 2014 +0900 +++ b/contrib/hgclient.py Sat Sep 27 19:18:20 2014 +0900 @@ -1,6 +1,6 @@ # A minimal client for Mercurial's command server -import sys, struct, subprocess, cStringIO +import os, sys, signal, struct, socket, subprocess, time, cStringIO def connectpipe(path=None): cmdline = ['hg', 'serve', '--cmdserver', 'pipe'] @@ -12,6 +12,43 @@ return server +class unixconnection(object): + def __init__(self, sockpath): + self.sock = sock = socket.socket(socket.AF_UNIX) + sock.connect(sockpath) + self.stdin = sock.makefile('wb') + self.stdout = sock.makefile('rb') + + def wait(self): + self.stdin.close() + self.stdout.close() + self.sock.close() + +class unixserver(object): + def __init__(self, sockpath, logpath=None, repopath=None): + self.sockpath = sockpath + cmdline = ['hg', 'serve', '--cmdserver', 'unix', '-a', sockpath] + if repopath: + cmdline += ['-R', repopath] + if logpath: + stdout = open(logpath, 'a') + stderr = subprocess.STDOUT + else: + stdout = stderr = None + self.server = subprocess.Popen(cmdline, stdout=stdout, stderr=stderr) + # wait for listen() + while self.server.poll() is None: + if os.path.exists(sockpath): + break + time.sleep(0.1) + + def connect(self): + return unixconnection(self.sockpath) + + def shutdown(self): + os.kill(self.server.pid, signal.SIGTERM) + self.server.wait() + def writeblock(server, data): server.stdin.write(struct.pack('>I', len(data))) server.stdin.write(data)