Skip to content

Commit 17286cf

Browse files
Merge pull request #607 from RonnyPfannschmidt/fix-regressions
fix regressions
2 parents 74c7466 + c3eb223 commit 17286cf

File tree

7 files changed

+118
-42
lines changed

7 files changed

+118
-42
lines changed

.github/workflows/python-tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ jobs:
5555
fail-fast: false
5656
matrix:
5757
python_version: [ '3.6', '3.9', 'pypy3' ]
58-
installer: ["pip install", easy_install]
58+
installer: ["pip install"]
5959
name: check self install - Python ${{ matrix.python_version }} via ${{ matrix.installer }}
6060
steps:
6161
- uses: actions/checkout@v1
@@ -67,7 +67,7 @@ jobs:
6767
# self install testing needs some clarity
6868
# so its being executed without any other tools running
6969
# setuptools smaller 52 is needed too di easy_install
70-
- run: pip install -U "setuptools<52"
70+
- run: pip install -U "setuptools<52" tomli
7171
- run: python setup.py egg_info
7272
- run: python setup.py sdist
7373
- run: ${{ matrix.installer }} dist/*

CHANGELOG.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
v6.1.1
2+
=======
3+
4+
* fix #605: completely disallow bdist_egg - modern enough setuptools>=45 uses pip
5+
* fix #606: re-integrate and harden toml parsing
6+
* fix #597: harden and expand support for figuring the current distribution name from
7+
`pyproject.toml` (`project.name` or `tool.setuptools_scm.dist_name`) section or `setup.cfg` (`metadata.name`)
8+
19
v6.1.0
210
======
311

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[build-system]
2-
requires = ["setuptools>=45", "wheel"]
2+
requires = ["setuptools>=45", "wheel", "tomli"]
33
build-backend = "setuptools.build_meta"

setup.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,8 @@ def scm_config():
2828
has_entrypoints = os.path.isdir(egg_info)
2929
import pkg_resources
3030

31-
pkg_resources.require("setuptools>=45")
32-
3331
sys.path.insert(0, src)
3432
pkg_resources.working_set.add_entry(src)
35-
# FIXME: remove debug
36-
print(src)
37-
print(pkg_resources.working_set)
3833

3934
from setuptools_scm.hacks import parse_pkginfo
4035
from setuptools_scm.git import parse as parse_git
@@ -50,13 +45,22 @@ def parse(root):
5045
version_scheme=guess_next_dev_version, local_scheme=get_local_node_and_date
5146
)
5247

48+
from setuptools.command.bdist_egg import bdist_egg as original_bdist_egg
49+
50+
class bdist_egg(original_bdist_egg):
51+
def run(self):
52+
raise SystemExit("bdist_egg is forbidden, please update to setuptools>=45")
53+
5354
if has_entrypoints:
54-
return dict(use_scm_version=config)
55+
return dict(use_scm_version=config, cmdclass={"bdist_egg": bdist_egg})
5556
else:
5657
from setuptools_scm import get_version
5758

58-
return dict(version=get_version(root=here, parse=parse, **config))
59+
return dict(
60+
version=get_version(root=here, parse=parse, **config),
61+
cmdclass={"bdist_egg": bdist_egg},
62+
)
5963

6064

6165
if __name__ == "__main__":
62-
setuptools.setup(**scm_config())
66+
setuptools.setup(setup_requires=["setuptools>=45", "tomli"], **scm_config())

src/setuptools_scm/config.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ def _check_absolute_root(root, relative_to):
5555
return os.path.abspath(root)
5656

5757

58+
def _lazy_tomli_load(data):
59+
from tomli import loads
60+
61+
return loads(data)
62+
63+
5864
class Configuration:
5965
"""Global configuration model"""
6066

@@ -163,16 +169,46 @@ def tag_regex(self, value):
163169
self._tag_regex = _check_tag_regex(value)
164170

165171
@classmethod
166-
def from_file(cls, name="pyproject.toml", dist_name=None):
172+
def from_file(
173+
cls,
174+
name: str = "pyproject.toml",
175+
dist_name=None, # type: str | None
176+
_load_toml=_lazy_tomli_load,
177+
):
167178
"""
168179
Read Configuration from pyproject.toml (or similar).
169180
Raises exceptions when file is not found or toml is
170181
not installed or the file has invalid format or does
171182
not contain the [tool.setuptools_scm] section.
172183
"""
184+
173185
with open(name, encoding="UTF-8") as strm:
174-
defn = __import__("toml").load(strm)
175-
section = defn.get("tool", {})["setuptools_scm"]
186+
data = strm.read()
187+
defn = _load_toml(data)
188+
try:
189+
section = defn.get("tool", {})["setuptools_scm"]
190+
except LookupError:
191+
raise FileNotFoundError(
192+
f"{name} does not contain a tool.setuptools_scm section"
193+
) from None
194+
if "dist_name" in section:
195+
if dist_name is None:
196+
dist_name = section.pop("dist_name")
197+
else:
198+
assert dist_name == section["dist_name"]
199+
del section["dist_name"]
200+
if dist_name is None:
201+
if "project" in defn:
202+
# minimal pep 621 support for figuring the pretend keys
203+
dist_name = defn["project"].get("name")
204+
if dist_name is None:
205+
# minimal effort to read dist_name off setup.cfg metadata
206+
import configparser
207+
208+
parser = configparser.ConfigParser()
209+
parser.read(["setup.cfg"])
210+
dist_name = parser.get("metadata", "name", fallback=None)
211+
176212
return cls(dist_name=dist_name, **section)
177213

178214

src/setuptools_scm/integration.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
import warnings
2+
3+
import setuptools
4+
15
from . import _get_version
26
from . import Configuration
37
from .utils import do
48
from .utils import iter_entry_points
59
from .utils import trace
6-
from .utils import trace_exception
710

811

9-
def version_keyword(dist, keyword, value):
12+
def version_keyword(dist: setuptools.Distribution, keyword, value):
1013
if not value:
1114
return
1215
if value is True:
@@ -39,24 +42,15 @@ def find_files(path=""):
3942
return []
4043

4144

42-
def _args_from_toml(name="pyproject.toml"):
43-
# todo: more sensible config initialization
44-
# move this helper back to config and unify it with the code from get_config
45-
import tomli
46-
47-
with open(name, encoding="UTF-8") as strm:
48-
defn = tomli.load(strm)
49-
return defn.get("tool", {})["setuptools_scm"]
50-
51-
52-
def infer_version(dist):
45+
def infer_version(dist: setuptools.Distribution):
5346
trace(
5447
"finalize hook",
5548
vars(dist.metadata),
5649
)
5750
dist_name = dist.metadata.name
5851
try:
5952
config = Configuration.from_file(dist_name=dist_name)
60-
except Exception:
61-
return trace_exception()
62-
dist.metadata.version = _get_version(config)
53+
except FileNotFoundError as e:
54+
warnings.warn(str(e))
55+
else:
56+
dist.metadata.version = _get_version(config)

testing/test_integration.py

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,64 @@ def test_pyproject_support(tmpdir, monkeypatch):
4040
assert res == "12.34"
4141

4242

43-
def test_pyproject_support_with_git(tmpdir, monkeypatch, wd):
44-
pytest.importorskip("toml")
45-
pkg = tmpdir.join("wd")
46-
pkg.join("pyproject.toml").write("""[tool.setuptools_scm]""")
47-
pkg.join("setup.py").write(
48-
"__import__('setuptools').setup(name='setuptools_scm_example')"
49-
)
50-
res = do((sys.executable, "setup.py", "--version"), pkg)
43+
PYPROJECT_FILES = {
44+
"setup.py": "[tool.setuptools_scm]",
45+
"setup.cfg": "[tool.setuptools_scm]",
46+
"pyproject tool.setuptools_scm": (
47+
"[tool.setuptools_scm]\ndist_name='setuptools_scm_example'"
48+
),
49+
"pyproject.project": (
50+
"[project]\nname='setuptools_scm_example'\n[tool.setuptools_scm]"
51+
),
52+
}
53+
54+
SETUP_PY_PLAIN = "__import__('setuptools').setup()"
55+
SETUP_PY_WITH_NAME = "__import__('setuptools').setup(name='setuptools_scm_example')"
56+
57+
SETUP_PY_FILES = {
58+
"setup.py": SETUP_PY_WITH_NAME,
59+
"setup.cfg": SETUP_PY_PLAIN,
60+
"pyproject tool.setuptools_scm": SETUP_PY_PLAIN,
61+
"pyproject.project": SETUP_PY_PLAIN,
62+
}
63+
64+
SETUP_CFG_FILES = {
65+
"setup.py": "",
66+
"setup.cfg": "[metadata]\nname=setuptools_scm_example",
67+
"pyproject tool.setuptools_scm": "",
68+
"pyproject.project": "",
69+
}
70+
71+
with_metadata_in = pytest.mark.parametrize(
72+
"metadata_in",
73+
["setup.py", "setup.cfg", "pyproject tool.setuptools_scm", "pyproject.project"],
74+
)
75+
76+
77+
@with_metadata_in
78+
def test_pyproject_support_with_git(wd, metadata_in):
79+
pytest.importorskip("tomli")
80+
wd.write("pyproject.toml", PYPROJECT_FILES[metadata_in])
81+
wd.write("setup.py", SETUP_PY_FILES[metadata_in])
82+
wd.write("setup.cfg", SETUP_CFG_FILES[metadata_in])
83+
res = wd((sys.executable, "setup.py", "--version"))
5184
assert res.endswith("0.1.dev0")
5285

5386

54-
def test_pretend_version(tmpdir, monkeypatch, wd):
87+
def test_pretend_version(monkeypatch, wd):
5588
monkeypatch.setenv(PRETEND_KEY, "1.0.0")
5689

5790
assert wd.get_version() == "1.0.0"
5891
assert wd.get_version(dist_name="ignored") == "1.0.0"
5992

6093

61-
def test_pretend_version_named_pyproject_integration(tmpdir, monkeypatch, wd):
62-
test_pyproject_support_with_git(tmpdir, monkeypatch, wd)
94+
@with_metadata_in
95+
def test_pretend_version_named_pyproject_integration(monkeypatch, wd, metadata_in):
96+
test_pyproject_support_with_git(wd, metadata_in)
6397
monkeypatch.setenv(
6498
PRETEND_KEY_NAMED.format(name="setuptools_scm_example".upper()), "3.2.1"
6599
)
66-
res = do((sys.executable, "setup.py", "--version"), tmpdir / "wd")
100+
res = wd((sys.executable, "setup.py", "--version"))
67101
assert res.endswith("3.2.1")
68102

69103

0 commit comments

Comments
 (0)