Skip to content

Commit 22dc0c5

Browse files
authored
fix: building from sdist fails (#136)
* Add test_source_distribution * Fix versioneer import for sdist build in an isolated pip build * Fix version returned by versioneer when "post" is already in an existing tag
1 parent 9ae3719 commit 22dc0c5

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

setup.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
#!/usr/bin/env python
22

3+
import os
34
import sys
4-
import versioneer
55

66
from distutils.text_file import TextFile
77
from skbuild import setup
88

9+
# Add current folder to path
10+
# This is required to import versioneer in an isolated pip build
11+
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
12+
13+
import versioneer # noqa: E402
14+
915

1016
with open('README.rst', 'r') as fp:
1117
readme = fp.read()

tests/test_distribution.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44
import textwrap
55

6-
from path import Path
6+
from path import Path, matchers
77

88
DIST_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '../dist'))
99

@@ -26,6 +26,21 @@ def _check_cmake_install(virtualenv, tmpdir):
2626
assert output[:len(expected)].lower() == expected.lower()
2727

2828

29+
@pytest.mark.skipif(not Path(DIST_DIR).exists(), reason="dist directory does not exist")
30+
def test_source_distribution(virtualenv, tmpdir):
31+
sdists = Path(DIST_DIR).files(match=matchers.CaseInsensitive("*.tar.gz"))
32+
if not sdists:
33+
pytest.skip("no source distribution available")
34+
assert len(sdists) == 1
35+
36+
if "SETUP_CMAKE_ARGS" in os.environ:
37+
virtualenv.env["SKBUILD_CONFIGURE_OPTIONS"] = os.environ["SETUP_CMAKE_ARGS"]
38+
virtualenv.run("pip install %s" % sdists[0])
39+
assert "cmake" in virtualenv.installed_packages()
40+
41+
_check_cmake_install(virtualenv, tmpdir)
42+
43+
2944
@pytest.mark.skipif(not Path(DIST_DIR).exists(), reason="dist directory does not exist")
3045
def test_wheel(virtualenv, tmpdir):
3146
wheels = Path(DIST_DIR).files(match="*.whl")

versioneer.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1283,7 +1283,22 @@ def render_pep440_post(pieces):
12831283
if pieces["closest-tag"]:
12841284
rendered = pieces["closest-tag"]
12851285
if pieces["distance"] or pieces["dirty"]:
1286-
rendered += ".post%d" % pieces["distance"]
1286+
if ".post" in rendered:
1287+
# update the existing post tag
1288+
start = rendered.index(".post") + 5
1289+
if len(rendered) == start:
1290+
rendered += "%d" % pieces["distance"]
1291+
else:
1292+
end = start + 1
1293+
while end <= len(rendered) and rendered[start:end].isdigit():
1294+
end += 1
1295+
end -= 1
1296+
distance = pieces["distance"]
1297+
if start != end:
1298+
distance += int(rendered[start:end])
1299+
rendered = rendered[:start] + "%d" % distance + rendered[end:]
1300+
else:
1301+
rendered += ".post%d" % pieces["distance"]
12871302
if pieces["dirty"]:
12881303
rendered += ".dev0"
12891304
rendered += plus_or_dot(pieces)

0 commit comments

Comments
 (0)