Commit a2e2c652 by Sébastien Eustace

Fix env detection

parent 473156ba
...@@ -97,16 +97,18 @@ class Env(object): ...@@ -97,16 +97,18 @@ class Env(object):
if cwd and (cwd / ".venv").exists(): if cwd and (cwd / ".venv").exists():
venv = cwd / ".venv" venv = cwd / ".venv"
return VirtualEnv( return VirtualEnv(Path(venv))
Path(venv), Path(getattr(sys, "base_prefix", sys.prefix))
)
return SystemEnv(Path(sys.prefix)) return SystemEnv(Path(sys.prefix))
return VirtualEnv( if os.environ.get("VIRTUAL_ENV") is not None:
Path(getattr(sys, "real_prefix", sys.prefix)), prefix = Path(os.environ["VIRTUAL_ENV"])
Path(getattr(sys, "base_prefix", sys.prefix)), base_prefix = None
) else:
prefix = sys.prefix
base_prefix = cls.get_base_prefix()
return VirtualEnv(prefix, base_prefix)
@classmethod @classmethod
def create_venv(cls, io, name=None, cwd=None): # type: (IO, bool, Path) -> Env def create_venv(cls, io, name=None, cwd=None): # type: (IO, bool, Path) -> Env
...@@ -178,11 +180,9 @@ class Env(object): ...@@ -178,11 +180,9 @@ class Env(object):
p_venv = os.path.normcase(str(venv)) p_venv = os.path.normcase(str(venv))
if any(p.startswith(p_venv) for p in paths): if any(p.startswith(p_venv) for p in paths):
# Running properly in the virtualenv, don't need to do anything # Running properly in the virtualenv, don't need to do anything
return SystemEnv( return SystemEnv(Path(sys.prefix), cls.get_base_prefix())
Path(sys.prefix), Path(getattr(sys, "base_prefix", sys.prefix))
)
return VirtualEnv(venv, Path(getattr(sys, "base_prefix", sys.prefix))) return VirtualEnv(venv)
@classmethod @classmethod
def build_venv(cls, path): def build_venv(cls, path):
...@@ -199,6 +199,15 @@ class Env(object): ...@@ -199,6 +199,15 @@ class Env(object):
build(path) build(path)
def get_base_prefix(self): # type: () -> Path
if hasattr(sys, "real_prefix"):
return sys.real_prefix
if hasattr(sys, "base_prefix"):
return sys.base_prefix
return sys.prefix
def get_version_info(self): # type: () -> Tuple[int] def get_version_info(self): # type: () -> Tuple[int]
raise NotImplementedError() raise NotImplementedError()
...@@ -284,6 +293,30 @@ class VirtualEnv(Env): ...@@ -284,6 +293,30 @@ class VirtualEnv(Env):
A virtual Python environment. A virtual Python environment.
""" """
def __init__(self, path, base=None): # type: (Path, Optional[Path]) -> None
super(VirtualEnv, self).__init__(path, base)
# If base is None, it probably means this is
# a virtualenv created from VIRTUAL_ENV.
# In this case we need to get sys.base_prefix
# from inside the virtualenv.
if base is None:
self._base = Path(
self.run(
"python",
"-c",
'"import sys; '
"print("
" getattr("
" sys,"
" 'real_prefix', "
" getattr(sys, 'base_prefix', sys.prefix)"
" )"
')"',
shell=True,
)
)
def get_version_info(self): # type: () -> Tuple[int] def get_version_info(self): # type: () -> Tuple[int]
output = self.run( output = self.run(
"python", "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