Skip to content

Commit 62c14a0

Browse files
BloodmalletBloodmalletdolfinus
authored
enable usage of pyproject.toml (#37)
* add `pyproject.toml` standalone functionality Co-authored-by: Bloodmallet <[email protected]> Co-authored-by: Martynov Maxim <[email protected]>
1 parent dcec019 commit 62c14a0

File tree

5 files changed

+83
-22
lines changed

5 files changed

+83
-22
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ repos:
6363
rev: v0.930
6464
hooks:
6565
- id: mypy
66-
additional_dependencies: [types-six]
66+
additional_dependencies: [types-six, types-toml]
6767
- repo: https://github.com/pycqa/flake8
6868
rev: 4.0.1
6969
hooks:

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@ No need.
3030
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.
3131

3232
## Usage
33+
### pyproject.toml
34+
Just add this line into your `pyproject.toml`:
3335

36+
```toml
37+
[tool.setuptools_git_versioning]
38+
```
39+
40+
### setup.py
3441
Just add these lines into your `setup.py`:
3542

3643
```python
@@ -447,7 +454,26 @@ setuptools.setup(
447454
448455
Default options are:
449456
457+
```toml
458+
# pyproject.toml
459+
[build-system]
460+
requires = [
461+
"setuptools>=45",
462+
"wheel",
463+
"setuptools-git-versioning",
464+
]
465+
build-backend = "setuptools.build_meta"
466+
467+
[tool.setuptools_git_versioning]
468+
template = "{tag}"
469+
dev_template = "{tag}.post{ccount}+git.{sha}"
470+
dirty_template = "{tag}.post{ccount}+git.{sha}.dirty"
471+
starting_version = "0.0.1"
472+
count_commits_from_version_file = false
473+
```
474+
450475
```python
476+
# setup.py
451477
setuptools.setup(
452478
version_config={
453479
"template": "{tag}",

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
setuptools; python_version>="3.5"
22
setuptools<45.0; python_version<"3"
33
six>=1.13.0
4+
toml>=0.10.2
45
typing; python_version<"3"

setup.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@
4545
entry_points={
4646
"distutils.setup_keywords": [
4747
"version_config = setuptools_git_versioning:parse_config",
48+
"setuptools_git_versioning = setuptools_git_versioning:parse_config",
49+
],
50+
"setuptools.finalize_distribution_options": [
51+
"version_config = setuptools_git_versioning:infer_version",
52+
"setuptools_git_versioning = setuptools_git_versioning:infer_version",
4853
],
4954
},
5055
include_package_data=True,

setuptools_git_versioning.py

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from distutils.errors import DistutilsSetupError
66
from typing import Any, Callable, List, Optional, Union
77

8+
import toml
89
from setuptools.dist import Distribution
910
from six.moves import collections_abc
1011

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

19+
DEFAULT_CONFIG = {
20+
"template": DEFAULT_TEMPLATE,
21+
"dev_template": DEFAULT_DEV_TEMPLATE,
22+
"dirty_template": DEFAULT_DIRTY_TEMPLATE,
23+
"starting_version": DEFAULT_STARTING_VERSION,
24+
"version_callback": None,
25+
"version_file": None,
26+
"count_commits_from_version_file": False,
27+
"branch_formatter": None,
28+
"sort_by": None,
29+
}
30+
1831

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

94107

108+
def load_config_from_dict(dictionary): # type: (Union[dict, collections_abc.Mapping]) -> dict
109+
config = {}
110+
for key, value in DEFAULT_CONFIG.items():
111+
config[key] = dictionary.get(key, value)
112+
return config
113+
114+
115+
def load_config_from_toml(file_name): # type: (str) -> dict
116+
with open(file_name, encoding="UTF-8", mode="r") as f:
117+
data = f.read()
118+
parsed_file = toml.loads(data)
119+
120+
filtered_file = parsed_file.get("tool", {}).get("setuptools_git_versioning", {})
121+
122+
config = load_config_from_dict(filtered_file)
123+
124+
return config
125+
126+
127+
def infer_version(dist): # type: (Distribution) -> None
128+
pyproject = "pyproject.toml"
129+
if not os.path.isfile(pyproject):
130+
return
131+
132+
config = load_config_from_toml(pyproject)
133+
134+
version = version_from_git(**config)
135+
136+
dist.metadata.version = version
137+
138+
95139
def parse_config(dist, _, value): # type: (Distribution, Any, Any) -> None
96140
if isinstance(value, bool):
97141
if value:
@@ -104,29 +148,14 @@ def parse_config(dist, _, value): # type: (Distribution, Any, Any) -> None
104148
if not isinstance(value, collections_abc.Mapping):
105149
raise DistutilsSetupError("Config in the wrong format")
106150

107-
template = value.get("template", DEFAULT_TEMPLATE)
108-
dev_template = value.get("dev_template", DEFAULT_DEV_TEMPLATE)
109-
dirty_template = value.get("dirty_template", DEFAULT_DIRTY_TEMPLATE)
110-
starting_version = value.get("starting_version", DEFAULT_STARTING_VERSION)
111-
version_callback = value.get("version_callback", None)
112-
version_file = value.get("version_file", None)
113-
count_commits_from_version_file = value.get("count_commits_from_version_file", False)
114-
branch_formatter = value.get("branch_formatter", None)
115-
sort_by = value.get("sort_by", None)
116-
117-
version = version_from_git(
118-
template=template,
119-
dev_template=dev_template,
120-
dirty_template=dirty_template,
121-
starting_version=starting_version,
122-
version_callback=version_callback,
123-
version_file=version_file,
124-
count_commits_from_version_file=count_commits_from_version_file,
125-
branch_formatter=branch_formatter,
126-
sort_by=sort_by,
127-
)
151+
config = load_config_from_dict(value)
152+
153+
version = version_from_git(**config)
154+
128155
dist.metadata.version = version
129156

157+
infer_version(dist)
158+
130159

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

0 commit comments

Comments
 (0)