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,77 +109,60 @@ class Env(object): ...@@ -101,77 +109,60 @@ 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)) config = Config.create("config.toml")
venv = Path(venv)
if not in_venv: create_venv = config.setting("settings.virtualenvs.create")
# Not currently in a virtual env, we create one root_venv = config.setting("settings.virtualenvs.in-project")
if cwd and (cwd / ".venv").exists():
venv = cwd / ".venv" venv_path = config.setting("settings.virtualenvs.path")
else: if root_venv:
config = Config.create("config.toml") if not cwd:
raise RuntimeError("Unable to determine the project's directory")
create_venv = config.setting("settings.virtualenvs.create")
root_venv = config.setting("settings.virtualenvs.in-project") venv_path = cwd / ".venv"
elif venv_path is None:
venv_path = config.setting("settings.virtualenvs.path") venv_path = Path(CACHE_DIR) / "virtualenvs"
if root_venv: else:
if not cwd: venv_path = Path(venv_path)
raise RuntimeError(
"Unable to determine the project's directory" if not name:
) name = Path.cwd().name
venv_path = cwd / ".venv" name = "{}-py{}".format(name, ".".join([str(v) for v in sys.version_info[:2]]))
elif venv_path is None:
venv_path = Path(CACHE_DIR) / "virtualenvs" if root_venv:
else: venv = venv_path
venv_path = Path(venv_path) else:
venv = venv_path / name
if not name:
name = Path.cwd().name if not venv.exists():
if create_venv is False:
name = "{}-py{}".format( io.writeln(
name, ".".join([str(v) for v in sys.version_info[:2]]) "<fg=black;bg=yellow>"
"Skipping virtualenv creation, "
"as specified in config file."
"</>"
) )
if root_venv: return SystemEnv(Path(sys.prefix))
venv = venv_path
else: io.writeln(
venv = venv_path / name "Creating virtualenv <info>{}</> in {}".format(name, str(venv_path))
)
if not venv.exists():
if create_venv is False: cls.build_venv(str(venv))
io.writeln( else:
"<fg=black;bg=yellow>" if io.is_very_verbose():
"Skipping virtualenv creation, " io.writeln("Virtualenv <info>{}</> already exists.".format(name))
"as specified in config file."
"</>"
)
return SystemEnv(Path(sys.prefix))
io.writeln(
"Creating virtualenv <info>{}</> in {}".format(
name, str(venv_path)
)
)
cls.build_venv(str(venv))
else:
if io.is_very_verbose():
io.writeln(
"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