Skip to content

Commit 04bde2d

Browse files
committed
Allow setting a fallback version in case everything else fails.
1 parent 3892d5f commit 04bde2d

File tree

7 files changed

+40
-6
lines changed

7 files changed

+40
-6
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
next
2+
====
3+
4+
* fix #198 by adding the ``fallback_version`` option, which sets the version to be used when everything else fails.
5+
16
v3.2.0
27
======
38

README.rst

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,6 @@ The currently supported configuration keys are:
253253
repository to point ``setuptools_scm`` at the root of the repository by
254254
supplying ``__file__``.
255255

256-
:parse:
257-
A function that will be used instead of the discovered SCM for parsing the
258-
version.
259-
Use with caution, this is a function for advanced use, and you should be
260-
familiar with the ``setuptools_scm`` internals to use it.
261-
262256
:tag_regex:
263257
A Python regex string to extract the version part from any SCM tag.
264258
The regex needs to contain three named groups prefix, version and suffix,
@@ -267,6 +261,18 @@ The currently supported configuration keys are:
267261
Defaults to the value of ``setuptools_scm.config.DEFAULT_TAG_REGEX``
268262
(see `config.py <src/setuptools_scm/config.py>`_).
269263

264+
:fallback_version:
265+
A version string that will be used if no other method for detecting the
266+
version worked (e.g., when using a tarball with no metadata). If this is
267+
unset (the default), setuptools_scm will error if it fails to detect the
268+
version.
269+
270+
:parse:
271+
A function that will be used instead of the discovered SCM for parsing the
272+
version.
273+
Use with caution, this is a function for advanced use, and you should be
274+
familiar with the ``setuptools_scm`` internals to use it.
275+
270276
:git_describe_command:
271277
This command will be used instead the default ``git describe`` command.
272278
Use with caution, this is a function for advanced use, and you should be

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def parse(root):
7676
.hg_archival.txt = setuptools_scm.hg:parse_archival
7777
PKG-INFO = setuptools_scm.hacks:parse_pkginfo
7878
pip-egg-info = setuptools_scm.hacks:parse_pip_egg_info
79+
setup.py = setuptools_scm.hacks:fallback_version
7980
8081
[setuptools_scm.files_command]
8182
.hg = setuptools_scm.file_finder_hg:hg_find_files

src/setuptools_scm/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ def get_version(
120120
write_to_template=None,
121121
relative_to=None,
122122
tag_regex=None,
123+
fallback_version=None,
123124
parse=None,
124125
git_describe_command=None,
125126
):
@@ -138,6 +139,7 @@ def get_version(
138139
config.write_to_template = write_to_template
139140
config.relative_to = relative_to
140141
config.tag_regex = tag_regex
142+
config.fallback_version = fallback_version
141143
config.parse = parse
142144
config.git_describe_command = git_describe_command
143145

src/setuptools_scm/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class Configuration(object):
4444
local_scheme = None
4545
write_to = None
4646
write_to_template = None
47+
fallback_version = None
4748
_relative_to = None
4849
parse = None
4950
_tag_regex = None
@@ -59,6 +60,7 @@ def __init__(self, relative_to=None, root="."):
5960
self.local_scheme = "node-and-date"
6061
self.write_to = ""
6162
self.write_to_template = None
63+
self.fallback_version = None
6264
self.parse = None
6365
self.tag_regex = DEFAULT_TAG_REGEX
6466
self.git_describe_command = None

src/setuptools_scm/hacks.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,8 @@ def parse_pip_egg_info(root, config=None):
2222
if not items:
2323
return
2424
return parse_pkginfo(os.path.join(pipdir, items[0]), config=config)
25+
26+
27+
def fallback_version(root, config=None):
28+
if config.fallback_version is not None:
29+
return meta(config.fallback_version, preformatted=True, config=config)

testing/test_basic_api.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import sys
23
import py
34
import pytest
45

@@ -56,6 +57,18 @@ def test_root_parameter_pass_by(monkeypatch, tmpdir):
5657
setuptools_scm.get_version(root=tmpdir.strpath)
5758

5859

60+
def test_fallback(tmpdir, monkeypatch):
61+
monkeypatch.delenv("SETUPTOOLS_SCM_DEBUG")
62+
p = tmpdir.ensure("sub/package", dir=1)
63+
p.join("setup.py").write(
64+
"""from setuptools import setup
65+
setup(use_scm_version={"fallback_version": "12.34"})
66+
"""
67+
)
68+
res = do((sys.executable, "setup.py", "--version"), p)
69+
assert res == "12.34"
70+
71+
5972
@pytest.mark.parametrize(
6073
"version", ["1.0", "1.2.3.dev1+ge871260", "1.2.3.dev15+ge871260.d20180625", "2345"]
6174
)

0 commit comments

Comments
 (0)