Skip to content

Commit 4a8f56c

Browse files
authored
Add support to Poetry's non-package mode (#119)
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
1 parent 6a55c18 commit 4a8f56c

File tree

4 files changed

+86
-23
lines changed

4 files changed

+86
-23
lines changed

src/poetry_plugin_bundle/bundlers/venv_bundler.py

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -165,30 +165,38 @@ def locked_repository(self) -> LockfileRepository:
165165
)
166166
return False
167167

168-
self._write(
169-
io,
170-
f"{message}: <info>Installing <c1>{poetry.package.pretty_name}</c1>"
171-
f" (<b>{poetry.package.pretty_version}</b>)</info>",
172-
)
168+
# Skip building the wheel if is_package_mode exists and is set to false
169+
if hasattr(poetry, "is_package_mode") and not poetry.is_package_mode:
170+
self._write(
171+
io,
172+
f"{message}: <info>Skipping installation for non package project"
173+
f" <c1>{poetry.package.pretty_name}</c1>",
174+
)
175+
else:
176+
self._write(
177+
io,
178+
f"{message}: <info>Installing <c1>{poetry.package.pretty_name}</c1>"
179+
f" (<b>{poetry.package.pretty_version}</b>)</info>",
180+
)
173181

174-
# Build a wheel of the project in a temporary directory
175-
# and install it in the newly create virtual environment
176-
with TemporaryDirectory() as directory:
177-
try:
178-
wheel_name = WheelBuilder.make_in(poetry, directory=Path(directory))
179-
wheel = Path(directory).joinpath(wheel_name)
180-
package = Package(
181-
poetry.package.name,
182-
poetry.package.version,
183-
source_type="file",
184-
source_url=str(wheel),
185-
)
186-
installer.executor.execute([Install(package)])
187-
except ModuleOrPackageNotFound:
188-
warnings.append(
189-
"The root package was not installed because no matching module or"
190-
" package was found."
191-
)
182+
# Build a wheel of the project in a temporary directory
183+
# and install it in the newly create virtual environment
184+
with TemporaryDirectory() as directory:
185+
try:
186+
wheel_name = WheelBuilder.make_in(poetry, directory=Path(directory))
187+
wheel = Path(directory).joinpath(wheel_name)
188+
package = Package(
189+
poetry.package.name,
190+
poetry.package.version,
191+
source_type="file",
192+
source_url=str(wheel),
193+
)
194+
installer.executor.execute([Install(package)])
195+
except ModuleOrPackageNotFound:
196+
warnings.append(
197+
"The root package was not installed because no matching module or"
198+
" package was found."
199+
)
192200

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

tests/bundlers/test_venv_bundler.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,3 +395,30 @@ def test_bundler_should_build_a_venv_at_specified_path_if_centralized_venv_exist
395395
• Bundled simple-project (1.2.3) into {path}
396396
"""
397397
assert expected == io.fetch_output()
398+
399+
400+
def test_bundler_non_package_mode(
401+
io: BufferedIO, tmp_venv: VirtualEnv, mocker: MockerFixture, config: Config
402+
) -> None:
403+
poetry = Factory().create_poetry(
404+
Path(__file__).parent.parent / "fixtures" / "non_package_mode"
405+
)
406+
poetry.set_config(config)
407+
408+
mocker.patch("poetry.installation.executor.Executor._execute_operation")
409+
410+
bundler = VenvBundler()
411+
bundler.set_path(tmp_venv.path)
412+
bundler.set_remove(True)
413+
414+
assert bundler.bundle(poetry, io)
415+
416+
path = str(tmp_venv.path)
417+
expected = f"""\
418+
• Bundling simple-project-non-package-mode (1.2.3) into {path}
419+
• Bundling simple-project-non-package-mode (1.2.3) into {path}: Creating a virtual environment using Poetry-determined Python
420+
• Bundling simple-project-non-package-mode (1.2.3) into {path}: Installing dependencies
421+
• Bundling simple-project-non-package-mode (1.2.3) into {path}: Skipping installation for non package project simple-project-non-package-mode
422+
• Bundled simple-project-non-package-mode (1.2.3) into {path}
423+
"""
424+
assert expected == io.fetch_output()

tests/fixtures/non_package_mode/poetry.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[tool.poetry]
2+
name = "simple-project-non-package-mode"
3+
version = "1.2.3"
4+
package-mode = false
5+
6+
# Requirements
7+
[tool.poetry.dependencies]
8+
python = "~2.7 || ^3.4"
9+
10+
11+
[tool.poetry.group.dev]
12+
optional = true
13+
dependencies = {}
14+
15+
[tool.poetry.scripts]
16+
foo = "foo:bar"
17+
baz = "bar:baz.boom.bim"

0 commit comments

Comments
 (0)