Commit e6261dc9 by Sébastien Eustace

Fix in-project virtual env detection

parent 567604d9
...@@ -16,7 +16,7 @@ class DebugInfoCommand(Command): ...@@ -16,7 +16,7 @@ class DebugInfoCommand(Command):
poetry = self.poetry poetry = self.poetry
package = poetry.package package = poetry.package
env = Env.get() env = Env.get(cwd=poetry.file.parent)
poetry_python_version = ".".join(str(s) for s in sys.version_info[:3]) poetry_python_version = ".".join(str(s) for s in sys.version_info[:3])
......
...@@ -81,7 +81,7 @@ class Env(object): ...@@ -81,7 +81,7 @@ class Env(object):
return self._bin("pip") return self._bin("pip")
@classmethod @classmethod
def get(cls, reload=False): # type: (IO, bool) -> Env def get(cls, reload=False, cwd=None): # type: (IO, bool) -> Env
if cls._env is not None and not reload: if cls._env is not None and not reload:
return cls._env return cls._env
...@@ -93,6 +93,14 @@ class Env(object): ...@@ -93,6 +93,14 @@ class Env(object):
) )
if not in_venv: if not in_venv:
# Checking if a local virtualenv exists
if cwd and (cwd / ".venv").exists():
venv = cwd / ".venv"
return VirtualEnv(
Path(venv), Path(getattr(sys, "base_prefix", sys.prefix))
)
return SystemEnv(Path(sys.prefix)) return SystemEnv(Path(sys.prefix))
return VirtualEnv( return VirtualEnv(
...@@ -101,24 +109,15 @@ class Env(object): ...@@ -101,24 +109,15 @@ class Env(object):
) )
@classmethod @classmethod
def create_venv(cls, io, name=None, cwd=None): # type: (IO, bool) -> Env def create_venv(cls, io, name=None, cwd=None): # type: (IO, bool, Path) -> Env
if cls._env is not None: if cls._env is not None:
return cls._env return cls._env
# Check if we are inside a virtualenv or not env = cls.get(cwd=cwd)
in_venv = ( if env.is_venv():
os.environ.get("VIRTUAL_ENV") is not None # Already inside a virtualenv.
or hasattr(sys, "real_prefix") return env
or (hasattr(sys, "base_prefix") and sys.base_prefix != sys.prefix)
)
venv = os.environ.get("VIRTUAL_ENV", getattr(sys, "real_prefix", sys.prefix))
venv = Path(venv)
if not in_venv:
# Not currently in a virtual env, we create one
if cwd and (cwd / ".venv").exists():
venv = cwd / ".venv"
else:
config = Config.create("config.toml") config = Config.create("config.toml")
create_venv = config.setting("settings.virtualenvs.create") create_venv = config.setting("settings.virtualenvs.create")
...@@ -127,9 +126,7 @@ class Env(object): ...@@ -127,9 +126,7 @@ class Env(object):
venv_path = config.setting("settings.virtualenvs.path") venv_path = config.setting("settings.virtualenvs.path")
if root_venv: if root_venv:
if not cwd: if not cwd:
raise RuntimeError( raise RuntimeError("Unable to determine the project's directory")
"Unable to determine the project's directory"
)
venv_path = cwd / ".venv" venv_path = cwd / ".venv"
elif venv_path is None: elif venv_path is None:
...@@ -140,9 +137,7 @@ class Env(object): ...@@ -140,9 +137,7 @@ class Env(object):
if not name: if not name:
name = Path.cwd().name name = Path.cwd().name
name = "{}-py{}".format( name = "{}-py{}".format(name, ".".join([str(v) for v in sys.version_info[:2]]))
name, ".".join([str(v) for v in sys.version_info[:2]])
)
if root_venv: if root_venv:
venv = venv_path venv = venv_path
...@@ -161,17 +156,13 @@ class Env(object): ...@@ -161,17 +156,13 @@ class Env(object):
return SystemEnv(Path(sys.prefix)) return SystemEnv(Path(sys.prefix))
io.writeln( io.writeln(
"Creating virtualenv <info>{}</> in {}".format( "Creating virtualenv <info>{}</> in {}".format(name, str(venv_path))
name, str(venv_path)
)
) )
cls.build_venv(str(venv)) cls.build_venv(str(venv))
else: else:
if io.is_very_verbose(): if io.is_very_verbose():
io.writeln( io.writeln("Virtualenv <info>{}</> already exists.".format(name))
"Virtualenv <info>{}</> already exists.".format(name)
)
# venv detection: # venv detection:
# stdlib venv may symlink sys.executable, so we can't use realpath. # stdlib venv may symlink sys.executable, so we can't use realpath.
......
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