17

我有这样的目录结构

/path/to/dir/a/foo
/path/to/dir/b/foo

并想在目录上运行 pep8,/path/to/dir/不包括/path/to/dir/a/foo

pep8 --exclude='/path/to/dir/a/foo' /path/to/dir

pep8 的预期输出是,它不应该包含来自/a/foo/

但是 pep8 也在检查里面的/a/foo/文件

当我这样做时

pep8 --exclude='foo' /path/to/dir

它排除了两者中的文件,并且a/foo /b/foo/

pep8 排除选项的模式是什么,以便它只排除文件/a/foo/而不排除文件b/foo/

4

2 回答 2

14

你可以尝试这样的事情:

pep8 --exclude='*/a/foo*' /path/to/dir

排除部分使用 fnmatch 来匹配源代码中看到的路径。

def excluded(filename):
    """
    Check if options.exclude contains a pattern that matches filename.
    """
    basename = os.path.basename(filename)
    for pattern in options.exclude:
        if fnmatch(basename, pattern):
            # print basename, 'excluded because it matches', pattern
            return True
于 2011-05-03T06:43:26.777 回答
2

我确定我在这里重新发明轮子,但我也无法让 API 工作:

import os
import re
from pep8 import StyleGuide


def get_pyfiles(directory=None, exclusions=None, ftype='.py'):
    '''generator of all ftype files in all subdirectories.
    if directory is None, will look in current directory.
    exclusions should be a regular expression.

    '''
    if directory is None:
        directory = os.getcwd()

    pyfiles = (os.path.join(dpath, fname)
               for dpath, dnames, fnames in os.walk(directory)
               for fname in [f for f in fnames
                             if f.endswith(ftype)])

    if exclusions is not None:
        c = re.compile(exclusions)
        pyfiles = (fname for fname in pyfiles if c.match(fname) is None)

    return pyfiles


def get_pep8_counter(directory=None, exclusions=None):
    if directory is None:
        directory = os.getcwd()
    paths = list(get_pyfiles(directory=directory, exclusions=exclusions))
    # I am only interested in counters (but you could do something else)
    return StyleGuide(paths=paths).check_files().counters

counter = get_pep8_counter(exclusions='.*src.*|.*doc.*')
于 2013-01-04T22:21:58.910 回答