Commit 5f5d6ebc authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Merge branch 'add-pre-commit' into develop

parents cd432825 c10a3197
Loading
Loading
Loading
Loading
Loading

.pre-commit-hooks.yaml

0 → 100644
+7 −0
Original line number Diff line number Diff line
- id: pylint
  name: PyLint
  entry: pylint-reporter run
  language: python
  require_serial: true
  types: [python]
  verbose: true
+3 −0
Original line number Diff line number Diff line
@@ -4,3 +4,6 @@ ARG PY_VERSION=3.7
FROM python:$PY_VERSION
RUN --mount=type=bind,rw,target=/src \
    pip install file:///src/#egg=pylint_reporter[badges]
RUN apt-get update && apt-get install -y \
    build-essential \
    python3-dev
+52 −17
Original line number Diff line number Diff line
#
#   Copyright 2019 Dominik Sekotill <dom.sekotill@kodo.org.uk>
#   Copyright 2019,2020 Dominik Sekotill <dom.sekotill@kodo.org.uk>
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
@@ -19,18 +19,36 @@ CLI command and subcommands

import argparse
import json
import subprocess
import sys

from pylint import lint

from . import badge, errors, reporter


def set_command(parser, cmd):
	parser.set_defaults(cmd=cmd, parser=parser)


def main(argv=None):
	"""Script and runpy entrypoint"""
	parser = argparse.ArgumentParser(__package__)
	subparsers = parser.add_subparsers(required=True)

	score_parser = subparsers.add_parser('score')
	score_parser.set_defaults(cmd=cmd_score)
	run_parser = subparsers.add_parser(
		'run',
		help=cmd_run.__doc__,
		usage=f'{parser.prog} run [options] PYLINT-ARGUMENT [PYLINT-ARGUMENT ...]',
	)
	run_parser.add_argument(
		'--extras', '-e',
		action='append',
		help="The names of optional requirements sets to install",
	)
	set_command(run_parser, cmd_run)

	score_parser = subparsers.add_parser('score', help=cmd_score.__doc__)
	score_parser.add_argument(
		'--minimum', '--min',
		type=float,
@@ -48,14 +66,9 @@ def main(argv=None):
		A Pylint report serialised as a JSON file
		"""
	)
	set_command(score_parser, cmd_score)

	badge_parser = subparsers.add_parser(
		'badge',
		help="""
		Generate a shield.io badge to display the score
		"""
	)
	badge_parser.set_defaults(cmd=cmd_badge)
	badge_parser = subparsers.add_parser('badge', help=cmd_badge.__doc__)
	badge_parser.add_argument(
		'input',
		nargs='?',
@@ -76,8 +89,10 @@ def main(argv=None):
		Save the badge to this file name.
		"""
	)
	set_command(badge_parser, cmd_badge)

	args = parser.parse_args(argv or sys.argv[1:])
	args, additional = parser.parse_known_args(argv or sys.argv[1:])
	args.additional = additional

	try:
		args.cmd(args)
@@ -85,7 +100,27 @@ def main(argv=None):
		parser.exit(3, str(exc) + '\n')


def cmd_score(args) -> int:
def cmd_run(args):
	"""
	Install dependencies in the current environment and run pylint on the given files
	"""
	if not args.additional:
		args.parser.error("at least one file to lint is required")

	cmd = ['pip', 'install', '-e', '.']
	if args.extras:
		cmd.append(".[{0}]".format(','.join(args.extras)))

	proc = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
	if proc.returncode:
		args.parser.exit(3, proc.stdout)

	cmd = ['--load-plugins=pylint_reporter.reporter']
	cmd.extend(args.additional)
	lint.Run(cmd)


def cmd_score(args):
	"""
	Display the linter score, and optionally compare it with a minimum score
	"""
@@ -95,13 +130,13 @@ def cmd_score(args) -> int:
	print("Lint Score: {0:.2f}".format(score))

	if args.minimum:
		return int(score < args.minimum)

	return 0
		args.parser.exit(int(score < args.minimum))


def cmd_badge(args) -> int:
	"""Make a lint score badge from lint stats"""
def cmd_badge(args):
	"""
	Generate a shield.io badge to display the score
	"""
	with args.input as fh:
		results = json.load(fh)
	score = reporter.calculate_score(results['stats'])