changeset 33768:82d564d5ac4f

sshpeer: make instance attributes and methods internal Peer types are supposed to conform to a formal interface defined by peer.peerrepository and wireproto.wirepeer. Every "public" attribute on *peer types makes it harder to understand what attributes are part of the interface and what are instance specific. This commit converts a number of "public" instance attributes and methods on sshpeer to internal so they can't be confused to be part of the peer API. The URL-related instance attributes were introduced in 876333a295ff in 2005. AFAICT most of them aren't used and could potentially be removed. But I kept them around anyway. I also reorded some code to make things slightly easier to read. .. api:: Rename attributes on sshpeer to reflect peer API Differential Revision: https://phab.mercurial-scm.org/D331
author Gregory Szorc <gregory.szorc@gmail.com>
date Thu, 10 Aug 2017 20:55:28 -0700
parents b47fe9733d76
children dd35abc409ee
files mercurial/sshpeer.py
diffstat 1 files changed, 42 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/sshpeer.py	Wed Aug 09 23:35:20 2017 -0700
+++ b/mercurial/sshpeer.py	Thu Aug 10 20:55:28 2017 -0700
@@ -118,7 +118,7 @@
     def __init__(self, ui, path, create=False):
         self._url = path
         self.ui = ui
-        self.pipeo = self.pipei = self.pipee = None
+        self._pipeo = self._pipei = self._pipee = None
 
         u = util.url(path, parsequery=False, parsefragment=False)
         if u.scheme != 'ssh' or not u.host or u.path is None:
@@ -126,22 +126,23 @@
 
         util.checksafessh(path)
 
-        self.user = u.user
         if u.passwd is not None:
             self._abort(error.RepoError(_("password in URL not supported")))
-        self.host = u.host
-        self.port = u.port
-        self.path = u.path or "."
+
+        self._user = u.user
+        self._host = u.host
+        self._port = u.port
+        self._path = u.path or '.'
 
         sshcmd = self.ui.config("ui", "ssh")
         remotecmd = self.ui.config("ui", "remotecmd")
 
-        args = util.sshargs(sshcmd, self.host, self.user, self.port)
+        args = util.sshargs(sshcmd, self._host, self._user, self._port)
 
         if create:
             cmd = '%s %s %s' % (sshcmd, args,
                 util.shellquote("%s init %s" %
-                    (_serverquote(remotecmd), _serverquote(self.path))))
+                    (_serverquote(remotecmd), _serverquote(self._path))))
             ui.debug('running %s\n' % cmd)
             res = ui.system(cmd, blockedtag='sshpeer')
             if res != 0:
@@ -154,26 +155,26 @@
 
     def _validaterepo(self, sshcmd, args, remotecmd):
         # cleanup up previous run
-        self.cleanup()
+        self._cleanup()
 
         cmd = '%s %s %s' % (sshcmd, args,
             util.shellquote("%s -R %s serve --stdio" %
-                (_serverquote(remotecmd), _serverquote(self.path))))
+                (_serverquote(remotecmd), _serverquote(self._path))))
         self.ui.debug('running %s\n' % cmd)
         cmd = util.quotecommand(cmd)
 
-        # while self.subprocess isn't used, having it allows the subprocess to
+        # while self._subprocess isn't used, having it allows the subprocess to
         # to clean up correctly later
         #
         # no buffer allow the use of 'select'
         # feel free to remove buffering and select usage when we ultimately
         # move to threading.
         sub = util.popen4(cmd, bufsize=0)
-        self.pipeo, self.pipei, self.pipee, self.subprocess = sub
+        self._pipeo, self._pipei, self._pipee, self._subprocess = sub
 
-        self.pipei = util.bufferedinputpipe(self.pipei)
-        self.pipei = doublepipe(self.ui, self.pipei, self.pipee)
-        self.pipeo = doublepipe(self.ui, self.pipeo, self.pipee)
+        self._pipei = util.bufferedinputpipe(self._pipei)
+        self._pipei = doublepipe(self.ui, self._pipei, self._pipee)
+        self._pipeo = doublepipe(self.ui, self._pipeo, self._pipee)
 
         # skip any noise generated by remote shell
         self._callstream("hello")
@@ -182,7 +183,7 @@
         max_noise = 500
         while lines[-1] and max_noise:
             l = r.readline()
-            self.readerr()
+            self._readerr()
             if lines[-1] == "1\n" and l == "\n":
                 break
             if l:
@@ -202,27 +203,27 @@
     def _capabilities(self):
         return self._caps
 
-    def readerr(self):
-        _forwardoutput(self.ui, self.pipee)
+    def _readerr(self):
+        _forwardoutput(self.ui, self._pipee)
 
     def _abort(self, exception):
-        self.cleanup()
+        self._cleanup()
         raise exception
 
-    def cleanup(self):
-        if self.pipeo is None:
+    def _cleanup(self):
+        if self._pipeo is None:
             return
-        self.pipeo.close()
-        self.pipei.close()
+        self._pipeo.close()
+        self._pipei.close()
         try:
             # read the error descriptor until EOF
-            for l in self.pipee:
+            for l in self._pipee:
                 self.ui.status(_("remote: "), l)
         except (IOError, ValueError):
             pass
-        self.pipee.close()
+        self._pipee.close()
 
-    __del__ = cleanup
+    __del__ = _cleanup
 
     def _submitbatch(self, req):
         rsp = self._callstream("batch", cmds=wireproto.encodebatchcmds(req))
@@ -246,7 +247,7 @@
     def _callstream(self, cmd, **args):
         args = pycompat.byteskwargs(args)
         self.ui.debug("sending %s command\n" % cmd)
-        self.pipeo.write("%s\n" % cmd)
+        self._pipeo.write("%s\n" % cmd)
         _func, names = wireproto.commands[cmd]
         keys = names.split()
         wireargs = {}
@@ -258,16 +259,16 @@
                 wireargs[k] = args[k]
                 del args[k]
         for k, v in sorted(wireargs.iteritems()):
-            self.pipeo.write("%s %d\n" % (k, len(v)))
+            self._pipeo.write("%s %d\n" % (k, len(v)))
             if isinstance(v, dict):
                 for dk, dv in v.iteritems():
-                    self.pipeo.write("%s %d\n" % (dk, len(dv)))
-                    self.pipeo.write(dv)
+                    self._pipeo.write("%s %d\n" % (dk, len(dv)))
+                    self._pipeo.write(dv)
             else:
-                self.pipeo.write(v)
-        self.pipeo.flush()
+                self._pipeo.write(v)
+        self._pipeo.flush()
 
-        return self.pipei
+        return self._pipei
 
     def _callcompressable(self, cmd, **args):
         return self._callstream(cmd, **args)
@@ -296,29 +297,29 @@
         for d in iter(lambda: fp.read(4096), ''):
             self._send(d)
         self._send("", flush=True)
-        return self.pipei
+        return self._pipei
 
     def _getamount(self):
-        l = self.pipei.readline()
+        l = self._pipei.readline()
         if l == '\n':
-            self.readerr()
+            self._readerr()
             msg = _('check previous remote output')
             self._abort(error.OutOfBandError(hint=msg))
-        self.readerr()
+        self._readerr()
         try:
             return int(l)
         except ValueError:
             self._abort(error.ResponseError(_("unexpected response:"), l))
 
     def _recv(self):
-        return self.pipei.read(self._getamount())
+        return self._pipei.read(self._getamount())
 
     def _send(self, data, flush=False):
-        self.pipeo.write("%d\n" % len(data))
+        self._pipeo.write("%d\n" % len(data))
         if data:
-            self.pipeo.write(data)
+            self._pipeo.write(data)
         if flush:
-            self.pipeo.flush()
-        self.readerr()
+            self._pipeo.flush()
+        self._readerr()
 
 instance = sshpeer