From c9dff24208135d83e5516bc29494c71910cac0c6 Mon Sep 17 00:00:00 2001 From: Jeremy Epstein Date: Thu, 25 Aug 2016 21:02:35 +1000 Subject: [PATCH] add click-ified 'lint' command --- tasks.py | 2 +- .../{{cookiecutter.app_name}}/app.py | 3 +- .../{{cookiecutter.app_name}}/cli.py | 34 +++++++++++++++++-- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/tasks.py b/tasks.py index dfd8996..d311351 100644 --- a/tasks.py +++ b/tasks.py @@ -42,5 +42,5 @@ def test(): """Run lint commands and tests.""" run('pip install -r {0} --ignore-installed'.format(REQUIREMENTS), echo=True) os.chdir(COOKIE) - #_run_manage_command('lint') + _run_manage_command('lint') _run_manage_command('test') diff --git a/{{cookiecutter.app_name}}/{{cookiecutter.app_name}}/app.py b/{{cookiecutter.app_name}}/{{cookiecutter.app_name}}/app.py index 8f8a499..332f20c 100644 --- a/{{cookiecutter.app_name}}/{{cookiecutter.app_name}}/app.py +++ b/{{cookiecutter.app_name}}/{{cookiecutter.app_name}}/app.py @@ -68,4 +68,5 @@ def register_shellcontext(app): def register_commands(app): """Register Click commands.""" - app.cli.add_command(cli.test_command) + app.cli.add_command(cli.test) + app.cli.add_command(cli.lint) diff --git a/{{cookiecutter.app_name}}/{{cookiecutter.app_name}}/cli.py b/{{cookiecutter.app_name}}/{{cookiecutter.app_name}}/cli.py index c1f619d..2695a72 100644 --- a/{{cookiecutter.app_name}}/{{cookiecutter.app_name}}/cli.py +++ b/{{cookiecutter.app_name}}/{{cookiecutter.app_name}}/cli.py @@ -1,17 +1,45 @@ # -*- coding: utf-8 -*- """Click commands.""" import click -import os from flask.cli import with_appcontext +from glob import glob +import os +from subprocess import call HERE = os.path.abspath(os.path.dirname(__file__)) PROJECT_ROOT = os.path.join(HERE, os.pardir) TEST_PATH = os.path.join(PROJECT_ROOT, 'tests') -@click.command('test') +@click.command() @with_appcontext -def test_command(): +def test(): """Run the tests.""" import pytest pytest.main([TEST_PATH, '--verbose']) + + +@click.command() +@click.option('-f', '--fix-imports', default=False, is_flag=True, + help='Fix imports using isort, before linting') +@with_appcontext +def lint(fix_imports): + """Lint and check code style with flake8 and isort.""" + skip = ['requirements'] + root_files = glob('*.py') + root_directories = [ + name for name in next(os.walk('.'))[1] if not name.startswith('.')] + files_and_directories = [ + arg for arg in root_files + root_directories if arg not in skip] + + def execute_tool(description, *args): + """Execute a checking tool with its arguments.""" + command_line = list(args) + files_and_directories + click.echo('{}: {}'.format(description, ' '.join(command_line))) + rv = call(command_line) + if rv is not 0: + exit(rv) + + if fix_imports: + execute_tool('Fixing import order', 'isort', '-rc') + execute_tool('Checking code style', 'flake8')