From 9fd260feb1e173ab0059d1fcdc99b18d007687e2 Mon Sep 17 00:00:00 2001 From: Andrew Glenn <29951057+andrew-glenn@users.noreply.github.com> Date: Mon, 21 Feb 2022 19:57:47 -0600 Subject: [PATCH] Adding argument: --summary-json-output-file --- pre_commit/commands/run.py | 13 +++++++++++-- pre_commit/main.py | 4 ++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/pre_commit/commands/run.py b/pre_commit/commands/run.py index 37f989b5..b1d2ee13 100644 --- a/pre_commit/commands/run.py +++ b/pre_commit/commands/run.py @@ -3,12 +3,15 @@ from __future__ import annotations import argparse import contextlib import functools +import json import logging import os +import pathlib import re import subprocess import time import unicodedata + from typing import Any from typing import Collection from typing import MutableMapping @@ -72,6 +75,7 @@ def filter_by_include_exclude( class Classifier: def __init__(self, filenames: Collection[str]) -> None: self.filenames = [f for f in filenames if os.path.lexists(f)] + self.summary_json = {} @functools.lru_cache(maxsize=None) def _types_for_file(self, filename: str) -> set[str]: @@ -160,6 +164,7 @@ def _run_single_hook( cols=cols, ), ) + classifier.summary_json[hook.name] = SKIPPED duration = None retcode = 0 diff_after = diff_before @@ -176,6 +181,7 @@ def _run_single_hook( cols=cols, ), ) + classifier.summary_json[hook.name] = SKIPPED duration = None retcode = 0 diff_after = diff_before @@ -204,7 +210,7 @@ def _run_single_hook( status = 'Passed' output.write_line(color.format_color(status, print_color, use_color)) - + classifier.summary_json[hook.name] = status if verbose or hook.verbose or retcode or files_modified: _subtle_line(f'- hook id: {hook.id}', use_color) @@ -306,7 +312,10 @@ def _run_hooks( 'git', '--no-pager', 'diff', '--no-ext-diff', f'--color={git_color_opt}', )) - + if args.summary_json_output_file: + fn = pathlib.Path(args.summary_json_output_file).expanduser().resolve() + with open(fn,'w') as f: + f.write(json.dumps(classifier.summary_json)) return retval diff --git a/pre_commit/main.py b/pre_commit/main.py index 7ab9515c..47b09902 100644 --- a/pre_commit/main.py +++ b/pre_commit/main.py @@ -154,6 +154,10 @@ def _add_run_options(parser: argparse.ArgumentParser) -> None: 'the rewrite' ), ) + parser.add_argument( + '--summary-json-output-file', + help='Write summary information to a json file.' + ) def _adjust_args_and_chdir(args: argparse.Namespace) -> None: