Adding argument: --summary-json-output-file

This commit is contained in:
Andrew Glenn 2022-02-21 19:57:47 -06:00
parent 7858ad066f
commit 9fd260feb1
2 changed files with 15 additions and 2 deletions

View file

@ -3,12 +3,15 @@ from __future__ import annotations
import argparse import argparse
import contextlib import contextlib
import functools import functools
import json
import logging import logging
import os import os
import pathlib
import re import re
import subprocess import subprocess
import time import time
import unicodedata import unicodedata
from typing import Any from typing import Any
from typing import Collection from typing import Collection
from typing import MutableMapping from typing import MutableMapping
@ -72,6 +75,7 @@ def filter_by_include_exclude(
class Classifier: class Classifier:
def __init__(self, filenames: Collection[str]) -> None: def __init__(self, filenames: Collection[str]) -> None:
self.filenames = [f for f in filenames if os.path.lexists(f)] self.filenames = [f for f in filenames if os.path.lexists(f)]
self.summary_json = {}
@functools.lru_cache(maxsize=None) @functools.lru_cache(maxsize=None)
def _types_for_file(self, filename: str) -> set[str]: def _types_for_file(self, filename: str) -> set[str]:
@ -160,6 +164,7 @@ def _run_single_hook(
cols=cols, cols=cols,
), ),
) )
classifier.summary_json[hook.name] = SKIPPED
duration = None duration = None
retcode = 0 retcode = 0
diff_after = diff_before diff_after = diff_before
@ -176,6 +181,7 @@ def _run_single_hook(
cols=cols, cols=cols,
), ),
) )
classifier.summary_json[hook.name] = SKIPPED
duration = None duration = None
retcode = 0 retcode = 0
diff_after = diff_before diff_after = diff_before
@ -204,7 +210,7 @@ def _run_single_hook(
status = 'Passed' status = 'Passed'
output.write_line(color.format_color(status, print_color, use_color)) 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: if verbose or hook.verbose or retcode or files_modified:
_subtle_line(f'- hook id: {hook.id}', use_color) _subtle_line(f'- hook id: {hook.id}', use_color)
@ -306,7 +312,10 @@ def _run_hooks(
'git', '--no-pager', 'diff', '--no-ext-diff', 'git', '--no-pager', 'diff', '--no-ext-diff',
f'--color={git_color_opt}', 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 return retval

View file

@ -154,6 +154,10 @@ def _add_run_options(parser: argparse.ArgumentParser) -> None:
'the rewrite' '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: def _adjust_args_and_chdir(args: argparse.Namespace) -> None: