Patchwork [6,of,7] test-commandserver: add connector for unix domain socket server

login
register
mail settings
Submitter Yuya Nishihara
Date Oct. 4, 2014, 10:52 a.m.
Message ID <0368d1677c97ad896468.1412419949@mimosa>
Download mbox | patch
Permalink /patch/6113/
State Accepted
Headers show

Comments

Yuya Nishihara - Oct. 4, 2014, 10:52 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1411813100 -32400
#      Sat Sep 27 19:18:20 2014 +0900
# Node ID 0368d1677c97ad896468ab123b05760f9e34591a
# Parent  48d895c10d39e370d1029e24119441aaf5ae898e
test-commandserver: add connector for unix domain socket server

The next patch will introduce --cmdserver unix.

Patch

diff --git a/contrib/hgclient.py b/contrib/hgclient.py
--- a/contrib/hgclient.py
+++ b/contrib/hgclient.py
@@ -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 @@  def connectpipe(path=None):
 
     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)