diff --git a/README.rst b/README.rst index a48309c1..1384b2bd 100644 --- a/README.rst +++ b/README.rst @@ -8,7 +8,9 @@ or in a SCM managed file. Additionally ``setuptools_scm`` provides setuptools with a list of files that are managed by the SCM (i.e. it automatically adds all of the SCM-managed files to the sdist). Unwanted files must be excluded -by discarding them via ``MANIFEST.in``. +by discarding them via ``MANIFEST.in``. It is also possible to +opt-out of this behavior by setting the ``enable_find_files`` config +argument to false. ``setuptools_scm`` supports the following scm out of the box: @@ -77,6 +79,7 @@ to be supplied to ``get_version()``. For example: # pyproject.toml [tool.setuptools_scm] write_to = "pkg/_version.py" + enable_find_files = true Where ``pkg`` is the name of your package. diff --git a/src/setuptools_scm/_config.py b/src/setuptools_scm/_config.py index 108c86e7..d7dbf916 100644 --- a/src/setuptools_scm/_config.py +++ b/src/setuptools_scm/_config.py @@ -90,6 +90,7 @@ class Configuration: dist_name: str | None = None version_cls: type[_VersionT] = _Version search_parent_directories: bool = False + enable_find_files: bool = True parent: _t.PathT | None = None diff --git a/src/setuptools_scm/_file_finders/__init__.py b/src/setuptools_scm/_file_finders/__init__.py index 90726c45..f8725d89 100644 --- a/src/setuptools_scm/_file_finders/__init__.py +++ b/src/setuptools_scm/_file_finders/__init__.py @@ -5,9 +5,11 @@ from typing import Callable from typing import TYPE_CHECKING +from .. import _config from .. import _log from .. import _types as _t from .._entrypoints import iter_entry_points +from .._integration.setuptools import read_dist_name_from_setup_cfg if TYPE_CHECKING: from typing_extensions import TypeGuard @@ -94,13 +96,31 @@ def is_toplevel_acceptable(toplevel: str | None) -> TypeGuard[str]: return toplevel not in ignored +def _get_config() -> _config.Configuration | None: + dist_name: str | None = None + config: _config.Configuration | None = None + if dist_name is None: + dist_name = read_dist_name_from_setup_cfg() + if not os.path.isfile("pyproject.toml"): + return None + if dist_name == "setuptools_scm": + return None + try: + config = _config.Configuration.from_file(dist_name=dist_name) + except LookupError as e: + log.exception(e) + return config + + def find_files(path: _t.PathT = "") -> list[str]: - for ep in itertools.chain( - iter_entry_points("setuptools_scm.files_command"), - iter_entry_points("setuptools_scm.files_command_fallback"), - ): - command: Callable[[_t.PathT], list[str]] = ep.load() - res: list[str] = command(path) - if res: - return res + config = _get_config() + if config is not None and config.enable_find_files: + for ep in itertools.chain( + iter_entry_points("setuptools_scm.files_command"), + iter_entry_points("setuptools_scm.files_command_fallback"), + ): + command: Callable[[_t.PathT], list[str]] = ep.load() + res: list[str] = command(path) + if res: + return res return [] diff --git a/testing/conftest.py b/testing/conftest.py index ef5883c8..2629f377 100644 --- a/testing/conftest.py +++ b/testing/conftest.py @@ -42,6 +42,16 @@ def pytest_addoption(parser: Any) -> None: ) +def write_pyproject_config(directory: Path, enable_find_files: bool) -> None: + with open(directory / "pyproject.toml", "w") as fh: + fh.write( + f"[project]\n" + f'name = "test"\n' + f"[tool.setuptools_scm]\n" + f"enable_find_files = {str(enable_find_files).lower()}\n" + ) + + class DebugMode(contextlib.AbstractContextManager): # type: ignore[type-arg] from setuptools_scm import _log as __module diff --git a/testing/test_file_finder.py b/testing/test_file_finder.py index 3825500d..d067e65c 100644 --- a/testing/test_file_finder.py +++ b/testing/test_file_finder.py @@ -7,6 +7,7 @@ import pytest +from .conftest import write_pyproject_config from .wd_wrapper import WorkDir from setuptools_scm._file_finders import find_files @@ -42,6 +43,7 @@ def inwd( if request.node.get_closest_marker("skip_commit") is None: wd.add_and_commit() monkeypatch.chdir(wd.cwd) + write_pyproject_config(wd.cwd, True) yield wd @@ -55,6 +57,13 @@ def test_basic(inwd: WorkDir) -> None: assert set(find_files("adir")) == _sep({"adir/filea"}) +def test_basic_find_files_disabled(inwd: WorkDir) -> None: + write_pyproject_config(inwd.cwd, False) + assert find_files() == [] + assert find_files(".") == [] + assert find_files("adir") == [] + + def test_whitespace(inwd: WorkDir) -> None: (inwd.cwd / "adir" / "space file").touch() inwd.add_and_commit() @@ -66,7 +75,7 @@ def test_case(inwd: WorkDir) -> None: (inwd.cwd / "file2").touch() inwd.add_and_commit() assert set(find_files()) == _sep( - {"CamelFile", "file2", "file1", "adir/filea", "bdir/fileb"} + {"CamelFile", "file2", "file1", "adir/filea", "bdir/fileb", "pyproject.toml"} ) @@ -174,6 +183,7 @@ def test_double_include_through_symlink(inwd: WorkDir) -> None: "adir/filea", "bdir/fileb", "data/datafile", + "pyproject.toml", } ) @@ -193,6 +203,7 @@ def test_symlink_not_in_scm_while_target_is(inwd: WorkDir) -> None: # because the symlink_to themselves are not in scm "bdir/fileb", "data/datafile", + "pyproject.toml", } ) @@ -207,6 +218,7 @@ def test_unexpanded_git_archival(wd: WorkDir, monkeypatch: pytest.MonkeyPatch) - # When substitutions in `.git_archival.txt` are not expanded, files should # not be automatically listed. monkeypatch.chdir(wd.cwd) + write_pyproject_config(wd.cwd, True) (wd.cwd / ".git_archival.txt").write_text("node: $Format:%H$", encoding="utf-8") (wd.cwd / "file1.txt").touch() assert find_files() == [] @@ -219,6 +231,7 @@ def test_archive( # When substitutions in `.git_archival.txt` are not expanded, files should # not be automatically listed. monkeypatch.chdir(wd.cwd) + write_pyproject_config(wd.cwd, True) sha = "a1bda3d984d1a40d7b00ae1d0869354d6d503001" (wd.cwd / archive_file).write_text(f"node: {sha}", encoding="utf-8") (wd.cwd / "data").mkdir() @@ -230,4 +243,6 @@ def test_archive( else: os.link("data/datafile", datalink) - assert set(find_files()) == _sep({archive_file, "data/datafile", "data/datalink"}) + assert set(find_files()) == _sep( + {archive_file, "data/datafile", "data/datalink", "pyproject.toml"} + ) diff --git a/testing/test_git.py b/testing/test_git.py index 9e2e9b5c..fad425ea 100644 --- a/testing/test_git.py +++ b/testing/test_git.py @@ -17,6 +17,7 @@ import setuptools_scm._file_finders from .conftest import DebugMode +from .conftest import write_pyproject_config from .wd_wrapper import WorkDir from setuptools_scm import Configuration from setuptools_scm import git @@ -98,6 +99,7 @@ def test_git_gone(wd: WorkDir, monkeypatch: pytest.MonkeyPatch) -> None: @pytest.mark.issue("https://github.com/pypa/setuptools_scm/issues/298") @pytest.mark.issue(403) def test_file_finder_no_history(wd: WorkDir, caplog: pytest.LogCaptureFixture) -> None: + write_pyproject_config(wd.cwd, True) file_list = git_find_files(str(wd.cwd)) assert file_list == [] @@ -359,6 +361,7 @@ def test_git_archive_export_ignore( wd("git add test1.txt test2.txt") wd.commit() monkeypatch.chdir(wd.cwd) + write_pyproject_config(wd.cwd, True) assert setuptools_scm._file_finders.find_files(".") == [opj(".", "test1.txt")] @@ -369,6 +372,7 @@ def test_git_archive_subdirectory(wd: WorkDir, monkeypatch: pytest.MonkeyPatch) wd("git add foobar") wd.commit() monkeypatch.chdir(wd.cwd) + write_pyproject_config(wd.cwd, True) assert setuptools_scm._file_finders.find_files(".") == [ opj(".", "foobar", "test1.txt") ] @@ -383,6 +387,7 @@ def test_git_archive_run_from_subdirectory( wd("git add foobar") wd.commit() monkeypatch.chdir(wd.cwd / "foobar") + write_pyproject_config(wd.cwd / "foobar", True) assert setuptools_scm._file_finders.find_files(".") == [opj(".", "test1.txt")] diff --git a/testing/test_mercurial.py b/testing/test_mercurial.py index 1b35d11c..7c451b30 100644 --- a/testing/test_mercurial.py +++ b/testing/test_mercurial.py @@ -6,6 +6,7 @@ import pytest import setuptools_scm._file_finders +from .conftest import write_pyproject_config from setuptools_scm import Configuration from setuptools_scm._run_cmd import has_command from setuptools_scm.hg import archival_to_version @@ -71,6 +72,7 @@ def test_find_files_stop_at_root_hg( # issue 251 wd.add_and_commit() monkeypatch.chdir(project) + write_pyproject_config(project, True) assert setuptools_scm._file_finders.find_files() == ["setup.cfg"]