Skip to content

Implement declarative config for compatibility with setuptools declarative config #364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 25, 2019
Merged
4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ def parse(root):
[setuptools.file_finders]
setuptools_scm = setuptools_scm.integration:find_files

[setuptools.finalize_distribution_options]
setuptools_scm = setuptools_scm.integration:infer_version

[setuptools_scm.parse_scm]
.hg = setuptools_scm.hg:parse
.git = setuptools_scm.git:parse
Expand Down Expand Up @@ -111,6 +114,7 @@ def parse(root):
"Topic :: Utilities",
],
python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*",
extras_require=dict(toml=["toml"]),
)

if __name__ == "__main__":
Expand Down
17 changes: 13 additions & 4 deletions src/setuptools_scm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ def dump_version(root, version, write_to, template=None):


def _do_parse(config):
if not config.enabled:
return

pretended = os.environ.get(PRETEND_KEY)
if pretended:
# we use meta here since the pretended version
Expand Down Expand Up @@ -146,18 +149,24 @@ def get_version(
config.fallback_version = fallback_version
config.parse = parse
config.git_describe_command = git_describe_command
config.enabled = True
return _get_version(config)


def _get_version(config):
parsed_version = _do_parse(config)

if parsed_version:
version_string = format_version(
parsed_version, version_scheme=version_scheme, local_scheme=local_scheme
parsed_version,
version_scheme=config.version_scheme,
local_scheme=config.local_scheme,
)
dump_version(
root=root,
root=config.root,
version=version_string,
write_to=write_to,
template=write_to_template,
write_to=config.write_to,
template=config.write_to_template,
)

return version_string
14 changes: 14 additions & 0 deletions src/setuptools_scm/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Configuration(object):
parse = None
_tag_regex = None
_absolute_root = None
enabled = False

def __init__(self, relative_to=None, root="."):
# TODO:
Expand Down Expand Up @@ -105,3 +106,16 @@ def tag_regex(self):
@tag_regex.setter
def tag_regex(self, value):
self._tag_regex = _check_tag_regex(value)

@classmethod
def from_file(cls, name="pyproject.toml"):
"""
Read Configuration from pyproject.toml (or similar)
"""
if not os.path.isfile(name):
return cls()
with open(name) as strm:
defn = __import__("toml").load(strm)
config = cls()
vars(config).update(defn.get("tool", {}).get("setuptools_scm", {}))
return config
9 changes: 8 additions & 1 deletion src/setuptools_scm/integration.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from pkg_resources import iter_entry_points

from .version import _warn_if_setuptools_outdated
from .config import Configuration
from .utils import do
from . import get_version
from . import get_version, _get_version


def version_keyword(dist, keyword, value):
Expand All @@ -28,3 +29,9 @@ def find_files(path=""):
if res:
return res
return []


def infer_version(dist):
version = _get_version(Configuration.from_file())
if version:
dist.metadata.version = version
9 changes: 9 additions & 0 deletions testing/test_config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import unicode_literals

from setuptools_scm.config import Configuration

import pytest
Expand All @@ -18,3 +20,10 @@ def test_tag_regex(tag, expected_version):
match = config.tag_regex.match(tag)
version = match.group("version")
assert version == expected_version


def test_config_from_pyproject(tmpdir):
fn = tmpdir / "pyproject.toml"
fn.write_text("[tool.setuptools_scm]\nenabled = true\n", encoding="utf-8")
config = Configuration.from_file(str(fn))
assert config.enabled
17 changes: 17 additions & 0 deletions testing/test_integration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import sys

from setuptools_scm.utils import do


def test_pyproject_support(tmpdir, monkeypatch):
monkeypatch.delenv("SETUPTOOLS_SCM_DEBUG")
pkg = tmpdir.ensure("package", dir=42)
pkg.join("pyproject.toml").write(
"""[tool.setuptools_scm]
enabled = true
fallback_version = "12.34"
"""
)
pkg.join("setup.py").write("__import__('setuptools').setup()")
res = do((sys.executable, "setup.py", "--version"), pkg)
assert res == "12.34"
3 changes: 3 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ skip_install=
test: False
deps=
pytest
setuptools >= 42
commands=
test: py.test []
selfcheck: python setup.py --version
extras =
toml

[testenv:flake8]
skip_install=True
Expand Down