Commit 8a2eceb6 by Bartosz Sokorski Committed by GitHub

Added virtualenvs.prompt option to config (#5606)

parent f1c589d9
......@@ -60,6 +60,7 @@ virtualenvs.options.no-setuptools = false
virtualenvs.options.system-site-packages = false
virtualenvs.path = "{cache-dir}/virtualenvs" # /path/to/cache/directory/virtualenvs
virtualenvs.prefer-active-python = false
virtualenvs.prompt = "{project_name}-py{python_version}"
```
## Displaying a single configuration setting
......@@ -228,6 +229,14 @@ existing `.venv` directory.
Directory where virtual environments will be created.
Defaults to `{cache-dir}/virtualenvs` (`{cache-dir}\virtualenvs` on Windows).
### `virtualenvs.prompt`
**Type**: string
Format string defining the prompt to be displayed when the virtual environment is activated.
The variables `project_name` and `python_version` are available for formatting.
Defaults to `"{project_name}-py{python_version}"`.
### `virtualenvs.options.always-copy`
**Type**: boolean
......
......@@ -123,6 +123,7 @@ class Config:
"no-setuptools": False,
},
"prefer-active-python": False,
"prompt": "{project_name}-py{python_version}",
},
"experimental": {"new-installer": True, "system-git-client": False},
"installer": {"parallel": True, "max-workers": None, "no-binary": None},
......@@ -234,11 +235,17 @@ class Config:
if not isinstance(value, str):
return value
return re.sub(
r"{(.+?)}",
lambda m: self.get(m.group(1)), # type: ignore[no-any-return]
value,
)
def resolve_from_config(match: re.Match[str]) -> Any:
key = match.group(1)
config_value = self.get(key)
if config_value:
return config_value
# The key doesn't exist in the config but might be resolved later,
# so we keep it as a format variable.
return f"{{{key}}}"
return re.sub(r"{(.+?)}", resolve_from_config, value)
@staticmethod
def _get_normalizer(name: str) -> Callable[[str], Any]:
......
......@@ -108,6 +108,11 @@ To remove a repository (repo is a short alias for repositories):
int_normalizer,
None,
),
"virtualenvs.prompt": (
str,
lambda val: str(val),
"{project_name}-py{python_version}",
),
"installer.no-binary": (
PackageFilterPolicy.validator,
PackageFilterPolicy.normalize,
......
......@@ -879,6 +879,7 @@ class EnvManager:
prefer_active_python = self._poetry.config.get(
"virtualenvs.prefer-active-python"
)
venv_prompt = self._poetry.config.get("virtualenvs.prompt")
if not executable and prefer_active_python:
executable = self._detect_active_python(io)
......@@ -980,6 +981,11 @@ class EnvManager:
name = f"{name}-py{python_minor.strip()}"
venv = venv_path / name
if venv_prompt is not None:
venv_prompt = venv_prompt.format(
project_name=self._poetry.package.name, python_version=python_minor
)
if not venv.exists():
if create_venv is False:
io.write_line(
......@@ -1011,6 +1017,7 @@ class EnvManager:
venv,
executable=executable,
flags=self._poetry.config.get("virtualenvs.options"),
prompt=venv_prompt,
)
# venv detection:
......@@ -1040,6 +1047,7 @@ class EnvManager:
with_pip: bool | None = None,
with_wheel: bool | None = None,
with_setuptools: bool | None = None,
prompt: str | None = None,
) -> virtualenv.run.session.Session:
flags = flags or {}
......@@ -1071,6 +1079,9 @@ class EnvManager:
executable or sys.executable,
]
if prompt is not None:
args.extend(["--prompt", prompt])
for flag, value in flags.items():
if value is True:
args.append(f"--{flag}")
......
......@@ -77,6 +77,7 @@ def test_activate_activates_non_existing_virtualenv_no_envs_file(
"no-pip": False,
"no-setuptools": False,
},
prompt="simple-project-py3.7",
)
envs_file = TOMLFile(venv_cache / "envs.toml")
......
......@@ -64,6 +64,7 @@ virtualenvs.options.no-setuptools = false
virtualenvs.options.system-site-packages = false
virtualenvs.path = {venv_path} # {config_cache_dir / 'virtualenvs'}
virtualenvs.prefer-active-python = false
virtualenvs.prompt = "{{project_name}}-py{{python_version}}"
"""
assert tester.io.fetch_output() == expected
......@@ -92,6 +93,7 @@ virtualenvs.options.no-setuptools = false
virtualenvs.options.system-site-packages = false
virtualenvs.path = {venv_path} # {config_cache_dir / 'virtualenvs'}
virtualenvs.prefer-active-python = false
virtualenvs.prompt = "{{project_name}}-py{{python_version}}"
"""
assert config.set_config_source.call_count == 0
......@@ -144,6 +146,7 @@ virtualenvs.options.no-setuptools = false
virtualenvs.options.system-site-packages = false
virtualenvs.path = {venv_path} # {config_cache_dir / 'virtualenvs'}
virtualenvs.prefer-active-python = false
virtualenvs.prompt = "{{project_name}}-py{{python_version}}"
"""
assert config.set_config_source.call_count == 1
......
......@@ -218,6 +218,7 @@ def test_activate_activates_non_existing_virtualenv_no_envs_file(
"no-pip": False,
"no-setuptools": False,
},
prompt="simple-project-py3.7",
)
envs_file = TOMLFile(Path(tmp_dir) / "envs.toml")
......@@ -355,6 +356,7 @@ def test_activate_activates_different_virtualenv_with_envs_file(
"no-pip": False,
"no-setuptools": False,
},
prompt="simple-project-py3.6",
)
assert envs_file.exists()
......@@ -418,6 +420,7 @@ def test_activate_activates_recreates_for_different_patch(
"no-pip": False,
"no-setuptools": False,
},
prompt="simple-project-py3.7",
)
remove_venv_m.assert_called_with(Path(tmp_dir) / f"{venv_name}-py3.7")
......@@ -855,6 +858,7 @@ def test_create_venv_tries_to_find_a_compatible_python_executable_using_generic_
"no-pip": False,
"no-setuptools": False,
},
prompt="simple-project-py3.7",
)
......@@ -888,6 +892,7 @@ def test_create_venv_tries_to_find_a_compatible_python_executable_using_specific
"no-pip": False,
"no-setuptools": False,
},
prompt="simple-project-py3.9",
)
......@@ -980,6 +985,7 @@ def test_create_venv_uses_patch_version_to_detect_compatibility(
"no-pip": False,
"no-setuptools": False,
},
prompt=f"simple-project-py{version.major}.{version.minor}",
)
......@@ -1021,6 +1027,7 @@ def test_create_venv_uses_patch_version_to_detect_compatibility_with_executable(
"no-pip": False,
"no-setuptools": False,
},
prompt=f"simple-project-py{version.major}.{version.minor - 1}",
)
......@@ -1091,6 +1098,7 @@ def test_activate_with_in_project_setting_does_not_fail_if_no_venvs_dir(
"no-pip": False,
"no-setuptools": False,
},
prompt="simple-project-py3.7",
)
envs_file = TOMLFile(Path(tmp_dir) / "virtualenvs" / "envs.toml")
......@@ -1324,6 +1332,7 @@ def test_create_venv_accepts_fallback_version_w_nonzero_patchlevel(
"no-pip": False,
"no-setuptools": False,
},
prompt="simple-project-py3.5",
)
......
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