Skip to content

Commit 6b444b9

Browse files
Merge pull request #500 from RonnyPfannschmidt/fix-321-SETUPTOOLS_SCM_PRETEND_VERSION_FOR_DIST_NAME
fix #321: add SETUPTOOLS_SCM_PRETEND_VERSION_FOR_DIST_NAME
2 parents d0d2973 + 131808a commit 6b444b9

File tree

6 files changed

+56
-5
lines changed

6 files changed

+56
-5
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ v4.2.0
1010
* enhance documentation
1111
* consider SOURCE_DATE_EPOCH for versioning
1212
* add a version_tuple to write_to templates
13+
* fix #321: add suppport for the ``SETUPTOOLS_SCM_PRETEND_VERSION_FOR_${DISTRIBUTION_NAME}`` env var to target the pretend key
1314
* fix #142: clearly list supported scm
1415

1516

README.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,15 @@ Environment variables
442442
its used as the primary source for the version number
443443
in which case it will be a unparsed string
444444

445+
446+
:SETUPTOOLS_SCM_PRETEND_VERSION_FOR_${UPPERCASED_DIST_NAME}:
447+
when defined and not empty,
448+
its used as the primary source for the version number
449+
in which case it will be a unparsed string
450+
451+
it takes precedence over ``SETUPTOOLS_SCM_PRETEND_VERSION``
452+
453+
445454
:SETUPTOOLS_SCM_DEBUG:
446455
when defined and not empty,
447456
a lot of debug information will be printed as part of ``setuptools_scm``

src/setuptools_scm/__init__.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
DEFAULT_LOCAL_SCHEME,
1212
DEFAULT_TAG_REGEX,
1313
)
14-
from .utils import function_has_arg, string_types
14+
from .utils import function_has_arg, string_types, trace
1515
from .version import format_version, meta
1616
from .discover import iter_matching_entrypoints
1717

1818
PRETEND_KEY = "SETUPTOOLS_SCM_PRETEND_VERSION"
19+
PRETEND_KEY_NAMED = PRETEND_KEY + "_FOR_{name}"
1920

2021
TEMPLATES = {
2122
".py": """\
@@ -97,7 +98,18 @@ def dump_version(root, version, write_to, template=None):
9798

9899

99100
def _do_parse(config):
100-
pretended = os.environ.get(PRETEND_KEY)
101+
102+
trace("dist name:", config.dist_name)
103+
if config.dist_name is not None:
104+
pretended = os.environ.get(
105+
PRETEND_KEY_NAMED.format(name=config.dist_name.upper())
106+
)
107+
else:
108+
pretended = None
109+
110+
if pretended is None:
111+
pretended = os.environ.get(PRETEND_KEY)
112+
101113
if pretended:
102114
# we use meta here since the pretended version
103115
# must adhere to the pep to begin with
@@ -144,6 +156,7 @@ def get_version(
144156
fallback_root=".",
145157
parse=None,
146158
git_describe_command=None,
159+
dist_name=None,
147160
):
148161
"""
149162
If supplied, relative_to should be a file from which root may

src/setuptools_scm/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def __init__(
5454
fallback_root=".",
5555
parse=None,
5656
git_describe_command=None,
57+
dist_name=None,
5758
):
5859
# TODO:
5960
self._relative_to = relative_to
@@ -70,6 +71,7 @@ def __init__(
7071
self.parse = parse
7172
self.tag_regex = tag_regex
7273
self.git_describe_command = git_describe_command
74+
self.dist_name = dist_name
7375

7476
@property
7577
def fallback_root(self):

src/setuptools_scm/integration.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from pkg_resources import iter_entry_points
22

33
from .version import _warn_if_setuptools_outdated
4-
from .utils import do, trace_exception
4+
from .utils import do, trace_exception, trace
55
from . import _get_version, Configuration
66

77

@@ -13,7 +13,12 @@ def version_keyword(dist, keyword, value):
1313
value = {}
1414
if getattr(value, "__call__", None):
1515
value = value()
16-
config = Configuration(**value)
16+
assert (
17+
"dist_name" not in value
18+
), "dist_name may not be specified in the setup keyword "
19+
trace("dist name", dist, dist.name)
20+
dist_name = dist.name if dist.name != 0 else None
21+
config = Configuration(dist_name=dist_name, **value)
1722
dist.metadata.version = _get_version(config)
1823

1924

@@ -32,7 +37,7 @@ def find_files(path=""):
3237

3338
def _args_from_toml(name="pyproject.toml"):
3439
# todo: more sensible config initialization
35-
# move this elper back to config and unify it with the code from get_config
40+
# move this helper back to config and unify it with the code from get_config
3641

3742
with open(name) as strm:
3843
defn = __import__("toml").load(strm)

testing/test_integration.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44

55
from setuptools_scm.utils import do
6+
from setuptools_scm import PRETEND_KEY, PRETEND_KEY_NAMED
67

78

89
@pytest.fixture
@@ -40,3 +41,23 @@ def test_pyproject_support_with_git(tmpdir, monkeypatch, wd):
4041
pkg.join("setup.py").write("__import__('setuptools').setup()")
4142
res = do((sys.executable, "setup.py", "--version"), pkg)
4243
assert res == "0.1.dev0"
44+
45+
46+
def test_pretend_version(tmpdir, monkeypatch, wd):
47+
monkeypatch.setenv(PRETEND_KEY, "1.0.0")
48+
49+
assert wd.get_version() == "1.0.0"
50+
assert wd.get_version(dist_name="ignored") == "1.0.0"
51+
52+
53+
def test_pretend_version_named(tmpdir, monkeypatch, wd):
54+
monkeypatch.setenv(PRETEND_KEY_NAMED.format(name="test".upper()), "1.0.0")
55+
monkeypatch.setenv(PRETEND_KEY_NAMED.format(name="test2".upper()), "2.0.0")
56+
assert wd.get_version(dist_name="test") == "1.0.0"
57+
assert wd.get_version(dist_name="test2") == "2.0.0"
58+
59+
60+
def test_pretend_version_name_takes_precedence(tmpdir, monkeypatch, wd):
61+
monkeypatch.setenv(PRETEND_KEY_NAMED.format(name="test".upper()), "1.0.0")
62+
monkeypatch.setenv(PRETEND_KEY, "2.0.0")
63+
assert wd.get_version(dist_name="test") == "1.0.0"

0 commit comments

Comments
 (0)