Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting glob-style operations #123

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
repos:
- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
- id: black

- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
hooks:
- id: pyupgrade
args: [--py38-plus]

- repo: https://github.com/psf/black-pre-commit-mirror
rev: 23.10.1
hooks:
- id: black

- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
Expand Down
23 changes: 19 additions & 4 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
"""Development automation."""
"""Development automation. Use this to build the documentation and run tests.

To install IPython for interactive debugging:

nox -s <name> -- -i

"""

import nox

Expand All @@ -18,6 +24,10 @@ def _install_this_editable(session, *, extras=None):
silent=True,
)

if "-i" in session.posargs:
session.posargs.pop(session.posargs.index("-i"))
session.install("ipython")


@nox.session(reuse_venv=True)
def lint(session):
Expand All @@ -44,6 +54,11 @@ def docs(session):
@nox.session(name="docs-live", reuse_venv=True)
def docs_live(session):
_install_this_editable(session, extras=["docs"])
session.run(
"sphinx-autobuild", "-b", "html", "docs/", "build/docs", *session.posargs
)
cmd = [
"sphinx-autobuild",
"-b=html",
"--port=0",
"docs/",
"build/docs",
]
session.run(*cmd, *session.posargs)
4 changes: 3 additions & 1 deletion src/sphinx_autobuild/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from colorama import Fore, Style

# Hard-coded options that we know how to pass-through to Sphinx
# ref: https://www.sphinx-doc.org/en/master/man/sphinx-build.html#cmdoption-sphinx-build-M # noqa
SPHINX_BUILD_OPTIONS = (
("b", "builder"),
("a", None),
Expand All @@ -23,7 +25,7 @@
("q", None),
("Q", None),
("w", "file"),
("-keep-going", None),
("keep_going", None),
("W", None),
("T", None),
("P", None),
Expand Down
24 changes: 21 additions & 3 deletions src/sphinx_autobuild/ignore.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Logic for ignoring paths."""
import fnmatch
import os
import re
from glob import glob
from os.path import abspath, sep


def get_ignore(regular, regex_based):
Expand All @@ -11,11 +12,28 @@ def get_ignore(regular, regex_based):

def ignore(path):
"""Determine if path should be ignored."""
# Any regular pattern matches.
# Return the full path so we make sure we handle relative paths OK
path_expanded = abspath(path)

# Any regular pattern and glob matches
for pattern in regular_patterns:
# Expand the pattern into a list of files that match a glob
matched_files = [abspath(ii) for ii in glob(pattern, recursive=True)]

# If this file matches any of the glob matches, we ignore it
if path_expanded in matched_files:
return True

# If the parent of this path matches any of the glob matches, ignore it
if any(path_expanded.startswith(imatch) for imatch in matched_files):
return True

# These two checks are for preserving old behavior.
# They might not be necessary but leaving here just in case.
# Neither depends on the files actually being on disk.
if fnmatch.fnmatch(path, pattern):
return True
if path.startswith(pattern + os.sep):
if path.strip(sep).startswith(pattern.strip(sep)):
return True

# Any regular expression matches.
Expand Down
24 changes: 24 additions & 0 deletions tests/test_ignore.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from glob import glob

from sphinx_autobuild.ignore import get_ignore


Expand Down Expand Up @@ -72,3 +74,25 @@ def test_multiple_both():
assert ignored("foo/random.txt")
assert ignored("foo/module.pyc")
assert ignored("bar/__pycache__/file.pyc")


def test_glob_expression():
ignored = get_ignore(
[
# Glob for folder
"**/do_ignore",
# Glob for files
"**/*doignore*.*",
],
[],
)
# Root folder of our glob test files. Assume tests are run from project root.
for ifile in glob("tests/test_ignore_glob/**/*"):
# Convert to be relative to the tests directory since that mimics
# the user's behavior.
if "do_ignore" in ifile or "doignore" in ifile:
print(f"Should ignore: {ifile})")
assert ignored(ifile)
else:
print(f"Should NOT ignore: {ifile})")
assert not ignored(ifile)
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.