| 1 |
|
| 2 |
PathSpec |
| 3 |
======== |
| 4 |
|
| 5 |
*pathspec* is a utility library for pattern matching of file paths. So far this |
| 6 |
only includes Git's `gitignore`_ pattern matching. |
| 7 |
|
| 8 |
.. _`gitignore`: http://git-scm.com/docs/gitignore |
| 9 |
|
| 10 |
|
| 11 |
Tutorial |
| 12 |
-------- |
| 13 |
|
| 14 |
Say you have a "Projects" directory and you want to back it up, but only |
| 15 |
certain files, and ignore others depending on certain conditions:: |
| 16 |
|
| 17 |
>>> from pathspec import PathSpec |
| 18 |
>>> # The gitignore-style patterns for files to select, but we're including |
| 19 |
>>> # instead of ignoring. |
| 20 |
>>> spec_text = """ |
| 21 |
... |
| 22 |
... # This is a comment because the line begins with a hash: "#" |
| 23 |
... |
| 24 |
... # Include several project directories (and all descendants) relative to |
| 25 |
... # the current directory. To reference only a directory you must end with a |
| 26 |
... # slash: "/" |
| 27 |
... /project-a/ |
| 28 |
... /project-b/ |
| 29 |
... /project-c/ |
| 30 |
... |
| 31 |
... # Patterns can be negated by prefixing with exclamation mark: "!" |
| 32 |
... |
| 33 |
... # Ignore temporary files beginning or ending with "~" and ending with |
| 34 |
... # ".swp". |
| 35 |
... !~* |
| 36 |
... !*~ |
| 37 |
... !*.swp |
| 38 |
... |
| 39 |
... # These are python projects so ignore compiled python files from |
| 40 |
... # testing. |
| 41 |
... !*.pyc |
| 42 |
... |
| 43 |
... # Ignore the build directories but only directly under the project |
| 44 |
... # directories. |
| 45 |
... !/*/build/ |
| 46 |
... |
| 47 |
... """ |
| 48 |
|
| 49 |
The ``PathSpec`` class provides an abstraction around pattern implementations, |
| 50 |
and we want to compile our patterns as "gitignore" patterns. You could call it a |
| 51 |
wrapper for a list of compiled patterns:: |
| 52 |
|
| 53 |
>>> spec = PathSpec.from_lines('gitignore', spec_text.splitlines()) |
| 54 |
|
| 55 |
If we wanted to manually compile the patterns, we can use the ``GitIgnoreBasicPattern`` |
| 56 |
class directly. It is used in the background for "gitignore" which internally |
| 57 |
converts patterns to regular expressions:: |
| 58 |
|
| 59 |
>>> from pathspec.patterns.gitignore.basic import GitIgnoreBasicPattern |
| 60 |
>>> patterns = map(GitIgnoreBasicPattern, spec_text.splitlines()) |
| 61 |
>>> spec = PathSpec(patterns) |
| 62 |
|
| 63 |
``PathSpec.from_lines()`` is a class method which simplifies that. |
| 64 |
|
| 65 |
If you want to load the patterns from file, you can pass the file object |
| 66 |
directly as well:: |
| 67 |
|
| 68 |
>>> with open('patterns.list', 'r') as fh: |
| 69 |
>>> spec = PathSpec.from_lines('gitignore', fh) |
| 70 |
|
| 71 |
You can perform matching on a whole directory tree with:: |
| 72 |
|
| 73 |
>>> matches = set(spec.match_tree_files('path/to/directory')) |
| 74 |
|
| 75 |
Or you can perform matching on a specific set of file paths with:: |
| 76 |
|
| 77 |
>>> matches = set(spec.match_files(file_paths)) |
| 78 |
|
| 79 |
Or check to see if an individual file matches:: |
| 80 |
|
| 81 |
>>> is_matched = spec.match_file(file_path) |
| 82 |
|
| 83 |
There's actually two implementations of "gitignore". The basic implementation is |
| 84 |
used by ``PathSpec`` and follows patterns as documented by `gitignore`_. |
| 85 |
However, Git's behavior differs from the documented patterns. There's some |
| 86 |
edge-cases, and in particular, Git allows including files from excluded |
| 87 |
directories which appears to contradict the documentation. ``GitIgnoreSpec`` |
| 88 |
handles these cases to more closely replicate Git's behavior:: |
| 89 |
|
| 90 |
>>> from pathspec import GitIgnoreSpec |
| 91 |
>>> spec = GitIgnoreSpec.from_lines(spec_text.splitlines()) |
| 92 |
|
| 93 |
You do not specify the style of pattern for ``GitIgnoreSpec`` because it should |
| 94 |
always use ``GitIgnoreSpecPattern`` internally. |
| 95 |
|
| 96 |
|
| 97 |
Performance |
| 98 |
----------- |
| 99 |
|
| 100 |
Running lots of regular expression matches against thousands of files in Python |
| 101 |
is slow. Alternate regular expression backends can be used to improve |
| 102 |
performance. ``PathSpec`` and ``GitIgnoreSpec`` both accept a ``backend`` |
| 103 |
parameter to control the backend. The default is "best" to automatically choose |
| 104 |
the best available backend. There are currently 3 backends. |
| 105 |
|
| 106 |
The "simple" backend is the default and it simply uses Python's ``re.Pattern`` |
| 107 |
objects that are normally created. This can be the fastest when there's only 1 |
| 108 |
or 2 patterns. |
| 109 |
|
| 110 |
The "hyperscan" backend uses the `hyperscan`_ library. Hyperscan tends to be at |
| 111 |
least 2 times faster than "simple", and generally slower than "re2". This can be |
| 112 |
faster than "re2" under the right conditions with pattern counts of 1-25. |
| 113 |
|
| 114 |
The "re2" backend uses the `google-re2`_ library (not to be confused with the |
| 115 |
*re2* library on PyPI which is unrelated and abandoned). Google's re2 tends to |
| 116 |
be significantly faster than "simple", and 3 times faster than "hyperscan" at |
| 117 |
high pattern counts. |
| 118 |
|
| 119 |
See `benchmarks_backends.md`_ for comparisons between native Python regular |
| 120 |
expressions and the optional backends. |
| 121 |
|
| 122 |
|
| 123 |
.. _`benchmarks_backends.md`: https://github.com/cpburnz/python-pathspec/blob/master/benchmarks_backends.md |
| 124 |
.. _`google-re2`: https://pypi.org/project/google-re2/ |
| 125 |
.. _`hyperscan`: https://pypi.org/project/hyperscan/ |
| 126 |
|
| 127 |
|
| 128 |
FAQ |
| 129 |
--- |
| 130 |
|
| 131 |
|
| 132 |
1. How do I ignore files like *.gitignore*? |
| 133 |
+++++++++++++++++++++++++++++++++++++++++++ |
| 134 |
|
| 135 |
``GitIgnoreSpec`` (and ``PathSpec``) positively match files by default. To find |
| 136 |
the files to keep, and exclude files like *.gitignore*, you need to set |
| 137 |
``negate=True`` to flip the results:: |
| 138 |
|
| 139 |
>>> from pathspec import GitIgnoreSpec |
| 140 |
>>> spec = GitIgnoreSpec.from_lines([...]) |
| 141 |
>>> keep_files = set(spec.match_tree_files('path/to/directory', negate=True)) |
| 142 |
>>> ignore_files = set(spec.match_tree_files('path/to/directory')) |
| 143 |
|
| 144 |
|
| 145 |
License |
| 146 |
------- |
| 147 |
|
| 148 |
*pathspec* is licensed under the `Mozilla Public License Version 2.0`_. See |
| 149 |
`LICENSE`_ or the `FAQ`_ for more information. |
| 150 |
|
| 151 |
In summary, you may use *pathspec* with any closed or open source project |
| 152 |
without affecting the license of the larger work so long as you: |
| 153 |
|
| 154 |
- give credit where credit is due, |
| 155 |
|
| 156 |
- and release any custom changes made to *pathspec*. |
| 157 |
|
| 158 |
.. _`Mozilla Public License Version 2.0`: http://www.mozilla.org/MPL/2.0 |
| 159 |
.. _`LICENSE`: LICENSE |
| 160 |
.. _`FAQ`: http://www.mozilla.org/MPL/2.0/FAQ.html |
| 161 |
|
| 162 |
|
| 163 |
Source |
| 164 |
------ |
| 165 |
|
| 166 |
The source code for *pathspec* is available from the GitHub repo |
| 167 |
`cpburnz/python-pathspec`_. |
| 168 |
|
| 169 |
.. _`cpburnz/python-pathspec`: https://github.com/cpburnz/python-pathspec |
| 170 |
|
| 171 |
|
| 172 |
Installation |
| 173 |
------------ |
| 174 |
|
| 175 |
*pathspec* is available for install through `PyPI`_:: |
| 176 |
|
| 177 |
pip install pathspec |
| 178 |
|
| 179 |
*pathspec* can also be built from source. The following packages will be |
| 180 |
required: |
| 181 |
|
| 182 |
- `build`_ (>=0.6.0) |
| 183 |
|
| 184 |
*pathspec* can then be built and installed with:: |
| 185 |
|
| 186 |
python -m build |
| 187 |
pip install dist/pathspec-*-py3-none-any.whl |
| 188 |
|
| 189 |
The following optional dependencies can be installed: |
| 190 |
|
| 191 |
- `google-re2`_: Enables optional "re2" backend. |
| 192 |
- `hyperscan`_: Enables optional "hyperscan" backend. |
| 193 |
- `typing-extensions`_: Improves some type hints. |
| 194 |
|
| 195 |
.. _`PyPI`: http://pypi.python.org/pypi/pathspec |
| 196 |
.. _`build`: https://pypi.org/project/build/ |
| 197 |
.. _`typing-extensions`: https://pypi.org/project/typing-extensions/ |
| 198 |
|
| 199 |
|
| 200 |
Documentation |
| 201 |
------------- |
| 202 |
|
| 203 |
Documentation for *pathspec* is available on `Read the Docs`_. |
| 204 |
|
| 205 |
The full change history can be found in `CHANGES.rst`_ and `Change History`_. |
| 206 |
|
| 207 |
An upgrade guide is available in `UPGRADING.rst`_ and `Upgrade Guide`_. |
| 208 |
|
| 209 |
.. _`CHANGES.rst`: https://github.com/cpburnz/python-pathspec/blob/master/CHANGES.rst |
| 210 |
.. _`Change History`: https://python-path-specification.readthedocs.io/en/stable/changes.html |
| 211 |
.. _`Read the Docs`: https://python-path-specification.readthedocs.io |
| 212 |
.. _`UPGRADING.rst`: https://github.com/cpburnz/python-pathspec/blob/master/UPGRADING.rst |
| 213 |
.. _`Upgrade Guide`: https://python-path-specification.readthedocs.io/en/stable/upgrading.html |
| 214 |
|
| 215 |
|
| 216 |
Other Languages |
| 217 |
--------------- |
| 218 |
|
| 219 |
The related project `pathspec-ruby`_ (by *highb*) provides a similar library as |
| 220 |
a `Ruby gem`_. |
| 221 |
|
| 222 |
.. _`pathspec-ruby`: https://github.com/highb/pathspec-ruby |
| 223 |
.. _`Ruby gem`: https://rubygems.org/gems/pathspec |