Skip to content

Instantly share code, notes, and snippets.

@kpantic
Last active August 29, 2015 14:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kpantic/d90db4760c02924984c1 to your computer and use it in GitHub Desktop.
Save kpantic/d90db4760c02924984c1 to your computer and use it in GitHub Desktop.
Snippet for adding PEP8 and JSHint validation to your Fabric fabfile.
from fabric.api import (task, local, lcd)
"""
The pep8 and jshint tasks return False if errors were found and True if found.
The show_errors parameter controls if the method prints out the errors returned by
the checker program.
Requirements: Install pep8 and jshint, so that they can be run from the command line
- pip install pep8
- npm -g install jshint
"""
@task
def pep8(show_errors=False):
with(lcd(os.path.join(os.path.dirname(__file__), APP_DIR))):
return handle_error_checking_cmd(
"find . -name '*.py' -not -iwholename "
"'*migrations*' -exec pep8 {} \;",
show_errors,
"PEP8"
)
@task
def jshint(show_errors=False):
with(nested(lcd(os.path.join(os.path.dirname(__file__), STATIC_JS_DIR)),
settings(warn_only=True))):
return handle_error_checking_cmd(
"jshint . | grep ': line *'",
show_errors,
"JSHint"
)
# Helper function to avoid repetition
def handle_error_checking_cmd(cmd, show_errors, error_name=''):
errors = local(cmd, capture=True)
error_list = errors.splitlines()
error_count = len(error_list)
if show_errors:
print(errors)
if error_count > 0:
print("You have %s %s errors" % (error_count, error_name))
return False
else:
print("You have no %s errors :D" % (error_name, ))
return True
@v6
Copy link

v6 commented Jan 8, 2015

I assume we're to replace STATIC_JS_DIR with a target to validate, right?

@v6
Copy link

v6 commented Jan 8, 2015

Also, where does nested come from?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment