From: Oleh Prypin Date: Mon, 6 Jun 2016 20:32:06 +0000 (+0300) Subject: Allow selecting remote Python interpreter X-Git-Tag: 0.0.29~3^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=8a18b769c8b344e821e59fccf2490c394780f14f;p=remoto.git Allow selecting remote Python interpreter 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 --- diff --git a/remoto/connection.py b/remoto/connection.py index 13550c5..98cbe48 100644 --- a/remoto/connection.py +++ b/remoto/connection.py @@ -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 diff --git a/remoto/tests/test_connection.py b/remoto/tests/test_connection.py index 6996f00..ccd78ed 100644 --- a/remoto/tests/test_connection.py +++ b/remoto/tests/test_connection.py @@ -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):