Commit a854f592 by Sébastien Eustace

Improve Venv.exec()

parent d0fb2f11
...@@ -2,6 +2,8 @@ import os ...@@ -2,6 +2,8 @@ import os
import subprocess import subprocess
import sys import sys
import pexpect
from contextlib import contextmanager from contextlib import contextmanager
from pathlib import Path from pathlib import Path
from subprocess import CalledProcessError from subprocess import CalledProcessError
...@@ -153,14 +155,18 @@ class Venv: ...@@ -153,14 +155,18 @@ class Venv:
return output.decode() return output.decode()
def exec(self, bin, *args): def exec(self, bin, *args, **kwargs):
if not self.is_venv(): if not self.is_venv():
return subprocess.run([bin] + list(args)).returncode return subprocess.run([bin] + list(args)).returncode
else: else:
with self.temp_environ(): with self.temp_environ():
os.environ['PATH'] = self._path() os.environ['PATH'] = self._path()
os.environ['VIRTUAL_ENV'] = str(self._venv)
self.unset_env('PYTHONHOME')
self.unset_env('__PYVENV_LAUNCHER__')
completed = subprocess.run([bin] + list(args)) completed = subprocess.run([bin] + list(args), **kwargs)
return completed.returncode return completed.returncode
...@@ -179,6 +185,10 @@ class Venv: ...@@ -179,6 +185,10 @@ class Venv:
os.environ['PATH'], os.environ['PATH'],
]) ])
def unset_env(self, key):
if key in os.environ:
del os.environ[key]
def get_shell(self): def get_shell(self):
shell = Path(os.environ.get('SHELL', '')).stem shell = Path(os.environ.get('SHELL', '')).stem
if shell in ('bash', 'zsh', 'fish'): if shell in ('bash', 'zsh', 'fish'):
......
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