Skip to content

Commit

Permalink
#124: Add support for OVERRIDE env var
Browse files Browse the repository at this point in the history
  • Loading branch information
mtkennerly committed Jun 11, 2023
1 parent 235b1d9 commit 9427e87
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## Unreleased

* Added:
* Support for `POETRY_DYNAMIC_VERSIONING_OVERRIDE` environment variable.

## v0.22.0 (2023-05-19)

* Added:
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ you can apply some global overrides via environment variables.
for the active project and any path/SSH dependencies that also use the plugin.
This is mainly for distro package maintainers who need to patch existing releases,
without needing access to the original repository.
* `POETRY_DYNAMIC_VERSIONING_OVERRIDE`:
Use a static version for specific packages only, but leave dynamic versioning enabled otherwise.
For example, `pkg1 = 0.1.0, pkg2 = 0.2.0` (spaces are optional) would set pkg1 to 0.1.0 and pkg2 to 0.2.0.
This only affects packages for which poetry-dynamic-versioning is enabled.
When both variables are set, `OVERRIDE` takes precedence over `BYPASS`.
* `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.
Expand Down
28 changes: 25 additions & 3 deletions poetry_dynamic_versioning/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import tomlkit

_BYPASS_ENV = "POETRY_DYNAMIC_VERSIONING_BYPASS"
_OVERRIDE_ENV = "POETRY_DYNAMIC_VERSIONING_OVERRIDE"


class _ProjectState:
Expand Down Expand Up @@ -184,11 +185,32 @@ def _format_timestamp(value: Optional[dt.datetime]) -> Optional[str]:
return value.strftime("%Y%m%d%H%M%S")


def _get_version(config: Mapping) -> str:
bypass = os.environ.get(_BYPASS_ENV)
def _get_override_version(name: Optional[str], env: Optional[Mapping] = None) -> Optional[str]:
env = env if env is not None else os.environ

if name is not None:
raw_overrides = env.get(_OVERRIDE_ENV)
if raw_overrides is not None:
pairs = raw_overrides.split(",")
for pair in pairs:
if "=" not in pair:
continue
k, v = pair.split("=", 1)
if k.strip() == name:
return v.strip()

bypass = env.get(_BYPASS_ENV)
if bypass is not None:
return bypass

return None


def _get_version(config: Mapping, name: Optional[str] = None) -> str:
override = _get_override_version(name)
if override is not None:
return override

import jinja2
from dunamai import (
bump_version,
Expand Down Expand Up @@ -356,7 +378,7 @@ def _get_and_apply_version(
target_dir = pyproject_path.parent
os.chdir(str(target_dir))
try:
version = _get_version(config)
version = _get_version(config, name)
finally:
os.chdir(str(initial_dir))

Expand Down
20 changes: 20 additions & 0 deletions tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,23 @@ def test__get_version__format_jinja_imports_with_module_and_item(config):
config["format-jinja"] = "{{ pow(2, 3) }}"
config["format-jinja-imports"] = [{"module": "math", "item": "pow"}]
assert plugin._get_version(config) == "8.0"


def test__get_override_version__bypass():
env = {plugin._BYPASS_ENV: "0.1.0"}
assert plugin._get_override_version(None, env) == "0.1.0"
assert plugin._get_override_version("foo", env) == "0.1.0"


def test__get_override_version__override():
env = {plugin._OVERRIDE_ENV: "foo=0.1.0,bar=0.2.0"}
assert plugin._get_override_version(None, env) is None
assert plugin._get_override_version("foo", env) == "0.1.0"
assert plugin._get_override_version("bar", env) == "0.2.0"
assert plugin._get_override_version("baz", env) is None


def test__get_override_version__combined():
env = {plugin._BYPASS_ENV: "0.0.0", plugin._OVERRIDE_ENV: "foo = 0.1.0, bar = 0.2.0"}
assert plugin._get_override_version(None, env) == "0.0.0"
assert plugin._get_override_version("foo", env) == "0.1.0"

0 comments on commit 9427e87

Please sign in to comment.