Skip to content

Commit 819fbad

Browse files
committed
Make find_files behavior opt-in.
Changes it so find_files is only enabled with config parameter `enable_find_files` is set to true, otherwise will always return empty list.
1 parent ebbc395 commit 819fbad

File tree

5 files changed

+63
-13
lines changed

5 files changed

+63
-13
lines changed

README.rst

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ or in a SCM managed file.
77

88
Additionally ``setuptools_scm`` provides setuptools with a list of
99
files that are managed by the SCM (i.e. it automatically adds all of
10-
the SCM-managed files to the sdist). Unwanted files must be excluded
11-
by discarding them via ``MANIFEST.in``.
10+
the SCM-managed files to the sdist). This is opt-in, by supplying
11+
config argument: ``enable_find_files``. Once opted in unwanted files
12+
must be excluded by discarding them via ``MANIFEST.in``.
1213

1314
``setuptools_scm`` supports the following scm out of the box:
1415

@@ -77,6 +78,7 @@ to be supplied to ``get_version()``. For example:
7778
# pyproject.toml
7879
[tool.setuptools_scm]
7980
write_to = "pkg/_version.py"
81+
enable_find_files = true
8082
8183
Where ``pkg`` is the name of your package.
8284

src/setuptools_scm/_config.py

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class Configuration:
9090
dist_name: str | None = None
9191
version_cls: type[_VersionT] = _Version
9292
search_parent_directories: bool = False
93+
enable_find_files: bool = False
9394

9495
parent: _t.PathT | None = None
9596

src/setuptools_scm/_file_finders/__init__.py

+29-9
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
import itertools
44
import os
5-
from typing import Callable
5+
from typing import Callable, Optional
66
from typing import TYPE_CHECKING
77

8+
from .. import _config
89
from .. import _log
910
from .. import _types as _t
1011
from .._entrypoints import iter_entry_points
12+
from .._integration.setuptools import read_dist_name_from_setup_cfg
1113

1214
if TYPE_CHECKING:
1315
from typing_extensions import TypeGuard
@@ -94,13 +96,31 @@ def is_toplevel_acceptable(toplevel: str | None) -> TypeGuard[str]:
9496
return toplevel not in ignored
9597

9698

99+
def _get_config() -> Optional[_config.Configuration]:
100+
dist_name: Optional[str] = None
101+
config: Optional[_config.Configuration] = None
102+
if dist_name is None:
103+
dist_name = read_dist_name_from_setup_cfg()
104+
if not os.path.isfile("pyproject.toml"):
105+
return None
106+
if dist_name == "setuptools_scm":
107+
return None
108+
try:
109+
config = _config.Configuration.from_file(dist_name=dist_name)
110+
except LookupError as e:
111+
log.exception(e)
112+
return config
113+
114+
97115
def find_files(path: _t.PathT = "") -> list[str]:
98-
for ep in itertools.chain(
99-
iter_entry_points("setuptools_scm.files_command"),
100-
iter_entry_points("setuptools_scm.files_command_fallback"),
101-
):
102-
command: Callable[[_t.PathT], list[str]] = ep.load()
103-
res: list[str] = command(path)
104-
if res:
105-
return res
116+
config = _get_config()
117+
if config is not None and config.enable_find_files:
118+
for ep in itertools.chain(
119+
iter_entry_points("setuptools_scm.files_command"),
120+
iter_entry_points("setuptools_scm.files_command_fallback"),
121+
):
122+
command: Callable[[_t.PathT], list[str]] = ep.load()
123+
res: list[str] = command(path)
124+
if res:
125+
return res
106126
return []

testing/test_file_finder.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import os
44
import sys
5+
from pathlib import Path
56
from typing import Generator
67
from typing import Iterable
78

@@ -11,6 +12,11 @@
1112
from setuptools_scm._file_finders import find_files
1213

1314

15+
def _write_pyproject_config(directory: Path, enable_find_files: bool) -> None:
16+
with open(directory / "pyproject.toml", "wt") as fh:
17+
fh.write(f"[project]\nname = \"test\"\n[tool.setuptools_scm]\nenable_find_files = {str(enable_find_files).lower()}\n")
18+
19+
1420
@pytest.fixture(params=["git", "hg"])
1521
def inwd(
1622
request: pytest.FixtureRequest, wd: WorkDir, monkeypatch: pytest.MonkeyPatch
@@ -42,6 +48,7 @@ def inwd(
4248
if request.node.get_closest_marker("skip_commit") is None:
4349
wd.add_and_commit()
4450
monkeypatch.chdir(wd.cwd)
51+
_write_pyproject_config(wd.cwd, True)
4552
yield wd
4653

4754

@@ -55,6 +62,13 @@ def test_basic(inwd: WorkDir) -> None:
5562
assert set(find_files("adir")) == _sep({"adir/filea"})
5663

5764

65+
def test_basic_find_files_disabled(inwd: WorkDir) -> None:
66+
_write_pyproject_config(inwd.cwd, False)
67+
assert find_files() == []
68+
assert find_files(".") == []
69+
assert find_files("adir") == []
70+
71+
5872
def test_whitespace(inwd: WorkDir) -> None:
5973
(inwd.cwd / "adir" / "space file").touch()
6074
inwd.add_and_commit()
@@ -66,7 +80,7 @@ def test_case(inwd: WorkDir) -> None:
6680
(inwd.cwd / "file2").touch()
6781
inwd.add_and_commit()
6882
assert set(find_files()) == _sep(
69-
{"CamelFile", "file2", "file1", "adir/filea", "bdir/fileb"}
83+
{"CamelFile", "file2", "file1", "adir/filea", "bdir/fileb", "pyproject.toml"}
7084
)
7185

7286

@@ -174,6 +188,7 @@ def test_double_include_through_symlink(inwd: WorkDir) -> None:
174188
"adir/filea",
175189
"bdir/fileb",
176190
"data/datafile",
191+
"pyproject.toml"
177192
}
178193
)
179194

@@ -193,6 +208,7 @@ def test_symlink_not_in_scm_while_target_is(inwd: WorkDir) -> None:
193208
# because the symlink_to themselves are not in scm
194209
"bdir/fileb",
195210
"data/datafile",
211+
"pyproject.toml"
196212
}
197213
)
198214

@@ -207,6 +223,7 @@ def test_unexpanded_git_archival(wd: WorkDir, monkeypatch: pytest.MonkeyPatch) -
207223
# When substitutions in `.git_archival.txt` are not expanded, files should
208224
# not be automatically listed.
209225
monkeypatch.chdir(wd.cwd)
226+
_write_pyproject_config(wd.cwd, True)
210227
(wd.cwd / ".git_archival.txt").write_text("node: $Format:%H$", encoding="utf-8")
211228
(wd.cwd / "file1.txt").touch()
212229
assert find_files() == []
@@ -219,6 +236,7 @@ def test_archive(
219236
# When substitutions in `.git_archival.txt` are not expanded, files should
220237
# not be automatically listed.
221238
monkeypatch.chdir(wd.cwd)
239+
_write_pyproject_config(wd.cwd, True)
222240
sha = "a1bda3d984d1a40d7b00ae1d0869354d6d503001"
223241
(wd.cwd / archive_file).write_text(f"node: {sha}", encoding="utf-8")
224242
(wd.cwd / "data").mkdir()
@@ -230,4 +248,4 @@ def test_archive(
230248
else:
231249
os.link("data/datafile", datalink)
232250

233-
assert set(find_files()) == _sep({archive_file, "data/datafile", "data/datalink"})
251+
assert set(find_files()) == _sep({archive_file, "data/datafile", "data/datalink", "pyproject.toml"})

testing/test_git.py

+9
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
)
3434

3535

36+
def _write_pyproject_config(directory: Path, enable_find_files: bool) -> None:
37+
with open(directory / "pyproject.toml", "wt") as fh:
38+
fh.write(f"[project]\nname = \"test\"\n[tool.setuptools_scm]\nenable_find_files = {str(enable_find_files).lower()}\n")
39+
40+
3641
@pytest.fixture(name="wd")
3742
def wd(wd: WorkDir, monkeypatch: pytest.MonkeyPatch, debug_mode: DebugMode) -> WorkDir:
3843
debug_mode.disable()
@@ -98,6 +103,7 @@ def test_git_gone(wd: WorkDir, monkeypatch: pytest.MonkeyPatch) -> None:
98103
@pytest.mark.issue("https://github.com/pypa/setuptools_scm/issues/298")
99104
@pytest.mark.issue(403)
100105
def test_file_finder_no_history(wd: WorkDir, caplog: pytest.LogCaptureFixture) -> None:
106+
_write_pyproject_config(wd.cwd, True)
101107
file_list = git_find_files(str(wd.cwd))
102108
assert file_list == []
103109

@@ -359,6 +365,7 @@ def test_git_archive_export_ignore(
359365
wd("git add test1.txt test2.txt")
360366
wd.commit()
361367
monkeypatch.chdir(wd.cwd)
368+
_write_pyproject_config(wd.cwd, True)
362369
assert setuptools_scm._file_finders.find_files(".") == [opj(".", "test1.txt")]
363370

364371

@@ -369,6 +376,7 @@ def test_git_archive_subdirectory(wd: WorkDir, monkeypatch: pytest.MonkeyPatch)
369376
wd("git add foobar")
370377
wd.commit()
371378
monkeypatch.chdir(wd.cwd)
379+
_write_pyproject_config(wd.cwd, True)
372380
assert setuptools_scm._file_finders.find_files(".") == [
373381
opj(".", "foobar", "test1.txt")
374382
]
@@ -383,6 +391,7 @@ def test_git_archive_run_from_subdirectory(
383391
wd("git add foobar")
384392
wd.commit()
385393
monkeypatch.chdir(wd.cwd / "foobar")
394+
_write_pyproject_config(wd.cwd / "foobar", True)
386395
assert setuptools_scm._file_finders.find_files(".") == [opj(".", "test1.txt")]
387396

388397

0 commit comments

Comments
 (0)