Commit 11c8b925 authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Switch to 'click' for command line parsing

parent 605cd5f8
Loading
Loading
Loading
Loading
+24 −33
Original line number Diff line number Diff line
@@ -2,56 +2,47 @@
Run pylint from pre-commit with project dependencies installed
"""

import argparse
import subprocess
import sys

import click
from pylint import lint

__version__ = '0.3'


def argument_parser():
	parser = argparse.ArgumentParser(description=__doc__)
	parser.add_argument(
@click.command()
@click.option(
	'--extras', '-e',
		action='append',
	multiple=True,
	help="The names of optional requirements sets to install",
)
	parser.add_argument(
@click.option(
	'--fail-under', '-f',
	default=10.0,
	type=float,
	help="""
	If the score is below the given threshold a non-zero exit code will be returned
		"""
	)
	parser.add_argument(
		'files',
		nargs='*',
		help="Files to lint; these will typically be supplied by pre-commit",
	""",
)
	return parser


def main():
	ap = argument_parser()
	opts = ap.parse_args()

@click.argument('files', nargs=-1)
def main(extras, fail_under, files):
	join = ','.join
	cmd = [
		'pip', 'install', '--upgrade', '--editable',
		f'.[{join(opts.extras)}]' if opts.extras else '.',
		f'.[{join(extras)}]' if extras else '.',
	]

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

	run = lint.Run(opts.files, do_exit=False)
	run = lint.Run(files, do_exit=False)
	score = run.linter.stats['global_note']

	if score < opts.fail_under:
		ap.exit(1)
	if score < fail_under:
		sys.exit(1)


if __name__ == '__main__':
+1 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ author = "Dom Sekotill"
author-email = "dom.sekotill@kodo.org.uk"
home-page = "https://code.kodo.org.uk/dom/pre-commit-pylint"
requires = [
	"click ~=7.0",
	"pylint >=2,<3",
]
classifiers = [