Skip to content

Commit

Permalink
Add support to Poetry's non_package mode
Browse files Browse the repository at this point in the history
Since Poetry version 1.8.0 there's a non_package mode allowing using Poetry for dependency management only.
This adds support to that feature, enabling bundling the virtual environment only, without building the package wheel
  • Loading branch information
falkets committed Sep 5, 2024
1 parent d141a87 commit 41928dd
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 23 deletions.
53 changes: 30 additions & 23 deletions src/poetry_plugin_bundle/bundlers/venv_bundler.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,30 +165,37 @@ def locked_repository(self) -> LockfileRepository:
)
return False

self._write(
io,
f"{message}: <info>Installing <c1>{poetry.package.pretty_name}</c1>"
f" (<b>{poetry.package.pretty_version}</b>)</info>",
)
# Skip building the wheel if is_package_mode exists and is set to false
if hasattr(poetry, "is_package_mode") and not poetry.is_package_mode:
self._write(
io,
f"{message}: <info>Skipping installation for non package project <c1>{poetry.package.pretty_name}</c1>",
)
else:
self._write(
io,
f"{message}: <info>Installing <c1>{poetry.package.pretty_name}</c1>"
f" (<b>{poetry.package.pretty_version}</b>)</info>",
)

# Build a wheel of the project in a temporary directory
# and install it in the newly create virtual environment
with TemporaryDirectory() as directory:
try:
wheel_name = WheelBuilder.make_in(poetry, directory=Path(directory))
wheel = Path(directory).joinpath(wheel_name)
package = Package(
poetry.package.name,
poetry.package.version,
source_type="file",
source_url=str(wheel),
)
installer.executor.execute([Install(package)])
except ModuleOrPackageNotFound:
warnings.append(
"The root package was not installed because no matching module or"
" package was found."
)
# Build a wheel of the project in a temporary directory
# and install it in the newly create virtual environment
with TemporaryDirectory() as directory:
try:
wheel_name = WheelBuilder.make_in(poetry, directory=Path(directory))
wheel = Path(directory).joinpath(wheel_name)
package = Package(
poetry.package.name,
poetry.package.version,
source_type="file",
source_url=str(wheel),
)
installer.executor.execute([Install(package)])
except ModuleOrPackageNotFound:
warnings.append(
"The root package was not installed because no matching module or"
" package was found."
)

self._write(io, self._get_message(poetry, self._path, done=True))

Expand Down
27 changes: 27 additions & 0 deletions tests/bundlers/test_venv_bundler.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,30 @@ def test_bundler_should_build_a_venv_at_specified_path_if_centralized_venv_exist
• Bundled simple-project (1.2.3) into {path}
"""
assert expected == io.fetch_output()


def test_bundler_non_package_mode(
io: BufferedIO, tmp_venv: VirtualEnv, mocker: MockerFixture, config: Config
) -> None:
poetry = Factory().create_poetry(
Path(__file__).parent.parent / "fixtures" / "non_package_mode"
)
poetry.set_config(config)

mocker.patch("poetry.installation.executor.Executor._execute_operation")

bundler = VenvBundler()
bundler.set_path(tmp_venv.path)
bundler.set_remove(True)

assert bundler.bundle(poetry, io)

path = str(tmp_venv.path)
expected = f"""\
• Bundling simple-project-non-package-mode (1.2.3) into {path}
• Bundling simple-project-non-package-mode (1.2.3) into {path}: Creating a virtual environment using Poetry-determined Python
• Bundling simple-project-non-package-mode (1.2.3) into {path}: Installing dependencies
• Bundling simple-project-non-package-mode (1.2.3) into {path}: Skipping installation for non package project simple-project-non-package-mode
• Bundled simple-project-non-package-mode (1.2.3) into {path}
"""
assert expected == io.fetch_output()
11 changes: 11 additions & 0 deletions tests/fixtures/non_package_mode/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions tests/fixtures/non_package_mode/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[tool.poetry]
name = "simple-project-non-package-mode"
version = "1.2.3"
package-mode = false

# Requirements
[tool.poetry.dependencies]
python = "~2.7 || ^3.4"


[tool.poetry.group.dev]
optional = true
dependencies = {}

[tool.poetry.scripts]
foo = "foo:bar"
baz = "bar:baz.boom.bim"

0 comments on commit 41928dd

Please sign in to comment.