Skip to content

Commit

Permalink
Make find_files behavior opt-in.
Browse files Browse the repository at this point in the history
Changes it so find_files is only enabled with config parameter
`enable_find_files` is set to true, otherwise will always return empty
list.
  • Loading branch information
daveware-nv committed May 23, 2023
1 parent 6754be9 commit 1772b25
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 13 deletions.
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
38 changes: 29 additions & 9 deletions src/setuptools_scm/_file_finders/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import itertools
import os
from typing import Callable
from typing import Callable, Optional

from typing_extensions import TypeGuard

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

log = _log.log.getChild("file_finder")

Expand Down Expand Up @@ -91,13 +93,31 @@ def is_toplevel_acceptable(toplevel: str | None) -> TypeGuard[str]:
return toplevel not in ignored


def _get_config() -> Optional[_config.Configuration]:
dist_name: Optional[str] = None
config: Optional[_config.Configuration] = 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 []
22 changes: 20 additions & 2 deletions testing/test_file_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os
import sys
from pathlib import Path
from typing import Generator
from typing import Iterable

Expand All @@ -11,6 +12,11 @@
from setuptools_scm._file_finders import find_files


def _write_pyproject_config(directory: Path, enable_find_files: bool) -> None:
with open(directory / "pyproject.toml", "wt") as fh:
fh.write(f"[project]\nname = \"test\"\n[tool.setuptools_scm]\nenable_find_files = {str(enable_find_files).lower()}\n")


@pytest.fixture(params=["git", "hg"])
def inwd(
request: pytest.FixtureRequest, wd: WorkDir, monkeypatch: pytest.MonkeyPatch
Expand Down Expand Up @@ -42,6 +48,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 +62,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 +80,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 +188,7 @@ def test_double_include_through_symlink(inwd: WorkDir) -> None:
"adir/filea",
"bdir/fileb",
"data/datafile",
"pyproject.toml"
}
)

Expand All @@ -193,6 +208,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 +223,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 +236,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 +248,4 @@ 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"})
9 changes: 9 additions & 0 deletions testing/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
)


def _write_pyproject_config(directory: Path, enable_find_files: bool) -> None:
with open(directory / "pyproject.toml", "wt") as fh:
fh.write(f"[project]\nname = \"test\"\n[tool.setuptools_scm]\nenable_find_files = {str(enable_find_files).lower()}\n")


@pytest.fixture(name="wd")
def wd(wd: WorkDir, monkeypatch: pytest.MonkeyPatch, debug_mode: DebugMode) -> WorkDir:
debug_mode.disable()
Expand Down Expand Up @@ -98,6 +103,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 +365,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 +376,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 +391,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

0 comments on commit 1772b25

Please sign in to comment.