Commit 0d691d7f authored by Xavier Thompson's avatar Xavier Thompson

grid/utils: Improve parsing of instance's python

See merge request nexedi/slapos.core!520
parents f9f4510e 06e9266d
...@@ -37,6 +37,7 @@ import stat ...@@ -37,6 +37,7 @@ import stat
import sys import sys
import logging import logging
import psutil import psutil
import shlex
import time import time
if sys.version_info >= (3,): if sys.version_info >= (3,):
...@@ -221,10 +222,15 @@ def getPythonExecutableFromSoftwarePath(software_path): ...@@ -221,10 +222,15 @@ def getPythonExecutableFromSoftwarePath(software_path):
try: try:
with open(os.path.join(software_path, 'bin', 'buildout')) as f: with open(os.path.join(software_path, 'bin', 'buildout')) as f:
shebang = f.readline() shebang = f.readline()
if shebang.startswith('#!'):
executable = shebang[2:].split(None, 1)[0]
if executable == '/bin/sh':
exec_wrapper = shlex.split(f.readline())
if len(exec_wrapper) >= 2 and exec_wrapper[0] == 'exec':
return exec_wrapper[1]
return executable
except (IOError, OSError): except (IOError, OSError):
return return
if shebang.startswith('#!'):
return shebang[2:].split(None, 1)[0]
def getCleanEnvironment(logger, home_path='/tmp'): def getCleanEnvironment(logger, home_path='/tmp'):
......
...@@ -40,6 +40,7 @@ else: ...@@ -40,6 +40,7 @@ else:
import subprocess32 as subprocess import subprocess32 as subprocess
import mock import mock
import six
import slapos.grid.utils import slapos.grid.utils
...@@ -473,3 +474,28 @@ class TestSetRunning(unittest.TestCase): ...@@ -473,3 +474,28 @@ class TestSetRunning(unittest.TestCase):
tf.name) tf.name)
with open(tf.name) as f: with open(tf.name) as f:
self.assertEqual(f.read(), str(os.getpid())) self.assertEqual(f.read(), str(os.getpid()))
@unittest.skipIf(six.PY2, "Python3 only")
class TestGetPythonExecutableFromBinBuildout(unittest.TestCase):
def test_simple_shebang(self):
with tempfile.TemporaryDirectory() as d:
binpath = os.path.join(d, 'bin')
python = os.path.realpath(os.path.join(d, 'python'))
os.mkdir(binpath)
with open(os.path.join(binpath, 'buildout'), 'w') as f:
f.write('#!' + python)
self.assertEqual(
slapos.grid.utils.getPythonExecutableFromSoftwarePath(d),
python)
def test_exec_wrapper(self):
with tempfile.TemporaryDirectory() as d:
binpath = os.path.join(d, 'bin')
python = os.path.realpath(os.path.join(d, 'python'))
os.mkdir(binpath)
with open(os.path.join(binpath, 'buildout'), 'w') as f:
f.write('#!/bin/sh\n"exec" "%s" "$0" "$@"' % python)
self.assertEqual(
slapos.grid.utils.getPythonExecutableFromSoftwarePath(d),
python)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment