[PATCH 2 of 7] cmdserver: switch service objects by mode

Yuya Nishihara yuya at tcha.org
Sat Oct 4 05:52:25 CDT 2014


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1411797886 -32400
#      Sat Sep 27 15:04:46 2014 +0900
# Node ID 4545d11a3950be83e452bafda4ba22ce847efecf
# Parent  ad2f42cf0437da2a20df23c200d6c1f05b69db8d
cmdserver: switch service objects by mode

server class will be changed to accept fin/fout pair instead of mode string
so that it can interact with socket files.

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -5544,7 +5544,7 @@ def serve(ui, repo, **opts):
         s.serve_forever()
 
     if opts["cmdserver"]:
-        service = commandserver.pipeservice(ui, repo, opts)
+        service = commandserver.createservice(ui, repo, opts)
         return cmdutil.service(opts, initfn=service.init, runfn=service.run)
 
     # this way we can check if something was given in the command-line
diff --git a/mercurial/commandserver.py b/mercurial/commandserver.py
--- a/mercurial/commandserver.py
+++ b/mercurial/commandserver.py
@@ -129,7 +129,7 @@ class server(object):
     Listens for commands on stdin, runs them and writes the output on a channel
     based stream to stdout.
     """
-    def __init__(self, ui, repo, mode):
+    def __init__(self, ui, repo):
         self.cwd = os.getcwd()
 
         logpath = ui.config("cmdserver", "log", None)
@@ -151,15 +151,12 @@ class server(object):
             self.ui = ui
             self.repo = self.repoui = None
 
-        if mode == 'pipe':
-            self.cerr = channeledoutput(sys.stdout, 'e')
-            self.cout = channeledoutput(sys.stdout, 'o')
-            self.cin = channeledinput(sys.stdin, sys.stdout, 'I')
-            self.cresult = channeledoutput(sys.stdout, 'r')
+        self.cerr = channeledoutput(sys.stdout, 'e')
+        self.cout = channeledoutput(sys.stdout, 'o')
+        self.cin = channeledinput(sys.stdin, sys.stdout, 'I')
+        self.cresult = channeledoutput(sys.stdout, 'r')
 
-            self.client = sys.stdin
-        else:
-            raise util.Abort(_('unknown mode %s') % mode)
+        self.client = sys.stdin
 
     def _read(self, size):
         if not size:
@@ -251,10 +248,21 @@ class server(object):
 
 class pipeservice(object):
     def __init__(self, ui, repo, opts):
-        self.server = server(ui, repo, opts['cmdserver'])
+        self.server = server(ui, repo)
 
     def init(self):
         pass
 
     def run(self):
         return self.server.serve()
+
+_servicemap = {
+    'pipe': pipeservice,
+    }
+
+def createservice(ui, repo, opts):
+    mode = opts['cmdserver']
+    try:
+        return _servicemap[mode](ui, repo, opts)
+    except KeyError:
+        raise util.Abort(_('unknown mode %s') % mode)


More information about the Mercurial-devel mailing list