1. Mathieu Agopian
  2. flake8

Commits

Mathieu Agopian  committed 352b13b

Take the --excluded param into account

  • Participants
  • Parent commits bf318be
  • Branches support-exclude

Comments (0)

Files changed (2)

File flake8/run.py

View file
 """
 import sys
 import os
-import os.path
+from fnmatch import fnmatch
 from subprocess import PIPE, Popen
 import select
 try:
     return warnings
 
 
-def _get_python_files(paths):
+def _excluded(filename, exclude):
+    """Is a file/folder matching one of the exclude patterns?"""
+    basename = os.path.basename(filename)
+    for pattern in exclude:
+        if fnmatch(basename, pattern):
+            return True
+
+
+def _get_python_files(paths, exclude=None):
     for path in paths:
+        if _excluded(path, exclude):
+            continue
         if os.path.isdir(path):
             for dirpath, dirnames, filenames in os.walk(path):
+                if _excluded(dirpath, exclude):
+                    continue
                 for filename in filenames:
-                    if not filename.endswith('.py'):
+                    if (_excluded(filename, exclude) or
+                        not filename.endswith('.py')):
                         continue
                     fullpath = os.path.join(dirpath, filename)
                     if not skip_file(fullpath):
         pyflakes._MAGIC_GLOBALS = orig_builtins | builtins
 
     if args and options.filename is not None:
-        for path in _get_python_files(args):
+        for path in _get_python_files(args, options.exclude):
             warnings += check_file(path, complexity)
     else:
         # wait for 1 second on the stdin fd

File flake8/tests/test_flake8.py

View file
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from os import path
+from unittest import TestCase
+
+from flake8.run import _get_python_files
+
+
+class TestFlake8(TestCase):
+
+    def test_exclude(self):
+        """Exclude files/folders matching the given exclude pattern"""
+        here = path.dirname(__file__)
+        paths = [here]
+        # exclude files "test_*": only __init__.py left in current folder
+        files = _get_python_files(paths, ["test_*"])
+        self.assertEqual(len(list(files)), 1)
+        # exclude folder "tests": nothing left
+        files = _get_python_files(paths, ["tests"])
+        self.assertEqual(len(list(files)), 0)