diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e70d98f..1b1339b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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: diff --git a/README.md b/README.md index bfcfa46..5f7da66 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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}", diff --git a/requirements.txt b/requirements.txt index bc5dda6..d31b935 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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" diff --git a/setup.py b/setup.py index 31fd999..b18e118 100644 --- a/setup.py +++ b/setup.py @@ -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, diff --git a/setuptools_git_versioning.py b/setuptools_git_versioning.py index 0d7e72c..3de0f97 100644 --- a/setuptools_git_versioning.py +++ b/setuptools_git_versioning.py @@ -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 @@ -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: @@ -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: @@ -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: