4 Summary: Utility library for gitignore style pattern matching of file paths.
5 Author-email: "Caleb P. Burns" <cpburnz@gmail.com>
7 Description-Content-Type: text/x-rst
8 Classifier: Development Status :: 4 - Beta
9 Classifier: Intended Audience :: Developers
10 Classifier: License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)
11 Classifier: Operating System :: OS Independent
12 Classifier: Programming Language :: Python
13 Classifier: Programming Language :: Python :: 3
14 Classifier: Programming Language :: Python :: 3.7
15 Classifier: Programming Language :: Python :: 3.8
16 Classifier: Programming Language :: Python :: 3.9
17 Classifier: Programming Language :: Python :: 3.10
18 Classifier: Programming Language :: Python :: 3.11
19 Classifier: Programming Language :: Python :: Implementation :: CPython
20 Classifier: Programming Language :: Python :: Implementation :: PyPy
21 Classifier: Topic :: Software Development :: Libraries :: Python Modules
22 Classifier: Topic :: Utilities
23 Project-URL: Documentation, https://python-path-specification.readthedocs.io/en/latest/index.html
24 Project-URL: Issue Tracker, https://github.com/cpburnz/python-pathspec/issues
25 Project-URL: Source Code, https://github.com/cpburnz/python-pathspec
31 *pathspec* is a utility library for pattern matching of file paths. So
32 far this only includes Git's wildmatch pattern matching which itself is
33 derived from Rsync's wildmatch. Git uses wildmatch for its `gitignore`_
36 .. _`gitignore`: http://git-scm.com/docs/gitignore
42 Say you have a "Projects" directory and you want to back it up, but only
43 certain files, and ignore others depending on certain conditions::
46 >>> # The gitignore-style patterns for files to select, but we're including
47 >>> # instead of ignoring.
50 ... # This is a comment because the line begins with a hash: "#"
52 ... # Include several project directories (and all descendants) relative to
53 ... # the current directory. To reference a directory you must end with a
59 ... # Patterns can be negated by prefixing with exclamation mark: "!"
61 ... # Ignore temporary files beginning or ending with "~" and ending with
67 ... # These are python projects so ignore compiled python files from
71 ... # Ignore the build directories but only directly under the project
77 We want to use the ``GitWildMatchPattern`` class to compile our patterns. The
78 ``PathSpec`` class provides an interface around pattern implementations::
80 >>> spec = pathspec.PathSpec.from_lines(pathspec.patterns.GitWildMatchPattern, spec_text.splitlines())
82 That may be a mouthful but it allows for additional patterns to be implemented
83 in the future without them having to deal with anything but matching the paths
84 sent to them. ``GitWildMatchPattern`` is the implementation of the actual
85 pattern which internally gets converted into a regular expression. ``PathSpec``
86 is a simple wrapper around a list of compiled patterns.
88 To make things simpler, we can use the registered name for a pattern class
89 instead of always having to provide a reference to the class itself. The
90 ``GitWildMatchPattern`` class is registered as **gitwildmatch**::
92 >>> spec = pathspec.PathSpec.from_lines('gitwildmatch', spec_text.splitlines())
94 If we wanted to manually compile the patterns we can just do the following::
96 >>> patterns = map(pathspec.patterns.GitWildMatchPattern, spec_text.splitlines())
97 >>> spec = PathSpec(patterns)
99 ``PathSpec.from_lines()`` is simply a class method which does just that.
101 If you want to load the patterns from file, you can pass the file instance
104 >>> with open('patterns.list', 'r') as fh:
105 >>> spec = pathspec.PathSpec.from_lines('gitwildmatch', fh)
107 You can perform matching on a whole directory tree with::
109 >>> matches = spec.match_tree('path/to/directory')
111 Or you can perform matching on a specific set of file paths with::
113 >>> matches = spec.match_files(file_paths)
115 Or check to see if an individual file matches::
117 >>> is_matched = spec.match_file(file_path)
119 There is a specialized class, ``pathspec.GitIgnoreSpec``, which more closely
120 implements the behavior of **gitignore**. This uses ``GitWildMatchPattern``
121 pattern by default and handles some edge cases differently from the generic
122 ``PathSpec`` class. ``GitIgnoreSpec`` can be used without specifying the pattern
125 >>> spec = pathspec.GitIgnoreSpec.from_lines(spec_text.splitlines())
131 *pathspec* is licensed under the `Mozilla Public License Version 2.0`_. See
132 `LICENSE`_ or the `FAQ`_ for more information.
134 In summary, you may use *pathspec* with any closed or open source project
135 without affecting the license of the larger work so long as you:
137 - give credit where credit is due,
139 - and release any custom changes made to *pathspec*.
141 .. _`Mozilla Public License Version 2.0`: http://www.mozilla.org/MPL/2.0
142 .. _`LICENSE`: LICENSE
143 .. _`FAQ`: http://www.mozilla.org/MPL/2.0/FAQ.html
149 The source code for *pathspec* is available from the GitHub repo
150 `cpburnz/python-pathspec`_.
152 .. _`cpburnz/python-pathspec`: https://github.com/cpburnz/python-pathspec
158 *pathspec* is available for install through `PyPI`_::
162 *pathspec* can also be built from source. The following packages will be
167 *pathspec* can then be built and installed with::
170 pip install dist/pathspec-*-py3-none-any.whl
172 .. _`PyPI`: http://pypi.python.org/pypi/pathspec
173 .. _`build`: https://pypi.org/project/build/
179 Documentation for *pathspec* is available on `Read the Docs`_.
181 .. _`Read the Docs`: https://python-path-specification.readthedocs.io
187 The related project `pathspec-ruby`_ (by *highb*) provides a similar library as
190 .. _`pathspec-ruby`: https://github.com/highb/pathspec-ruby
191 .. _`Ruby gem`: https://rubygems.org/gems/pathspec
204 - `Issue #80`_: match_files with negated path spec. `pathspec.PathSpec.match_*()` now have a `negate` parameter to make using *.gitignore* logic easier and more efficient.
208 - `Pull #76`_: Add edge case: patterns that end with an escaped space
209 - `Issue #77`_/`Pull #78`_: Negate with caret symbol as with the exclamation mark.
212 .. _`Pull #76`: https://github.com/cpburnz/python-pathspec/pull/76
213 .. _`Issue #77`: https://github.com/cpburnz/python-pathspec/issues/77
214 .. _`Pull #78`: https://github.com/cpburnz/python-pathspec/pull/78/
215 .. _`Issue #80`: https://github.com/cpburnz/python-pathspec/issues/80
223 - `Issue #74`_: Include directory should override exclude file.
227 - `Pull #75`_: Fix partially unknown PathLike type.
228 - Convert `os.PathLike` to a string properly using `os.fspath`.
231 .. _`Issue #74`: https://github.com/cpburnz/python-pathspec/issues/74
232 .. _`Pull #75`: https://github.com/cpburnz/python-pathspec/pull/75
240 - Changed build backend to `flit_core.buildapi`_ from `setuptools.build_meta`_. Building with `setuptools` through `setup.py` is still supported for distributions that need it. See `Issue #72`_.
244 - `Issue #72`_/`Pull #73`_: Please consider switching the build-system to flit_core to ease setuptools bootstrap.
247 .. _`flit_core.buildapi`: https://flit.pypa.io/en/latest/index.html
248 .. _`Issue #72`: https://github.com/cpburnz/python-pathspec/issues/72
249 .. _`Pull #73`: https://github.com/cpburnz/python-pathspec/pull/73
257 - Added utility function `pathspec.util.append_dir_sep()` to aid in distinguishing between directories and files on the file-system. See `Issue #65`_.
261 - `Issue #66`_/`Pull #67`_: Package not marked as py.typed.
262 - `Issue #68`_: Exports are considered private.
263 - `Issue #70`_/`Pull #71`_: 'Self' string literal type is Unknown in pyright.
267 - `Issue #65`_: Checking directories via match_file() does not work on Path objects.
270 .. _`Issue #65`: https://github.com/cpburnz/python-pathspec/issues/65
271 .. _`Issue #66`: https://github.com/cpburnz/python-pathspec/issues/66
272 .. _`Pull #67`: https://github.com/cpburnz/python-pathspec/pull/67
273 .. _`Issue #68`: https://github.com/cpburnz/python-pathspec/issues/68
274 .. _`Issue #70`: https://github.com/cpburnz/python-pathspec/issues/70
275 .. _`Pull #71`: https://github.com/cpburnz/python-pathspec/pull/71
283 - Fix failing tests on Windows.
284 - Type hint on *root* parameter on `pathspec.pathspec.PathSpec.match_tree_entries()`.
285 - Type hint on *root* parameter on `pathspec.pathspec.PathSpec.match_tree_files()`.
286 - Type hint on *root* parameter on `pathspec.util.iter_tree_entries()`.
287 - Type hint on *root* parameter on `pathspec.util.iter_tree_files()`.
288 - `Issue #64`_: IndexError with my .gitignore file when trying to build a Python package.
292 - `Pull #58`_: CI: add GitHub Actions test workflow.
295 .. _`Pull #58`: https://github.com/cpburnz/python-pathspec/pull/58
296 .. _`Issue #64`: https://github.com/cpburnz/python-pathspec/issues/64
304 - Fix documentation on `pathspec.pattern.RegexPattern.match_file()`.
305 - `Pull #60`_: Remove redundant wheel dep from pyproject.toml.
306 - `Issue #61`_: Dist failure for Fedora, CentOS, EPEL.
307 - `Issue #62`_: Since version 0.10.0 pure wildcard does not work in some cases.
311 - Restore support for legacy installations using `setup.py`. See `Issue #61`_.
314 .. _`Pull #60`: https://github.com/cpburnz/python-pathspec/pull/60
315 .. _`Issue #61`: https://github.com/cpburnz/python-pathspec/issues/61
316 .. _`Issue #62`: https://github.com/cpburnz/python-pathspec/issues/62
324 - Dropped support of EOL Python 2.7, 3.5, 3.6. See `Issue #47`_.
325 - The *gitwildmatch* pattern `dir/*` is now handled the same as `dir/`. This means `dir/*` will now match all descendants rather than only direct children. See `Issue #19`_.
326 - Added `pathspec.GitIgnoreSpec` class (see new features).
327 - Changed build system to `pyproject.toml`_ and build backend to `setuptools.build_meta`_ which may have unforeseen consequences.
328 - Renamed GitHub project from `python-path-specification`_ to `python-pathspec`_. See `Issue #35`_.
332 - Deprecated: `pathspec.util.match_files()` is an old function no longer used.
333 - Deprecated: `pathspec.match_files()` is an old function no longer used.
334 - Deprecated: `pathspec.util.normalize_files()` is no longer used.
335 - Deprecated: `pathspec.util.iter_tree()` is an alias for `pathspec.util.iter_tree_files()`.
336 - Deprecated: `pathspec.iter_tree()` is an alias for `pathspec.util.iter_tree_files()`.
337 - Deprecated: `pathspec.pattern.Pattern.match()` is no longer used. Use or implement
338 `pathspec.pattern.Pattern.match_file()`.
342 - Added class `pathspec.gitignore.GitIgnoreSpec` (with alias `pathspec.GitIgnoreSpec`) to implement *gitignore* behavior not possible with standard `PathSpec` class. The particular *gitignore* behavior implemented is prioritizing patterns matching the file directly over matching an ancestor directory.
346 - `Issue #19`_: Files inside an ignored sub-directory are not matched.
347 - `Issue #41`_: Incorrectly (?) matches files inside directories that do match.
348 - `Pull #51`_: Refactor deprecated unittest aliases for Python 3.11 compatibility.
349 - `Issue #53`_: Symlink pathspec_meta.py breaks Windows.
350 - `Issue #54`_: test_util.py uses os.symlink which can fail on Windows.
351 - `Issue #55`_: Backslashes at start of pattern not handled correctly.
352 - `Pull #56`_: pyproject.toml: include subpackages in setuptools config
353 - `Issue #57`_: `!` doesn't exclude files in directories if the pattern doesn't have a trailing slash.
357 - Support Python 3.10, 3.11.
358 - Modernize code to Python 3.7.
359 - `Issue #52`_: match_files() is not a pure generator function, and it impacts tree_*() gravely.
362 .. _`python-path-specification`: https://github.com/cpburnz/python-path-specification
363 .. _`python-pathspec`: https://github.com/cpburnz/python-pathspec
364 .. _`pyproject.toml`: https://pip.pypa.io/en/stable/reference/build-system/pyproject-toml/
365 .. _`setuptools.build_meta`: https://setuptools.pypa.io/en/latest/build_meta.html
366 .. _`Issue #19`: https://github.com/cpburnz/python-pathspec/issues/19
367 .. _`Issue #35`: https://github.com/cpburnz/python-pathspec/issues/35
368 .. _`Issue #41`: https://github.com/cpburnz/python-pathspec/issues/41
369 .. _`Issue #47`: https://github.com/cpburnz/python-pathspec/issues/47
370 .. _`Pull #51`: https://github.com/cpburnz/python-pathspec/pull/51
371 .. _`Issue #52`: https://github.com/cpburnz/python-pathspec/issues/52
372 .. _`Issue #53`: https://github.com/cpburnz/python-pathspec/issues/53
373 .. _`Issue #54`: https://github.com/cpburnz/python-pathspec/issues/54
374 .. _`Issue #55`: https://github.com/cpburnz/python-pathspec/issues/55
375 .. _`Pull #56`: https://github.com/cpburnz/python-pathspec/pull/56
376 .. _`Issue #57`: https://github.com/cpburnz/python-pathspec/issues/57
382 - `Issue #44`_/`Pull #50`_: Raise `GitWildMatchPatternError` for invalid git patterns.
383 - `Pull #45`_: Fix for duplicate leading double-asterisk, and edge cases.
384 - `Issue #46`_: Fix matching absolute paths.
385 - API change: `util.normalize_files()` now returns a `Dict[str, List[pathlike]]` instead of a `Dict[str, pathlike]`.
386 - Added type hinting.
388 .. _`Issue #44`: https://github.com/cpburnz/python-pathspec/issues/44
389 .. _`Pull #45`: https://github.com/cpburnz/python-pathspec/pull/45
390 .. _`Issue #46`: https://github.com/cpburnz/python-pathspec/issues/46
391 .. _`Pull #50`: https://github.com/cpburnz/python-pathspec/pull/50
397 - `Pull #43`_: Add support for addition operator.
399 .. _`Pull #43`: https://github.com/cpburnz/python-pathspec/pull/43
405 - `Issue #30`_: Expose what patterns matched paths. Added `util.detailed_match_files()`.
406 - `Issue #31`_: `match_tree()` doesn't return symlinks.
407 - `Issue #34`_: Support `pathlib.Path`\ s.
408 - Add `PathSpec.match_tree_entries` and `util.iter_tree_entries()` to support directories and symlinks.
409 - API change: `match_tree()` has been renamed to `match_tree_files()`. The old name `match_tree()` is still available as an alias.
410 - API change: `match_tree_files()` now returns symlinks. This is a bug fix but it will change the returned results.
412 .. _`Issue #30`: https://github.com/cpburnz/python-pathspec/issues/30
413 .. _`Issue #31`: https://github.com/cpburnz/python-pathspec/issues/31
414 .. _`Issue #34`: https://github.com/cpburnz/python-pathspec/issues/34
420 - `Pull #28`_: Add support for Python 3.8, and drop Python 3.4.
421 - `Pull #29`_: Publish bdist wheel.
423 .. _`Pull #28`: https://github.com/cpburnz/python-pathspec/pull/28
424 .. _`Pull #29`: https://github.com/cpburnz/python-pathspec/pull/29
430 - `Pull #24`_: Drop support for Python 2.6, 3.2, and 3.3.
431 - `Pull #25`_: Update README.rst.
432 - `Pull #26`_: Method to escape gitwildmatch.
434 .. _`Pull #24`: https://github.com/cpburnz/python-pathspec/pull/24
435 .. _`Pull #25`: https://github.com/cpburnz/python-pathspec/pull/25
436 .. _`Pull #26`: https://github.com/cpburnz/python-pathspec/pull/26
442 - Fixed file system error handling.
448 - Improved type checking.
449 - Created scripts to test Python 2.6 because Tox removed support for it.
450 - Improved byte string handling in Python 3.
451 - `Issue #22`_: Handle dangling symlinks.
453 .. _`Issue #22`: https://github.com/cpburnz/python-pathspec/issues/22
459 - `Issue #21`_: Fix collections deprecation warning.
461 .. _`Issue #21`: https://github.com/cpburnz/python-pathspec/issues/21
467 - Improved unit tests.
468 - Improved type checking.
469 - `Issue #20`_: Support current directory prefix.
471 .. _`Issue #20`: https://github.com/cpburnz/python-pathspec/issues/20
477 - Add documentation link to README.
483 - `Pull #17`_: Add link to Ruby implementation of *pathspec*.
484 - Add sphinx documentation.
486 .. _`Pull #17`: https://github.com/cpburnz/python-pathspec/pull/17
492 - `Issue #14`_: Fix byte strings for Python 3.
493 - `Pull #15`_: Include "LICENSE" in source package.
494 - `Issue #16`_: Support Python 2.6.
496 .. _`Issue #14`: https://github.com/cpburnz/python-pathspec/issues/14
497 .. _`Pull #15`: https://github.com/cpburnz/python-pathspec/pull/15
498 .. _`Issue #16`: https://github.com/cpburnz/python-pathspec/issues/16
510 - `Pull #13`_: Add equality methods to `PathSpec` and `RegexPattern`.
512 .. _`Pull #13`: https://github.com/cpburnz/python-pathspec/pull/13
518 - `Issue #12`_: Add `PathSpec.match_file()`.
519 - Renamed `gitignore.GitIgnorePattern` to `patterns.gitwildmatch.GitWildMatchPattern`.
520 - Deprecated `gitignore.GitIgnorePattern`.
522 .. _`Issue #12`: https://github.com/cpburnz/python-pathspec/issues/12
528 - `Issue #11`_: Support converting patterns into regular expressions without compiling them.
529 - API change: Subclasses of `RegexPattern` should implement `pattern_to_regex()`.
531 .. _`Issue #11`: https://github.com/cpburnz/python-pathspec/issues/11
537 - `Pull #7`_: Fixed non-recursive links.
538 - `Pull #8`_: Fixed edge cases in gitignore patterns.
539 - `Pull #9`_: Fixed minor usage documentation.
540 - Fixed recursion detection.
541 - Fixed trivial incompatibility with Python 3.2.
543 .. _`Pull #7`: https://github.com/cpburnz/python-pathspec/pull/7
544 .. _`Pull #8`: https://github.com/cpburnz/python-pathspec/pull/8
545 .. _`Pull #9`: https://github.com/cpburnz/python-pathspec/pull/9
551 - Improved documentation.
557 - `Pull #5`_: Use tox for testing.
558 - `Issue #6`_: Fixed matching Windows paths.
559 - Improved documentation.
560 - API change: `spec.match_tree()` and `spec.match_files()` now return iterators instead of sets.
562 .. _`Pull #5`: https://github.com/cpburnz/python-pathspec/pull/5
563 .. _`Issue #6`: https://github.com/cpburnz/python-pathspec/issues/6
575 - `Pull #3`_: Fixed trailing slash in gitignore patterns.
576 - `Pull #4`_: Fixed test for trailing slash in gitignore patterns.
577 - Added registered patterns.
579 .. _`Pull #3`: https://github.com/cpburnz/python-pathspec/pull/3
580 .. _`Pull #4`: https://github.com/cpburnz/python-pathspec/pull/4
593 - Fixed comment gitignore patterns.
594 - Fixed relative path gitignore patterns.