Skip to content

Commit

Permalink
rearrange EnvManager build_venv()
Browse files Browse the repository at this point in the history
so that it is possible to force installation of setuptools and wheel
  • Loading branch information
dimbleby committed Sep 3, 2023
1 parent ccae5c0 commit 15d0428
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/poetry/inspection/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ def get_pep517_metadata(path: Path) -> PackageInfo:
return info

with ephemeral_environment(
flags={"no-pip": False, "no-setuptools": False, "no-wheel": False}
flags={"no-pip": False, "setuptools": "bundle", "wheel": "bundle"}
) as venv:
# TODO: cache PEP 517 build environment corresponding to each project venv
dest_dir = venv.path.parent / "dist"
Expand Down
2 changes: 1 addition & 1 deletion src/poetry/utils/env/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
@contextmanager
def ephemeral_environment(
executable: Path | None = None,
flags: dict[str, bool] | None = None,
flags: dict[str, str | bool] | None = None,
) -> Iterator[VirtualEnv]:
with temporary_directory() as tmp_dir:
# TODO: cache PEP 517 build environment corresponding to each project venv
Expand Down
48 changes: 28 additions & 20 deletions src/poetry/utils/env/env_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,35 +612,40 @@ def build_venv(
cls,
path: Path,
executable: Path | None = None,
flags: dict[str, bool] | None = None,
flags: dict[str, str | bool] | None = None,
with_pip: bool | None = None,
with_wheel: bool | None = None,
with_setuptools: bool | None = None,
prompt: str | None = None,
) -> virtualenv.run.session.Session:
if WINDOWS:
path = get_real_windows_path(path)
executable = get_real_windows_path(executable) if executable else None

flags = flags or {}

flags["no-pip"] = (
not with_pip if with_pip is not None else flags.pop("no-pip", True)
)
if with_pip is not None:
flags["no-pip"] = not with_pip

flags["no-setuptools"] = (
not with_setuptools
if with_setuptools is not None
else flags.pop("no-setuptools", True)
)
if with_wheel is not None:
wheel_flags: dict[str, str | bool] = (
{"wheel": "bundle"} if with_wheel else {"no-wheel": True}
)
flags.update(wheel_flags)

# we want wheels to be enabled when pip is required and it has not been
# explicitly disabled
flags["no-wheel"] = (
not with_wheel
if with_wheel is not None
else flags.pop("no-wheel", flags["no-pip"])
)
if with_setuptools is not None:
setuptools_flags: dict[str, str | bool] = (
{"setuptools": "bundle"} if with_setuptools else {"no-setuptools": True}
)
flags.update(setuptools_flags)

flags.setdefault("no-pip", True)

if "setuptools" not in flags and "no-setuptools" not in flags:
flags["no-setuptools"] = True

if "wheel" not in flags and "no-wheel" not in flags:
flags["no-wheel"] = True

if WINDOWS:
path = get_real_windows_path(path)
executable = get_real_windows_path(executable) if executable else None

executable_str = None if executable is None else executable.resolve().as_posix()

Expand All @@ -658,6 +663,9 @@ def build_venv(
if value is True:
args.append(f"--{flag}")

elif value is not False:
args.append(f"--{flag}={value}")

args.append(str(path))

cli_result = virtualenv.cli_run(args)
Expand Down
7 changes: 5 additions & 2 deletions tests/utils/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -1489,18 +1489,21 @@ def test_env_system_packages_are_relative_to_lib(
@pytest.mark.parametrize(
("flags", "packages"),
[
({"no-pip": False}, {"pip", "wheel"}),
({"no-pip": False}, {"pip"}),
({"no-pip": False, "no-wheel": True}, {"pip"}),
({"no-pip": False, "no-wheel": False}, {"pip", "wheel"}),
({"no-pip": True}, set()),
({"no-setuptools": False}, {"setuptools"}),
({"no-setuptools": True}, set()),
({"setuptools": "bundle"}, {"setuptools"}),
({"no-pip": True, "no-setuptools": False}, {"setuptools"}),
({"no-wheel": False}, {"wheel"}),
({"wheel": "bundle"}, {"wheel"}),
({}, set()),
],
)
def test_env_no_pip(
tmp_path: Path, poetry: Poetry, flags: dict[str, bool], packages: set[str]
tmp_path: Path, poetry: Poetry, flags: dict[str, str | bool], packages: set[str]
) -> None:
venv_path = tmp_path / "venv"
EnvManager(poetry).build_venv(path=venv_path, flags=flags)
Expand Down

0 comments on commit 15d0428

Please sign in to comment.