-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: read build-backend ignore lists
Signed-off-by: Henry Schreiner <[email protected]>
- Loading branch information
Showing
5 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from __future__ import annotations | ||
|
||
from pathlib import Path | ||
from typing import Any | ||
|
||
import pathspec | ||
|
||
|
||
def backend_ignored_patterns( | ||
backend: str, pyproject: dict[str, Any], files: frozenset[str] | ||
) -> frozenset[str]: | ||
""" | ||
Return the ignored patterns for the given backend. If the generator is | ||
"none", then no patterns are ignored. If the generator is "auto", then the | ||
value is read from the pyproject.toml file. Any recognized backend can also | ||
be specified directly. | ||
""" | ||
|
||
if backend == "none": | ||
return files | ||
|
||
backend_resolved = ( | ||
pyproject.get("build-system", {}).get( | ||
"build-backend", "setuptools.build_meta.__legacy__" | ||
) | ||
if backend == "auto" | ||
else backend | ||
) | ||
|
||
if backend_resolved == "flit_core.buildapi": | ||
flit_core_exclude = ( | ||
pyproject.get("tool", {}) | ||
.get("flit", {}) | ||
.get("sdist", {}) | ||
.get("exclude", []) | ||
) | ||
for pattern in flit_core_exclude: | ||
results = {str(p) for p in Path().glob(pattern)} | ||
files = files - results | ||
return files | ||
if backend_resolved == "hatchling.build": | ||
hatch_exclude = ( | ||
pyproject.get("tool", {}) | ||
.get("hatch", {}) | ||
.get("build", {}) | ||
.get("targets", {}) | ||
.get("sdist", {}) | ||
.get("exclude", []) | ||
) | ||
sdist_spec = pathspec.GitIgnoreSpec.from_lines(hatch_exclude) | ||
return frozenset(p for p in files if not sdist_spec.match_file(p)) | ||
if backend_resolved == "scikit_build_core.build": | ||
hatch_exclude = ( | ||
pyproject.get("tool", {}) | ||
.get("scikit-build", {}) | ||
.get("sdist", {}) | ||
.get("exclude", []) | ||
) | ||
sdist_spec = pathspec.GitIgnoreSpec.from_lines(hatch_exclude) | ||
return frozenset(p for p in files if not sdist_spec.match_file(p)) | ||
if backend != "auto": | ||
msg = f"Unknown backend: {backend} - please add support in check_dist.backends" | ||
raise ValueError(msg) | ||
return files |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
from __future__ import annotations | ||
|
||
import inspect | ||
import os | ||
import subprocess | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
from check_sdist.__main__ import compare | ||
from check_sdist.inject import inject_junk_files | ||
|
||
|
@@ -27,3 +31,92 @@ def test_self_dir_injected(): | |
assert compare(DIR.parent, isolated=True) == 0 | ||
end = get_all_files(DIR.parent) | ||
assert start == end | ||
|
||
|
||
@pytest.fixture | ||
def git_dir(tmp_path: Path, monkeypatch: pytest.MonkeyPatch): | ||
monkeypatch.chdir(tmp_path) | ||
subprocess.run(["git", "init"], check=True) | ||
subprocess.run(["git", "config", "user.email", "[email protected]"], check=True) | ||
subprocess.run(["git", "config", "user.name", "CI"], check=True) | ||
return tmp_path | ||
|
||
|
||
@pytest.mark.usefixtures("git_dir") | ||
@pytest.mark.parametrize("backend", ["auto", "none"]) | ||
def test_hatchling(backend: str): | ||
Path("pyproject.toml").write_text( | ||
inspect.cleandoc(f""" | ||
[build-system] | ||
requires = ["hatchling"] | ||
build-backend = "hatchling.build" | ||
[project] | ||
name = "hatchling-test" | ||
version = "0.1.0" | ||
[tool.hatch] | ||
build.targets.sdist.exclude = ["ignore*", "some-file", "**/notme.txt"] | ||
[tool.check-sdist] | ||
build-backend = "{backend}" | ||
""") | ||
) | ||
Path(".gitignore").write_text( | ||
inspect.cleandoc(""" | ||
some-ignored-file.txt | ||
""") | ||
) | ||
Path("ignore-me.txt").touch() | ||
Path("not-ignored.txt").touch() | ||
Path("some-file").touch() | ||
Path("some-dir").mkdir() | ||
Path("some-dir/notme.txt").touch() | ||
subprocess.run(["git", "add", "."], check=True) | ||
subprocess.run(["git", "commit", "-m", "Initial commit"], check=True) | ||
Path("some-ignored-file.txt").touch() | ||
assert compare(Path(), isolated=True, verbose=True) == ( | ||
0 if backend == "auto" else 2 | ||
) | ||
|
||
|
||
@pytest.mark.usefixtures("git_dir") | ||
@pytest.mark.parametrize("backend", ["auto", "none"]) | ||
def test_flit_core(backend: str): | ||
Path("pyproject.toml").write_text( | ||
inspect.cleandoc(f""" | ||
[build-system] | ||
requires = ["flit-core"] | ||
build-backend = "flit_core.buildapi" | ||
[project] | ||
name = "flit-core-test" | ||
version = "0.1.0" | ||
description = "A test package" | ||
[tool.flit.sdist] | ||
include = ["not-ignored.txt", ".gitignore"] | ||
exclude = ["ignore*", "some-file", "**/notme.txt"] | ||
[tool.check-sdist] | ||
build-backend = "{backend}" | ||
""") | ||
) | ||
Path(".gitignore").write_text( | ||
inspect.cleandoc(""" | ||
some-ignored-file.txt | ||
""") | ||
) | ||
Path("ignore-me.txt").touch() | ||
Path("not-ignored.txt").touch() | ||
Path("some-file").touch() | ||
Path("some-dir").mkdir() | ||
Path("some-dir/notme.txt").touch() | ||
Path("flit_core_test").mkdir() | ||
Path("flit_core_test/__init__.py").touch() | ||
subprocess.run(["git", "add", "."], check=True) | ||
subprocess.run(["git", "commit", "-m", "Initial commit"], check=True) | ||
Path("some-ignored-file.txt").touch() | ||
assert compare(Path(), isolated=True, verbose=True) == ( | ||
0 if backend == "auto" else 2 | ||
) |