Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow opt-out of find_files behavior. (issue 561) #851

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,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``.
the SCM-managed files to the sdist). This is opt-in, by supplying
config argument: ``enable_find_files``. Once opted in unwanted files
must be excluded by discarding them via ``MANIFEST.in``.

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

Expand Down Expand Up @@ -77,6 +78,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.

Expand Down
1 change: 1 addition & 0 deletions src/setuptools_scm/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = False

parent: _t.PathT | None = None

Expand Down
36 changes: 28 additions & 8 deletions src/setuptools_scm/_file_finders/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 []
10 changes: 10 additions & 0 deletions testing/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 17 additions & 2 deletions testing/test_file_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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


Expand All @@ -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()
Expand All @@ -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"}
)


Expand Down Expand Up @@ -174,6 +183,7 @@ def test_double_include_through_symlink(inwd: WorkDir) -> None:
"adir/filea",
"bdir/fileb",
"data/datafile",
"pyproject.toml",
}
)

Expand All @@ -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",
}
)

Expand All @@ -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() == []
Expand All @@ -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()
Expand All @@ -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"}
)
5 changes: 5 additions & 0 deletions testing/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 == []

Expand Down Expand Up @@ -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")]


Expand All @@ -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")
]
Expand All @@ -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")]


Expand Down
2 changes: 2 additions & 0 deletions testing/test_mercurial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"]


Expand Down