Browse Source

Add pre-commit hook

David Cramer 11 years ago
parent
commit
74ac17bc74

+ 4 - 2
Makefile

@@ -8,11 +8,12 @@ UGLIFY_JS ?= node_modules/uglify-js/bin/uglifyjs
 JS_TESTS = tests/js/index.html
 JS_REPORTER = dot
 
-develop: update-submodules setup-git
+develop: update-submodules
 	npm install -q
 	pip install -q "file://`pwd`#egg=sentry[dev]" --use-mirrors
 	pip install -q "file://`pwd`#egg=sentry[tests]" --use-mirrors
 	pip install -q -e . --use-mirrors
+	make setup-git
 
 dev-postgres:
 	pip install -q "file://`pwd`#egg=sentry[dev]" --use-mirrors
@@ -29,6 +30,7 @@ dev-docs:
 
 setup-git:
 	git config branch.autosetuprebase always
+	cd .git/hooks && ln -sf ../../hooks/* ./
 
 build: locale
 
@@ -85,7 +87,7 @@ lint: lint-python lint-js
 
 lint-python:
 	@echo "Linting Python files"
-	flake8 --exclude=migrations,src/sentry/static/CACHE/* --ignore=E501,E225,E121,E123,E124,E125,E127,E128 src/sentry
+	flake8 src/sentry
 	@echo ""
 
 lint-js:

+ 41 - 0
hooks/pre-commit

@@ -0,0 +1,41 @@
+#!/usr/bin/env python
+
+import glob
+import os
+import sys
+
+sys.argv = [os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)]
+
+# git usurbs your bin path for hooks and will always run system python
+site_packages = glob.glob('%s/lib/*/site-packages' % os.environ['VIRTUAL_ENV'])[0]
+sys.path.insert(0, site_packages)
+
+def main():
+    """This is the function used by the git hook.
+
+    :param int complexity: (optional), any value > 0 enables complexity
+        checking with mccabe
+    :param bool strict: (optional), if True, this returns the total number of
+        errors which will cause the hook to fail
+    :param str ignore: (optional), a comma-separated list of errors and
+        warnings to ignore
+    :param bool lazy: (optional), allows for the instances where you don't add
+        the files to the index before running a commit, e.g., git commit -a
+    :returns: total number of errors if strict is True, otherwise 0
+    """
+    from flake8.main import DEFAULT_CONFIG
+    from flake8.engine import get_style_guide
+    from flake8.hooks import run
+
+    gitcmd = "git diff-index --cached --name-only HEAD"
+
+    _, files_modified, _ = run(gitcmd)
+    files_modified = filter(lambda x: x.endswith('.py'), files_modified)
+
+    flake8_style = get_style_guide(parse_argv=True, config_file=DEFAULT_CONFIG)
+    report = flake8_style.check_files(files_modified)
+
+    return report.total_errors
+
+if __name__ == '__main__':
+    sys.exit(main())

+ 5 - 0
setup.cfg

@@ -2,3 +2,8 @@
 python_files=test*.py
 addopts=--tb=short -x
 timeout=5
+
+[flake8]
+ignore = F821,F123,E128,E124,E501,F999
+max-line-length = 100
+exclude = .tox,.git,migrations,./src/sentry/static/CACHE/*,docs

+ 1 - 1
setup.py

@@ -43,7 +43,7 @@ if 'test' in sys.argv:
     setup_requires.append('pytest')
 
 dev_requires = [
-    'flake8>=1.7.0,<2.0',
+    'flake8>=2.0,<2.1',
 ]
 
 tests_require = [

+ 1 - 1
src/sentry/plugins/base.py

@@ -120,7 +120,7 @@ class IPlugin(local):
     control when or how the plugin gets instantiated, nor is it guaranteed that
     it will happen, or happen more than once.
 
-    >>> from sentry.plugins import Plugin
+    >>> from sentry.plugins import Plugin  # NOQA
     >>> class MyPlugin(Plugin):
     >>>     title = 'My Plugin'
     >>>

+ 2 - 2
src/sentry/web/decorators.py

@@ -4,8 +4,8 @@ from django.http import HttpResponseRedirect, HttpResponse
 from django.shortcuts import get_object_or_404
 
 from sentry.models import Project, Team, Group
-from sentry.web.helpers import render_to_response, \
-  get_login_url
+from sentry.web.helpers import (
+    render_to_response, get_login_url)
 
 
 def has_access(access_or_func=None, team=None, access=None):

+ 1 - 1
tests/__init__.py

@@ -1 +1 @@
-VERSION = 1.0
+VERSION = 1.0

+ 0 - 1
tests/sentry/buffer/redis/tests.py

@@ -8,7 +8,6 @@ from datetime import timedelta
 from django.utils import timezone
 from sentry.buffer.redis import RedisBuffer
 from sentry.models import Group, Project
-from sentry.tasks.process_buffer import process_incr
 from sentry.utils.compat import pickle
 from sentry.testutils import TestCase
 

+ 1 - 1
tests/sentry/coreapi/tests.py

@@ -8,7 +8,7 @@ import mock
 from sentry.models import Project, User
 from sentry.exceptions import InvalidTimestamp
 from sentry.coreapi import (
-    extract_auth_vars, project_from_auth_vars, APIUnauthorized, APIForbidden,
+    extract_auth_vars, project_from_auth_vars, APIForbidden,
     process_data_timestamp, validate_data, INTERFACE_ALIASES)
 from sentry.testutils import TestCase
 

+ 2 - 2
tests/sentry/plugins/helpers/tests.py

@@ -3,8 +3,8 @@
 from __future__ import absolute_import
 
 import mock
-from sentry.plugins.helpers import set_option, \
-  unset_option, get_option
+from sentry.plugins.helpers import (
+    set_option, unset_option, get_option)
 from sentry.testutils import TestCase
 
 

Some files were not shown because too many files changed in this diff