Skip to content

Commit

Permalink
Use stdlib.py as a default whitelist (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
RJ722 authored and jendrikseipp committed Jun 25, 2017
1 parent 7e41b08 commit f2c0cf5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -51,4 +51,5 @@ def run_tests(self):
cmdclass={'test': PyTest},
packages=['vulture'],
package_dir={'vulture': 'vulture'},
package_data={'vulture': ['whitelists/*.py']},
)
8 changes: 6 additions & 2 deletions tests/test_script.py
Expand Up @@ -20,12 +20,16 @@ def get_output(args):
return child.communicate()[0].decode("utf-8")


def test_script_with_whitelist():
def test_script_with_explicit_whitelist():
assert call_vulture(['vulture/core.py', WHITELIST]) == 0


def test_script_with_implicit_whitelist():
assert call_vulture(['vulture/core.py']) == 0


def test_script_without_whitelist():
assert call_vulture(['vulture/core.py']) == 1
assert call_vulture(['vulture/core.py', '--exclude', 'whitelists']) == 1


def test_exclude():
Expand Down
26 changes: 19 additions & 7 deletions vulture/core.py
Expand Up @@ -31,6 +31,7 @@
from fnmatch import fnmatchcase
import optparse
import os
import pkgutil
import re
import sys
import tokenize
Expand Down Expand Up @@ -166,15 +167,14 @@ def _get_modules(self, paths, toplevel=True):
return modules

def scavenge(self, paths):
modules = self._get_modules(paths)
included_modules = []
for module in modules:
if any(fnmatchcase(module, pattern) for pattern in self.exclude):
def exclude(name):
return any(fnmatchcase(name, pattern) for pattern in self.exclude)

for module in self._get_modules(paths):
if exclude(module):
self.log('Excluded:', module)
else:
included_modules.append(module)
continue

for module in included_modules:
self.log('Scanning:', module)
try:
module_string = read_file(module)
Expand All @@ -184,6 +184,18 @@ def scavenge(self, paths):
else:
self.scan(module_string, filename=module)

whitelist_names = ['stdlib.py']
for name in whitelist_names:
path = os.path.join('whitelists', name)
if exclude(path):
self.log('Excluded Whitelist:', path)
else:
module_data = pkgutil.get_data('vulture', path)
if module_data is None:
sys.exit('Error: Please use "python -m vulture.core".')
module_string = module_data.decode("utf-8")
self.scan(module_string, filename=path)

def report(self):
def file_lineno(item):
return (item.filename.lower(), item.lineno)
Expand Down

0 comments on commit f2c0cf5

Please sign in to comment.