Last active
July 29, 2016 15:57
-
-
Save mattvonrocketstein/3e3e881a183fd30bf641b9f6afbdb445 to your computer and use it in GitHub Desktop.
vulture fabfile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import os | |
import contextlib | |
from fabric import api | |
PKG_NAME = 'MY_PKG' | |
VULTURE_EXCLUDE_PATHS = ['tests', ] | |
VULTURE_IGNORE_FUNCTIONS = ['commandline_entry_point'] | |
@api.task | |
def vulture(): | |
""" try to find dead code paths """ | |
egrep_cmd = 'egrep -v "{0}"' | |
with api.quiet(): | |
if not api.local('which vulture').succeeded: | |
print 'vulture not found, installing it' | |
api.local('pip install vulture') | |
if VULTURE_IGNORE_FUNCTIONS: | |
ignore_functions_grep = egrep_cmd.format( | |
'|'.join(VULTURE_IGNORE_FUNCTIONS)) | |
else: | |
ignore_functions_grep = '' | |
excluded = ",".join(VULTURE_EXCLUDE_PATHS) | |
excluded_paths = (' --exclude ' + excluded) if excluded else '' | |
pipes = [] | |
if ignore_functions_grep: | |
pipes += [ignore_functions_grep] | |
# the empty-string below makes sure this | |
# command section is prefixed with a pipe | |
pipes = pipes and [''] + pipes | |
vulture_cmd = ( | |
'\n find {pkg_name}|' | |
'grep \.py$|' | |
'xargs vulture ' | |
'{pkg_name}{exclude}{pipes}') | |
vulture_cmd = vulture_cmd.format( | |
pkg_name=PKG_NAME, | |
exclude=excluded_paths, | |
pipes='|'.join(pipes)) | |
this_dir = os.path.dirname(__file__) | |
changedir = api.lcd(this_dir) | |
warn_only = api.settings(warn_only=True) | |
be_quit = api.hide('warnings') | |
err = 'could not find directory for package {0} in {1}' | |
err = err.format(PKG_NAME, this_dir) | |
with contextlib.nested(changedir, warn_only, be_quit): | |
assert os.path.exists(PKG_NAME), err | |
result = api.local(vulture_cmd, capture=True) | |
exit_code = result.return_code | |
result = '\n' + result.strip() | |
print result | |
raise SystemExit(exit_code) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment