]> git-server-git.apps.pok.os.sepia.ceph.com Git - remoto.git/commitdiff
Allow selecting remote Python interpreter
authorOleh Prypin <oleh@pryp.in>
Mon, 6 Jun 2016 20:32:06 +0000 (23:32 +0300)
committerOleh Prypin <oleh@pryp.in>
Mon, 6 Jun 2016 20:57:43 +0000 (23:57 +0300)
Automatically selects e. g. 'python2' or 'python3' based on the current version
but also allows overriding to a different name

Signed-off-by: Oleh Prypin <oleh@pryp.in>
remoto/connection.py
remoto/tests/test_connection.py

index 13550c5f7cccb75ea4f0822941e6fd2e0cb4fa8a..98cbe480b0c07cc319cfcd932ec56c018c3661e6 100644 (file)
@@ -1,4 +1,5 @@
 import socket
+import sys
 from .lib import execnet
 
 
@@ -8,13 +9,17 @@ from .lib import execnet
 
 class Connection(object):
 
-    def __init__(self, hostname, logger=None, sudo=False, threads=1, eager=True, detect_sudo=False):
+    def __init__(self, hostname, logger=None, sudo=False, threads=1, eager=True,
+                 detect_sudo=False, interpreter=None):
         self.sudo = sudo
         self.hostname = hostname
         self.logger = logger or FakeRemoteLogger()
         self.remote_module = None
         self.channel = None
         self.global_timeout = None  # wait for ever
+
+        self.interpreter = interpreter or 'python%s' % sys.version_info[0]
+
         if eager:
             if detect_sudo:
                 self.sudo = self._detect_sudo()
@@ -53,10 +58,12 @@ class Connection(object):
 
     def _make_connection_string(self, hostname, _needs_ssh=None, use_sudo=None):
         _needs_ssh = _needs_ssh or needs_ssh
+        interpreter = self.interpreter
         if use_sudo is not None:
-            interpreter = 'sudo python' if use_sudo else 'python'
-        else:
-            interpreter = 'sudo python' if self.sudo else 'python'
+            if use_sudo:
+                interpreter = 'sudo ' + interpreter
+        elif self.sudo:
+            interpreter = 'sudo ' + interpreter
         if _needs_ssh(hostname):
             return 'ssh=%s//python=%s' % (hostname, interpreter)
         return 'popen//python=%s' % interpreter
index 6996f00c82110d5b496454259ffbdfa3141c7edb..ccd78ed927fb597c41c5d1414dc72167f091d092 100644 (file)
@@ -1,4 +1,5 @@
-from mock import Mock
+import sys
+from mock import Mock, patch
 from py.test import raises
 from remoto import connection
 import fake_module
@@ -82,35 +83,46 @@ class TestModuleExecuteGetAttr(object):
 class TestMakeConnectionString(object):
 
     def test_makes_sudo_python_no_ssh(self):
-        conn = connection.Connection('localhost', sudo=True, eager=False)
+        conn = connection.Connection('localhost', sudo=True, eager=False, interpreter='python')
         conn_string = conn._make_connection_string('localhost', _needs_ssh=lambda x: False)
         assert conn_string == 'popen//python=sudo python'
 
     def test_makes_sudo_python_with_ssh(self):
-        conn = connection.Connection('localhost', sudo=True, eager=False)
+        conn = connection.Connection('localhost', sudo=True, eager=False, interpreter='python')
         conn_string = conn._make_connection_string('localhost', _needs_ssh=lambda x: True)
         assert conn_string == 'ssh=localhost//python=sudo python'
 
     def test_makes_python_no_ssh(self):
-        conn = connection.Connection('localhost', sudo=False, eager=False)
+        conn = connection.Connection('localhost', sudo=False, eager=False, interpreter='python')
         conn_string = conn._make_connection_string('localhost', _needs_ssh=lambda x: False)
         assert conn_string == 'popen//python=python'
 
     def test_makes_python_with_ssh(self):
-        conn = connection.Connection('localhost', sudo=False, eager=False)
+        conn = connection.Connection('localhost', sudo=False, eager=False, interpreter='python')
         conn_string = conn._make_connection_string('localhost', _needs_ssh=lambda x: True)
         assert conn_string == 'ssh=localhost//python=python'
 
     def test_makes_sudo_python_with_forced_sudo(self):
-        conn = connection.Connection('localhost', sudo=True, eager=False)
+        conn = connection.Connection('localhost', sudo=True, eager=False, interpreter='python')
         conn_string = conn._make_connection_string('localhost', _needs_ssh=lambda x: False, use_sudo=True)
         assert conn_string == 'popen//python=sudo python'
 
     def test_does_not_make_sudo_python_with_forced_sudo(self):
-        conn = connection.Connection('localhost', sudo=True, eager=False)
+        conn = connection.Connection('localhost', sudo=True, eager=False, interpreter='python')
         conn_string = conn._make_connection_string('localhost', _needs_ssh=lambda x: False, use_sudo=False)
         assert conn_string == 'popen//python=python'
 
+    def test_detects_python3(self):
+        with patch.object(sys, 'version_info', (3, 5, 1)):
+            conn = connection.Connection('localhost', sudo=True, eager=False)
+            conn_string = conn._make_connection_string('localhost', _needs_ssh=lambda x: False)
+            assert conn_string == 'popen//python=sudo python3'
+
+    def test_detects_python2(self):
+        with patch.object(sys, 'version_info', (2, 7, 11)):
+            conn = connection.Connection('localhost', sudo=False, eager=False)
+            conn_string = conn._make_connection_string('localhost', _needs_ssh=lambda x: True)
+            assert conn_string == 'ssh=localhost//python=python2'
 
 class TestDetectSudo(object):