Skip to content

Commit

Permalink
#164: Add POETRY_DYNAMIC_VERSIONING_DEBUG env var
Browse files Browse the repository at this point in the history
  • Loading branch information
mtkennerly committed Apr 30, 2024
1 parent 3890c4e commit b55623f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* `pattern-prefix` option to add a prefix to the version tag pattern.
* `ignore-untracked` option to control the detection of dirty state.
* `from-file` config section to read a version from a file instead of the VCS.
* `POETRY_DYNAMIC_VERSIONING_DEBUG` environment variable for some logging.
* Changed:
* Updated Dunamai to 1.21.0+ for the latest features.

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,9 @@ you can apply some global overrides via environment variables.
Comma-separated list of Poetry commands during which the plugin should **not** directly modify files.
The plugin will still set the dynamic version in memory so that Poetry itself can write it as needed.
Default: `version`.
* `POETRY_DYNAMIC_VERSIONING_DEBUG`:
If this is set to `1`, then some debug logs will be printed to stderr.
Right now, this logs some cases where substitution doesn't find anything to change.

## Command line mode
The plugin also has a command line mode for execution on demand.
Expand Down
20 changes: 20 additions & 0 deletions poetry_dynamic_versioning/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

_BYPASS_ENV = "POETRY_DYNAMIC_VERSIONING_BYPASS"
_OVERRIDE_ENV = "POETRY_DYNAMIC_VERSIONING_OVERRIDE"
_DEBUG_ENV = "POETRY_DYNAMIC_VERSIONING_DEBUG"

if sys.version_info >= (3, 8):
from typing import TypedDict
Expand Down Expand Up @@ -239,6 +240,13 @@ def _deep_merge_dicts(base: Mapping, addition: Mapping) -> Mapping:
return result


def _debug(message: str) -> None:
enabled = os.environ.get(_DEBUG_ENV) == "1"

if enabled:
print(message, file=sys.stderr)


def _find_higher_file(*names: str, start: Optional[Path] = None) -> Optional[Path]:
# Note: We need to make sure we get a pathlib object. Many tox poetry
# helpers will pass us a string and not a pathlib object. See issue #40.
Expand Down Expand Up @@ -524,19 +532,31 @@ def _substitute_version(name: str, version: str, folders: Sequence[_FolderConfig
files = {} # type: MutableMapping[Path, _FolderConfig]
for folder in folders:
for file_glob in folder.files:
i = 0

# call str() since file_glob here could be a non-internable string
for match in folder.path.glob(str(file_glob)):
i += 1
resolved = match.resolve()
if resolved in files:
continue
files[resolved] = folder

if i == 0:
_debug(
"No files found for substitution with glob '{}' in folder '{}'".format(
file_glob, folder.path
)
)

for file, config in files.items():
original_content = file.read_bytes().decode("utf-8")
new_content = _substitute_version_in_text(version, original_content, config.patterns)
if original_content != new_content:
_state.projects[name].substitutions[file] = original_content
file.write_bytes(new_content.encode("utf-8"))
else:
_debug("No changes made during substitution in file '{}'".format(file))


def _substitute_version_in_text(version: str, content: str, patterns: Sequence[_SubPattern]) -> str:
Expand Down

0 comments on commit b55623f

Please sign in to comment.