Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ repos:
rev: v0.930
hooks:
- id: mypy
additional_dependencies: [types-six]
additional_dependencies: [types-six, types-toml]
- repo: https://github.com/pycqa/flake8
rev: 4.0.1
hooks:
Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ No need.
Adding `setup_requires=['setuptools-git-versioning']` somewhere in `setup.py` will automatically download the latest version from PyPi and save it in the `.eggs` folder when `setup.py` is run.

## Usage
### pyproject.toml
Just add this line into your `pyproject.toml`:

```toml
[tool.setuptools_git_versioning]
```

### setup.py
Just add these lines into your `setup.py`:

```python
Expand Down Expand Up @@ -447,7 +454,26 @@ setuptools.setup(

Default options are:

```toml
# pyproject.toml
[build-system]
requires = [
"setuptools>=45",
"wheel",
"setuptools-git-versioning",
]
build-backend = "setuptools.build_meta"

[tool.setuptools_git_versioning]
template = "{tag}"
dev_template = "{tag}.post{ccount}+git.{sha}"
dirty_template = "{tag}.post{ccount}+git.{sha}.dirty"
starting_version = "0.0.1"
count_commits_from_version_file = false
```

```python
# setup.py
setuptools.setup(
version_config={
"template": "{tag}",
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
setuptools; python_version>="3.5"
setuptools<45.0; python_version<"3"
six>=1.13.0
toml>=0.10.2
typing; python_version<"3"
5 changes: 5 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
entry_points={
"distutils.setup_keywords": [
"version_config = setuptools_git_versioning:parse_config",
"setuptools_git_versioning = setuptools_git_versioning:parse_config",
],
"setuptools.finalize_distribution_options": [
"version_config = setuptools_git_versioning:infer_version",
"setuptools_git_versioning = setuptools_git_versioning:infer_version",
],
},
include_package_data=True,
Expand Down
71 changes: 50 additions & 21 deletions setuptools_git_versioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from distutils.errors import DistutilsSetupError
from typing import Any, Callable, List, Optional, Union

import toml
from setuptools.dist import Distribution
from six.moves import collections_abc

Expand All @@ -15,6 +16,18 @@
ENV_VARS_REGEXP = re.compile(r"\{env:([^:}]+):?([^}]+}?)?\}", re.IGNORECASE | re.UNICODE) # type: re.Pattern
TIMESTAMP_REGEXP = re.compile(r"\{timestamp:?([^:}]+)?\}", re.IGNORECASE | re.UNICODE) # type: re.Pattern

DEFAULT_CONFIG = {
"template": DEFAULT_TEMPLATE,
"dev_template": DEFAULT_DEV_TEMPLATE,
"dirty_template": DEFAULT_DIRTY_TEMPLATE,
"starting_version": DEFAULT_STARTING_VERSION,
"version_callback": None,
"version_file": None,
"count_commits_from_version_file": False,
"branch_formatter": None,
"sort_by": None,
}


def _exec(cmd): # type: (str) -> List[str]
try:
Expand Down Expand Up @@ -92,6 +105,37 @@ def count_since(name): # type: (str) -> Optional[int]
return None


def load_config_from_dict(dictionary): # type: (Union[dict, collections_abc.Mapping]) -> dict
config = {}
for key, value in DEFAULT_CONFIG.items():
config[key] = dictionary.get(key, value)
return config


def load_config_from_toml(file_name): # type: (str) -> dict
with open(file_name, encoding="UTF-8", mode="r") as f:
data = f.read()
parsed_file = toml.loads(data)

filtered_file = parsed_file.get("tool", {}).get("setuptools_git_versioning", {})

config = load_config_from_dict(filtered_file)

return config


def infer_version(dist): # type: (Distribution) -> None
pyproject = "pyproject.toml"
if not os.path.isfile(pyproject):
return

config = load_config_from_toml(pyproject)

version = version_from_git(**config)

dist.metadata.version = version


def parse_config(dist, _, value): # type: (Distribution, Any, Any) -> None
if isinstance(value, bool):
if value:
Expand All @@ -104,29 +148,14 @@ def parse_config(dist, _, value): # type: (Distribution, Any, Any) -> None
if not isinstance(value, collections_abc.Mapping):
raise DistutilsSetupError("Config in the wrong format")

template = value.get("template", DEFAULT_TEMPLATE)
dev_template = value.get("dev_template", DEFAULT_DEV_TEMPLATE)
dirty_template = value.get("dirty_template", DEFAULT_DIRTY_TEMPLATE)
starting_version = value.get("starting_version", DEFAULT_STARTING_VERSION)
version_callback = value.get("version_callback", None)
version_file = value.get("version_file", None)
count_commits_from_version_file = value.get("count_commits_from_version_file", False)
branch_formatter = value.get("branch_formatter", None)
sort_by = value.get("sort_by", None)

version = version_from_git(
template=template,
dev_template=dev_template,
dirty_template=dirty_template,
starting_version=starting_version,
version_callback=version_callback,
version_file=version_file,
count_commits_from_version_file=count_commits_from_version_file,
branch_formatter=branch_formatter,
sort_by=sort_by,
)
config = load_config_from_dict(value)

version = version_from_git(**config)

dist.metadata.version = version

infer_version(dist)


def read_version_from_file(path): # type: (Union[str, os.PathLike]) -> str
with open(path) as file:
Expand Down