Skip to content

Commit

Permalink
Merge pull request #7 from claui/ignore-dirty-git
Browse files Browse the repository at this point in the history
Add `template_ignore_dirty_git()` method
  • Loading branch information
claui authored Jun 8, 2024
2 parents b946fd5 + 01b5942 commit 77db080
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 16 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ what that means, then `pyproject-patcher` is probably not for you.
- Remove dependency to `setuptools-git-versioning` from
`build-system.requires`

- Configure `setuptools-git-versioning` to use a version template
without a `.dirty` suffix

## Installation

### Installing from PyPI
Expand Down
7 changes: 7 additions & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,10 @@ with patch_in_place('pyproject.toml') as toml:
toml.set_project_version('1.2.3')
toml.tools.setuptools_git_versioning.remove()
```

## Configure a version template without a `.dirty` suffix

```py
with patch_in_place('pyproject.toml') as toml:
toml.tools.setuptools_git_versioning.template_ignore_dirty_git()
```
56 changes: 43 additions & 13 deletions poetry.lock

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

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ pytest = "*"
sphinx = ">=7.1.2,<7.2.0"
types-colorama = "*"

[tool.poetry.group.test.dependencies]
setuptools-git-versioning = "*"

[tool.poe]
verbosity = -1

Expand Down
3 changes: 3 additions & 0 deletions pyproject_patcher/stubs/setuptools_git_versioning.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# pylint: disable=missing-module-docstring

DEFAULT_DEV_TEMPLATE: str
22 changes: 19 additions & 3 deletions pyproject_patcher/tools/setuptools_git_versioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
from dataclasses import dataclass
from typing import TYPE_CHECKING

import tomlkit

# This guard avoids circular imports
if TYPE_CHECKING:
from ..patcher import PyprojectPatcher

TOOL_NAME = "setuptools-git-versioning"

@dataclass(frozen=True)
class SetuptoolsGitVersioning:
"""This class wraps a `pyproject.toml` model and provides
methods to interact with the `tools.setuptools_git_versioning`
part and other entries related to it.
"""

patcher: "PyprojectPatcher"

def remove(self) -> None:
Expand All @@ -25,5 +27,19 @@ def remove(self) -> None:
requires the `setuptools-git-versioning` module.
"""
self.patcher.dynamic.remove("version")
self.patcher.tool.pop("setuptools-git-versioning")
self.patcher.remove_build_system_dependency("setuptools-git-versioning")
self.patcher.tool.pop(TOOL_NAME)
self.patcher.remove_build_system_dependency(TOOL_NAME)

def template_ignore_dirty_git(self) -> None:
"""Changes `dirty_template` so that it no longer contains a
`.dirty` suffix.
Useful for building system packages based on a VCS revision
checked out with Git in cases where `pyproject.toml` has
been modified before `setuptools_git_versioning` is run.
In that case, having a `.dirty` suffix would be misleading.
"""
section = self.patcher.tool.get(TOOL_NAME)
if not isinstance(section, tomlkit.items.Table):
raise KeyError(f"Expected TOMLKit table, found {type(section)}: {section}")
section["dirty_template"] = "{tag}.post{ccount}+git.{sha}"
15 changes: 15 additions & 0 deletions tests/tools/test_setuptools_git_versioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from collections.abc import Mapping
from pathlib import Path

from setuptools_git_versioning import DEFAULT_DEV_TEMPLATE
import tomlkit

from pyproject_patcher.patcher import patch_in_place
Expand All @@ -19,3 +20,17 @@ def test_remove(self, toml_with_git_versioning_lt_2: Path) -> None:
section = tomlkit.load(file).get("tool")
assert isinstance(section, Mapping)
assert "setuptools-git-versioning" not in section

def test_ignore_dirty_git(self, toml_with_git_versioning_lt_2: Path) -> None:
# When
with patch_in_place(toml_with_git_versioning_lt_2) as toml:
toml.tools.setuptools_git_versioning.template_ignore_dirty_git()

# Then
with toml_with_git_versioning_lt_2.open() as file:
l1_section = tomlkit.load(file).get("tool")
assert isinstance(l1_section, Mapping)
assert "setuptools-git-versioning" in l1_section
l2_section = l1_section.get("setuptools-git-versioning")
assert isinstance(l2_section, Mapping)
assert l2_section.get("dirty_template") == DEFAULT_DEV_TEMPLATE

0 comments on commit 77db080

Please sign in to comment.