Skip to content

Commit

Permalink
Merge e39325a into 4cc44c1
Browse files Browse the repository at this point in the history
  • Loading branch information
GaretJax committed Apr 3, 2015
2 parents 4cc44c1 + e39325a commit 64fca93
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
24 changes: 18 additions & 6 deletions sphinx_autobuild/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import argparse
import fnmatch
import os
import re
import subprocess
import sys

Expand Down Expand Up @@ -116,20 +117,30 @@ class SphinxBuilder(object):
"""
Helper class to run sphinx-build command.
"""
def __init__(self, outdir, args, ignored=None):
def __init__(self, outdir, args, ignored=None, regex_ignored=None):
self._outdir = outdir
self._args = args
self._ignored = ignored or []
self._ignored.append(outdir)
self._regex_ignored = [re.compile(r) for r in regex_ignored or []]

def __call__(self, watcher, src_path):
def is_ignored(self, src_path):
path = self.get_relative_path(src_path)

for i in self._ignored:
if fnmatch.fnmatch(path, i):
return
return True
if src_path.startswith(i + os.sep):
return
return True

for r in self._regex_ignored:
if r.search(src_path):
return True

def __call__(self, watcher, src_path):
path = self.get_relative_path(src_path)

if self.is_ignored(src_path):
return

watcher._action_file = path # TODO: Hack (see above)

Expand Down Expand Up @@ -202,6 +213,7 @@ def get_parser():
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--port', type=int, default=8000)
parser.add_argument('-H', '--host', type=str, default='127.0.0.1')
parser.add_argument('-r', '--re-ignore', action='append', default=[])
parser.add_argument('-i', '--ignore', action='append', default=[])
parser.add_argument('-z', '--watch', action='append', metavar='DIR',
default=[],
Expand Down Expand Up @@ -254,7 +266,7 @@ def main():
if not os.path.exists(outdir):
os.makedirs(outdir)

builder = SphinxBuilder(outdir, build_args, ignored)
builder = SphinxBuilder(outdir, build_args, ignored, args.re_ignore)
server = Server(watcher=LivereloadWatchdogWatcher())

server.watch(srcdir, builder)
Expand Down
2 changes: 1 addition & 1 deletion sphinx_autobuild/test/test_autobuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_autobuild_with_options(mock_makedirs,

# --ignore
mock_builder.assert_called_once_with(
'/output', ['/source', '/output'], ['/ignored'])
'/output', ['/source', '/output'], ['/ignored'], [])

# --watch
calls = [call('/source', mock_builder.return_value),
Expand Down
46 changes: 46 additions & 0 deletions sphinx_autobuild/test/test_ignored.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from sphinx_autobuild import SphinxBuilder


class TestWatcher(object):
pass


class TestBuilder(SphinxBuilder):
def __init__(self, ignored=None, regex_ignored=None):
super(TestBuilder, self).__init__('', [], ignored, regex_ignored)
self.built = []

def __call__(self, src_path):
watcher = TestWatcher()
super(TestBuilder, self).__call__(watcher, src_path)
return hasattr(watcher, '_action_file')

def build(self, path):
self.built.append(path)


def test_no_ignore():
builder = TestBuilder()
assert builder('test.rst')


def test_fnmatch():
builder = TestBuilder(ignored=[
'test.rst',
'test-?.rst',
'test'
])
assert builder('test1.rst')
assert not builder('test.rst')
assert not builder('test-2.rst')
assert not builder('test/test.rst')
assert builder('test1/test.rst')


def test_regex():
builder = TestBuilder(regex_ignored=[
r'__pycache__/.*\.py',
])
assert builder('test.py')
assert builder('__pycache__/test.rst')
assert not builder('__pycache__/test.py')

0 comments on commit 64fca93

Please sign in to comment.