Skip to content

Commit

Permalink
#129: Don't perform IO during 'poetry version'
Browse files Browse the repository at this point in the history
  • Loading branch information
mtkennerly committed Jun 29, 2023
1 parent cb2c25e commit aae0d1c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## Unreleased

* Changed:
* During `poetry version`, the plugin still activates, but no longer modifies pyproject.toml.

## v0.23.0 (2023-06-13)

* Added:
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ you can apply some global overrides via environment variables.
* `POETRY_DYNAMIC_VERSIONING_COMMANDS`:
You can set a comma-separated list of Poetry commands during which to activate the versioning.
For example, `build,publish` will limit the dynamic versioning to those two commands.
* `POETRY_DYNAMIC_VERSIONING_COMMANDS_NO_IO`:
Comma-separated list of Poetry commands during which the plugin should **not** directly modify files.
Default: `version`.

## Command line mode
The plugin also has a command line mode for execution on demand.
Expand Down
6 changes: 4 additions & 2 deletions poetry_dynamic_versioning/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,9 @@ def _get_and_apply_version(
pyproject: Optional[Mapping] = None,
pyproject_path: Optional[Path] = None,
retain: bool = False,
force: bool = False,
# fmt: off
force: bool = False
io: bool = True
# fmt: on
) -> Optional[str]:
if name is not None and name in _state.projects:
Expand Down Expand Up @@ -439,7 +440,8 @@ def _get_and_apply_version(
# Condition will always be true, but it makes Mypy happy.
if name is not None and original is not None:
_state.projects[name] = _ProjectState(pyproject_path, original, version)
_apply_version(version, config, pyproject_path, retain)
if io:
_apply_version(version, config, pyproject_path, retain)

return name

Expand Down
31 changes: 26 additions & 5 deletions poetry_dynamic_versioning/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@
)

_COMMAND_ENV = "POETRY_DYNAMIC_VERSIONING_COMMANDS"
_COMMAND_NO_IO_ENV = "POETRY_DYNAMIC_VERSIONING_COMMANDS_NO_IO"


def _patch_dependency_versions() -> None:
def _patch_dependency_versions(io: bool) -> None:
"""
The plugin system doesn't seem to expose a way to change dependency
versions, so we patch `Factory.create_poetry()` to do the work there.
Expand All @@ -48,7 +49,7 @@ def _patch_dependency_versions() -> None:
@functools.wraps(Factory.create_poetry)
def patched_create_poetry(*args, **kwargs):
instance = original_create_poetry(*args, **kwargs)
_apply_version_via_plugin(instance)
_apply_version_via_plugin(instance, io=io)
return instance

Factory.create_poetry = patched_create_poetry
Expand All @@ -63,8 +64,22 @@ def _should_apply(command: str) -> bool:
return command not in ["run", "shell", cli.Command.dv, cli.Command.dv_enable]


def _should_apply_with_io(command: str) -> bool:
override = os.environ.get(_COMMAND_NO_IO_ENV)
if override is not None:
return command not in override.split(",")
else:
return command not in ["version"]


def _apply_version_via_plugin(
poetry: Poetry, retain: bool = False, force: bool = False, standalone: bool = False
poetry: Poetry,
retain: bool = False,
force: bool = False,
standalone: bool = False,
# fmt: off
io: bool = True
# fmt: on
) -> None:
name = _get_and_apply_version(
name=poetry.local_config["name"],
Expand All @@ -73,6 +88,7 @@ def _apply_version_via_plugin(
pyproject_path=_get_pyproject_path_from_poetry(poetry.pyproject),
retain=retain,
force=force,
io=io,
)
if name:
version = _state.projects[name].version
Expand Down Expand Up @@ -153,13 +169,18 @@ def _apply_version(
if not _should_apply(event.command.name):
return

_apply_version_via_plugin(self._application.poetry)
_patch_dependency_versions()
io = _should_apply_with_io(event.command.name)

_apply_version_via_plugin(self._application.poetry, io=io)
_patch_dependency_versions(io)

def _revert_version(
self, event: ConsoleCommandEvent, kind: str, dispatcher: EventDispatcher
) -> None:
if not _should_apply(event.command.name):
return

if not _should_apply_with_io(event.command.name):
return

_revert_version()

0 comments on commit aae0d1c

Please sign in to comment.