-
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.
* ENH: ignore untracked files in cspell check * ENH: use `git ls-files` instead of `glob()` * FIX: wrap readthedocs pip install line
- Loading branch information
Showing
10 changed files
with
132 additions
and
33 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
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
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,80 @@ | ||
"""Functions for checking whether files exist on disk.""" | ||
|
||
from __future__ import annotations | ||
|
||
import subprocess # noqa: S404 | ||
|
||
from pathspec import PathSpec | ||
from pathspec.patterns import GitWildMatchPattern | ||
|
||
|
||
def filter_files(patterns: list[str], files: list[str] | None = None) -> list[str]: | ||
"""Filter filenames that match certain patterns. | ||
If :code:`files` is not supplied, get the files with :func:`git_ls_files`. | ||
>>> filter_files(["**/*.json", "**/*.txt"], ["a/b/file.json", "file.yaml"]) | ||
['a/b/file.json'] | ||
""" | ||
if files is None: | ||
files = git_ls_files(untracked=True) | ||
return [file for file in files if matches_patterns(file, patterns)] | ||
|
||
|
||
def filter_patterns(patterns: list[str], files: list[str] | None = None) -> list[str]: | ||
"""Filter patterns that match files. | ||
If :code:`files` is not supplied, get the files with :func:`git_ls_files`. | ||
>>> filter_patterns(["**/*.json", "**/*.txt"], ["file.json", "file.yaml"]) | ||
['**/*.json'] | ||
""" | ||
if files is None: | ||
files = git_ls_files(untracked=True) | ||
return [pattern for pattern in patterns if matches_files(pattern, files)] | ||
|
||
|
||
def git_ls_files(untracked: bool = False) -> list[str]: | ||
"""Get the tracked and untracked files, but excluding files in .gitignore.""" | ||
output = subprocess.check_output([ # noqa: S603, S607 | ||
"git", | ||
"ls-files", | ||
]).decode("utf-8") | ||
tracked_files = output.splitlines() | ||
if untracked: | ||
output = subprocess.check_output([ # noqa: S603, S607 | ||
"git", | ||
"ls-files", | ||
"--others", | ||
"--exclude-standard", | ||
]).decode("utf-8") | ||
return tracked_files + output.splitlines() | ||
return tracked_files | ||
|
||
|
||
def matches_files(pattern: str, files: list[str]) -> bool: | ||
"""Use git wild-match patterns to match a filename. | ||
>>> matches_files("**/*.json", [".cspell.json"]) | ||
True | ||
>>> matches_files("**/*.json", ["some/random/path/.cspell.json"]) | ||
True | ||
>>> matches_files("*/*.json", ["some/random/path/.cspell.json"]) | ||
False | ||
""" | ||
spec = PathSpec.from_lines(GitWildMatchPattern, [pattern]) | ||
return any(spec.match_file(file) for file in files) | ||
|
||
|
||
def matches_patterns(filename: str, patterns: list[str]) -> bool: | ||
"""Use git wild-match patterns to match a filename. | ||
>>> matches_patterns(".cspell.json", patterns=["**/*.json"]) | ||
True | ||
>>> matches_patterns("some/random/path/.cspell.json", patterns=["**/*.json"]) | ||
True | ||
>>> matches_patterns("some/random/path/.cspell.json", patterns=["*/*.json"]) | ||
False | ||
""" | ||
spec = PathSpec.from_lines(GitWildMatchPattern, patterns) | ||
return spec.match_file(filename) |
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