import socket
+import sys
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()
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
-from mock import Mock
+import sys
+from mock import Mock, patch
from py.test import raises
from remoto import connection
import fake_module
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):