A spec-compliant .gitignore
parser for Python.
pip3 install gitignorefile
Parses single .gitignore
file. Suppose /home/michael/project/.gitignore
contains the following:
__pycache__/
*.py[cod]
Then:
import gitignorefile
matches = gitignorefile.parse("/home/michael/project/.gitignore")
matches("/home/michael/project/main.py") # False
matches("/home/michael/project/main.pyc") # True
matches("/home/michael/project/dir/main.pyc") # True
matches("/home/michael/project/__pycache__") # True
shutil.copytree()
ignore function which checks if file is ignored by any .gitignore
in the directory tree.
Example:
import shutil
import gitignorefile
shutil.copytree("/source", "/destination", ignore=gitignorefile.ignore())
Checks if file is ignored by any .gitignore
in the directory tree.
import gitignorefile
gitignorefile.ignored("/home/michael/project/main.py") # False
Caches .gitignore
rules discovered in the directory tree.
import gitignorefile
matches = gitignorefile.Cache()
matches("/home/michael/project/main.py") # False
matches("/home/michael/project/main.pyc") # True
matches("/home/michael/project/dir/main.pyc") # True
matches("/home/michael/project/__pycache__") # True
You could override files, that will be used to fetch ignore rules. Default value is [".gitignore", ".git/info/exclude"]
.
import gitignorefile
matches = gitignorefile.Cache(ignore_names=[".mylovelytoolignore"])
matches("/home/michael/project/main.py") # False
matches("/home/michael/project/main.pyc") # True
matches("/home/michael/project/dir/main.pyc") # True
matches("/home/michael/project/__pycache__") # True
- https://github.com/snark/ignorance by Steve Cook
- https://github.com/mherrmann/gitignore_parser by Michael Herrmann
- https://github.com/bitranox/igittigitt by Robert Nowotny
- https://github.com/cpburnz/python-path-specification by Caleb Burns