From cbd6ff76ec36a09a2d6e7f7e6896c686bd63ca0d Mon Sep 17 00:00:00 2001 From: Yiwei Zhang Date: Fri, 7 Mar 2025 08:34:51 -0800 Subject: [PATCH] presubmit_support: write to stdout if --json_output - is specified As part of new findings cider extension work, we need to show presubmit result as findings in the new panel. The extension only has access to stdout/stderr of the process executing presubmit checks. Therefore, we need a way to write structured data (i.e. json) to the stdout. Bug: 397700948 Change-Id: Ie9e51d39389613c5f93ca054102b169a99e356b3 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/6330090 Reviewed-by: Josip Sokcevic Commit-Queue: Yiwei Zhang --- presubmit_support.py | 70 ++++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/presubmit_support.py b/presubmit_support.py index b5d10704e..2feb6b414 100755 --- a/presubmit_support.py +++ b/presubmit_support.py @@ -2011,16 +2011,43 @@ def DoPresubmitChecks(change, else: messages.setdefault('Messages', []).append(result) - # Print the different message types in a consistent order. ERRORS go - # last so that they will be most visible in the local-presubmit output. - for name in ['Messages', 'Warnings', 'ERRORS']: - if name in messages: - items = messages[name] - sys.stdout.write('** Presubmit %s: %d **\n' % - (name, len(items))) - for item in items: - item.handle() - sys.stdout.write('\n') + presubmit_results_json = json.dumps( + { + 'errors': + [error.json_format() for error in messages.get('ERRORS', [])], + 'notifications': [ + notification.json_format() + for notification in messages.get('Messages', []) + ], + 'warnings': [ + warning.json_format() + for warning in messages.get('Warnings', []) + ], + 'more_cc': + executer.more_cc, + }, + sort_keys=True) + + # Write json result to stdout if json_output is '-'. Otherwise, write + # output string to stdout and json result to the file specified by + # json_output + if json_output == '-': + sys.stdout.write('**** Presubmit Results ****\n') + sys.stdout.write(presubmit_results_json) + sys.stdout.write('\n**** End of Presubmit Results ****\n') + else: + # Print the different message types in a consistent order. ERRORS go + # last so that they will be most visible in the local-presubmit output. + for name in ['Messages', 'Warnings', 'ERRORS']: + if name in messages: + items = messages[name] + sys.stdout.write('** Presubmit %s: %d **\n' % + (name, len(items))) + for item in items: + item.handle() + sys.stdout.write('\n') + if json_output: + gclient_utils.FileWrite(json_output, presubmit_results_json) total_time = time_time() - start_time if total_time > 1.0: @@ -2039,26 +2066,6 @@ def DoPresubmitChecks(change, else: sys.stdout.write('There were presubmit errors.\n') - if json_output: - # Write the presubmit results to json output - presubmit_results = { - 'errors': - [error.json_format() for error in messages.get('ERRORS', [])], - 'notifications': [ - notification.json_format() - for notification in messages.get('Messages', []) - ], - 'warnings': [ - warning.json_format() - for warning in messages.get('Warnings', []) - ], - 'more_cc': - executer.more_cc, - } - - gclient_utils.FileWrite( - json_output, json.dumps(presubmit_results, sort_keys=True)) - global _ASKED_FOR_FEEDBACK # Ask for feedback one time out of 5. if (results and random.randint(0, 4) == 0 and not _ASKED_FOR_FEEDBACK): @@ -2399,7 +2406,8 @@ def main(argv=None): help='Run all tests specified by input_api.RunTests in ' 'all PRESUBMIT files in parallel.') parser.add_argument('--json_output', - help='Write presubmit errors to json output.') + help='Write presubmit results to json output. If \'-\' ' + 'is provided, the results will be writting to stdout.') parser.add_argument('--all_files', action='store_true', help='Mark all files under source control as modified.')