add click-ified 'lint' command

master
Jeremy Epstein 8 years ago
parent 7bf22549bd
commit c9dff24208
  1. 2
      tasks.py
  2. 3
      {{cookiecutter.app_name}}/{{cookiecutter.app_name}}/app.py
  3. 34
      {{cookiecutter.app_name}}/{{cookiecutter.app_name}}/cli.py

@ -42,5 +42,5 @@ def test():
"""Run lint commands and tests.""" """Run lint commands and tests."""
run('pip install -r {0} --ignore-installed'.format(REQUIREMENTS), echo=True) run('pip install -r {0} --ignore-installed'.format(REQUIREMENTS), echo=True)
os.chdir(COOKIE) os.chdir(COOKIE)
#_run_manage_command('lint') _run_manage_command('lint')
_run_manage_command('test') _run_manage_command('test')

@ -68,4 +68,5 @@ def register_shellcontext(app):
def register_commands(app): def register_commands(app):
"""Register Click commands.""" """Register Click commands."""
app.cli.add_command(cli.test_command) app.cli.add_command(cli.test)
app.cli.add_command(cli.lint)

@ -1,17 +1,45 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
"""Click commands.""" """Click commands."""
import click import click
import os
from flask.cli import with_appcontext from flask.cli import with_appcontext
from glob import glob
import os
from subprocess import call
HERE = os.path.abspath(os.path.dirname(__file__)) HERE = os.path.abspath(os.path.dirname(__file__))
PROJECT_ROOT = os.path.join(HERE, os.pardir) PROJECT_ROOT = os.path.join(HERE, os.pardir)
TEST_PATH = os.path.join(PROJECT_ROOT, 'tests') TEST_PATH = os.path.join(PROJECT_ROOT, 'tests')
@click.command('test') @click.command()
@with_appcontext @with_appcontext
def test_command(): def test():
"""Run the tests.""" """Run the tests."""
import pytest import pytest
pytest.main([TEST_PATH, '--verbose']) 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')

Loading…
Cancel
Save