From b4ba8ef4d7cb9f0fd5f64303b609603a7f7e1753 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=CC=81=20Padilla?= Date: Thu, 25 Jun 2015 16:58:24 -0400 Subject: [PATCH] Setup isort for code style linting --- .gitignore | 1 + .isort.cfg | 6 ++++++ .travis.yml | 2 +- requirements/requirements-codestyle.txt | 3 +++ runtests.py | 21 ++++++++++++++++++--- tox.ini | 8 ++++---- 6 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 .isort.cfg diff --git a/.gitignore b/.gitignore index 3d5f1043..e9222c2d 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ MANIFEST !.gitignore !.travis.yml +!.isort.cfg diff --git a/.isort.cfg b/.isort.cfg new file mode 100644 index 00000000..bd5648e0 --- /dev/null +++ b/.isort.cfg @@ -0,0 +1,6 @@ +[settings] +skip=.tox +atomic=true +multi_line_output=5 +known_third_party=pytest,django +known_first_party=rest_framework diff --git a/.travis.yml b/.travis.yml index 2134a144..50dc368e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,7 @@ language: python sudo: false env: - - TOX_ENV=py27-flake8 + - TOX_ENV=py27-lint - TOX_ENV=py27-docs - TOX_ENV=py34-django18 - TOX_ENV=py33-django18 diff --git a/requirements/requirements-codestyle.txt b/requirements/requirements-codestyle.txt index 88f61fdf..1412a4d8 100644 --- a/requirements/requirements-codestyle.txt +++ b/requirements/requirements-codestyle.txt @@ -1,3 +1,6 @@ # PEP8 code linting, which we run on all commits. flake8==2.4.0 pep8==1.5.7 + +# Sort and lint imports +isort==3.9.6 diff --git a/runtests.py b/runtests.py index 0008bfae..777f996a 100755 --- a/runtests.py +++ b/runtests.py @@ -1,11 +1,11 @@ #! /usr/bin/env python from __future__ import print_function -import pytest -import sys import os import subprocess +import sys +import pytest PYTEST_ARGS = { 'default': ['tests', '--tb=short'], @@ -14,6 +14,7 @@ PYTEST_ARGS = { FLAKE8_ARGS = ['rest_framework', 'tests', '--ignore=E501'] +ISORT_ARGS = ['--recursive', '--check-only', '.'] sys.path.append(os.path.dirname(__file__)) @@ -30,6 +31,13 @@ def flake8_main(args): return ret +def isort_main(args): + print('Running isort code checking') + ret = subprocess.call(['isort'] + args) + print('isort failed' if ret else 'isort passed') + return ret + + def split_class_and_function(string): class_string, function_string = string.split('.', 1) return "%s and %s" % (class_string, function_string) @@ -50,8 +58,10 @@ if __name__ == "__main__": sys.argv.remove('--nolint') except ValueError: run_flake8 = True + run_isort = True else: run_flake8 = False + run_isort = False try: sys.argv.remove('--lintonly') @@ -67,6 +77,7 @@ if __name__ == "__main__": else: style = 'fast' run_flake8 = False + run_isort = False if len(sys.argv) > 1: pytest_args = sys.argv[1:] @@ -79,7 +90,7 @@ if __name__ == "__main__": expression = split_class_and_function(first_arg) pytest_args = ['tests', '-k', expression] + pytest_args[1:] elif is_class(first_arg) or is_function(first_arg): - # `runtests.py TestCase [flags]` + # `runtests.py TestCase [flags]` # `runtests.py test_function [flags]` pytest_args = ['tests', '-k', pytest_args[0]] + pytest_args[1:] else: @@ -87,5 +98,9 @@ if __name__ == "__main__": if run_tests: exit_on_failure(pytest.main(pytest_args)) + if run_flake8: exit_on_failure(flake8_main(FLAKE8_ARGS)) + + if run_isort: + exit_on_failure(isort_main(ISORT_ARGS)) diff --git a/tox.ini b/tox.ini index f77ba8f8..d941b6d7 100644 --- a/tox.ini +++ b/tox.ini @@ -3,7 +3,7 @@ addopts=--tb=short [tox] envlist = - py27-{flake8,docs}, + py27-{lint,docs}, {py26,py27}-django14, {py26,py27,py32,py33,py34}-django{15,16}, {py27,py32,py33,py34}-django{17,18,master} @@ -22,14 +22,14 @@ deps = -rrequirements/requirements-testing.txt -rrequirements/requirements-optionals.txt -[testenv:py27-flake8] +[testenv:py27-lint] +commands = ./runtests.py --lintonly deps = -rrequirements/requirements-codestyle.txt -rrequirements/requirements-testing.txt -commands = ./runtests.py --lintonly [testenv:py27-docs] +commands = mkdocs build deps = -rrequirements/requirements-testing.txt -rrequirements/requirements-documentation.txt -commands = mkdocs build