[PATCH python-hglib v2] Add feature to allow hglib user to get call backs for prompts, output

Barry A. Scott barry at barrys-emacs.org
Sat Oct 22 17:37:07 UTC 2016


# HG changeset patch
# User Barry A. Scott <barry at barrys-emacs.org>
# Date 1477157573 -3600
#      Sat Oct 22 18:32:53 2016 +0100
# Node ID 12c96b1d8dfff261f91ed010cc59ce57bfa63b4f
# Parent  6f15cb7cc9cb4427f35c60080f85dbf4ca5abd10
Add feature to allow hglib user to get call backs for prompts, output
and errors.

setcbout(cbout), setcberr(cberr) and setcbprompt(cbprompt) are used to
set the call back function used by the hgclient class. cb stands for
call back.

cbout is a function that will be called with the stdout data of the
command as it runs. cbout is called with output as it is made available,
which can be as partial lines or multiple lines.

cberr is a function that will be called with the stderr data of the
command as it runs. cberr is called with output as it is made available,
which can be as partial lines or multiple lines.

Command that make remote connects can prompt for username and password
for HTTP/HTTPS connections.

cbprompt is called when hgclient need a response to a prompt from the
server. It receives the max number of bytes to return and the contents
of stdout received so far. The last text sent to either cbout or cberr
will contain the prompt text itself.

init() has been added to hgclient to allow use of setcbXXX functions
with init(). The init() code is based on the version in __init__.py.

Example use:
    c = hglib.open( None )
    c.setcbout(cbout)
    c.setcberr(cbout)
    c.init(b'path/for/new/repo')
    c.status() # works on new repo

clone() has been extended with all the parameters that are present in
__init__.py clone() to allow use of the setcbXXX functions.

Example use:
    c = hglib.open( None )
    c.setcbout(cbout)
    c.setcberr(cbout)
    c.setcbprompt(cbprompt)
    c.clone(b'http://example.com/hgrepo', b'path/for/new/repo')
    c.status() # works on new repo

diff -r 6f15cb7cc9cb -r 12c96b1d8dff hglib/client.py
--- a/hglib/client.py	Mon Jul 18 23:40:45 2016 -0500
+++ b/hglib/client.py	Sat Oct 22 18:32:53 2016 +0100
@@ -45,6 +45,7 @@
     def __init__(self, path, encoding, configs, connect=True):
         self._args = [hglib.HGPATH, 'serve', '--cmdserver', 'pipe',
                 '--config', 'ui.interactive=True']
+        self._path = path
         if path:
             self._args += ['-R', path]
         if configs:
@@ -59,9 +60,40 @@
         # include the hidden changesets if True
         self.hidden = None
 
+        self._cbout = None
+        self._cberr = None
+        self._cbprompt = None
+
         if connect:
             self.open()
 
+    def setcbout(self, cbout):
+        """
+        cbout is a function that will be called with the stdout data of
+         the command as it runs. Call with None to stop getting call backs.
+        """
+        self._cbout = cbout
+
+    def setcberr(self, cberr):
+        """
+        cberr is a function that will be called with the stderr data of
+         the command as it runs.Call with None to stop getting call backs.
+        """
+        self._cberr = cberr
+
+    def setcbprompt(self, cbprompt):
+        """
+        cbprompt is used to reply to prompts by the server
+         It receives the max number of bytes to return and the
+         contents of stdout received so far.
+
+        Call with None to stop getting call backs.
+
+        cbprompt is never called from merge() or import_()
+        which already handle the prompt.
+        """
+        self._cbprompt = cbprompt
+
     def __enter__(self):
         if self.server is None:
             self.open()
@@ -131,7 +163,6 @@
 
         while True:
             channel, data = self._readchannel()
-
             # input channels
             if channel in inchannels:
                 writeblock(inchannels[channel](data))
@@ -164,9 +195,25 @@
         It receives the max number of bytes to return
         """
         out, err = BytesIO(), BytesIO()
-        outchannels = {b('o') : out.write, b('e') : err.write}
+        outchannels = {}
+        if self._cbout is None:
+            outchannels[b('o')] = out.write
+        else:
+            def out_handler(data):
+                out.write(data)
+                self._cbout(data)
+            outchannels[b('o')] = out_handler
+        if self._cberr is None:
+            outchannels[b('e')] = err.write
+        else:
+            def err_handler(data):
+                err.write(data)
+                self._cberr(data)
+            outchannels[b('e')] = err_handler
 
         inchannels = {}
+        if prompt is None:
+            prompt = self._cbprompt
         if prompt is not None:
             def func(size):
                 reply = prompt(size, out.getvalue())
@@ -524,7 +571,8 @@
             return out
 
     def clone(self, source=b('.'), dest=None, branch=None, updaterev=None,
-              revrange=None):
+              revrange=None, pull=False, uncompressed=False, ssh=None,
+              remotecmd=None, insecure=False, encoding=None, configs=None):
         """
         Create a copy of an existing repository specified by source in a new
         directory dest.
@@ -536,9 +584,30 @@
         revrange - include the specified changeset
         """
         args = cmdbuilder(b('clone'), source, dest, b=branch,
-                          u=updaterev, r=revrange)
+                          u=updaterev, r=revrange, pull=pull,
+                          uncompresses=uncompressed, e=ssh,
+                          remotecmd=remotecmd, insecure=insecure)
         self.rawcommand(args)
 
+        if self._path is None:
+            self._path = dest
+            # become the client for the cloned hg repo
+            self.close()
+            self._args += ['-R', dest]
+            self.open()
+
+    def init(self, dest, ssh=None, remotecmd=None, insecure=False,
+             encoding=None, configs=None):
+        args = util.cmdbuilder(b'init', dest, e=ssh, remotecmd=remotecmd,
+                               insecure=insecure)
+        self.rawcommand(args)
+
+        # become the client for this new hg repo
+        self._path = dest
+        self.close()
+        self._args += ['-R', dest]
+        self.open()
+
     def commit(self, message=None, logfile=None, addremove=False,
                closebranch=False, date=None, user=None, include=None,
                exclude=None, amend=False):


More information about the Mercurial-devel mailing list