From 3c97367f8aa141b6e8d1bdafd8d9fdb0b69d45f8 Mon Sep 17 00:00:00 2001 From: Lukasz Towarek Date: Mon, 3 Dec 2018 10:23:27 +0100 Subject: [PATCH] Check formatting without files modification Change-Id: I1145214874ec9ed7d18e93173e9462f096025d35 --- scripts/format.py | 68 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 61 insertions(+), 7 deletions(-) diff --git a/scripts/format.py b/scripts/format.py index 5093480..5c3ef96 100644 --- a/scripts/format.py +++ b/scripts/format.py @@ -21,10 +21,12 @@ # import argparse +import difflib import glob import logging import os import subprocess +import sys from concurrent.futures import ThreadPoolExecutor from itertools import repeat @@ -38,12 +40,48 @@ def main(): logging.info('Finding source files') files = find_source_files(args.root_directory) + if args.check: + return check_formatting(files, args.clang_format) + else: + return format_files(files, args.clang_format) + + +def check_formatting(files, clang_format): + logging.info('Checking formatting') + with ThreadPoolExecutor() as executor: + logging.info('Reading expected values') + expected = executor.map(run_clang_format_star, + zip(files, + repeat(clang_format))) + + logging.info('Reading actual values') + actual = executor.map(read_file, files) + + logging.info('Comparing') + diff_results = executor.map(diff_star, zip(actual, expected)) + + diffs = [] + for f, d in zip(files, diff_results): + diff_str = '\n'.join(d) + if diff_str: + logging.warning('Diff in {}:\n{}'.format(f, diff_str)) + diffs.append(f) + else: + logging.debug('No diff in {}'.format(f)) + + logging.info('Formatting checked') + logging.info('Diff count: {}'.format(len(diffs))) + return len(diffs) + + +def format_files(files, clang_format): logging.info('Running clang-format') with ThreadPoolExecutor() as executor: executor.map(run_clang_format_star, - zip(files, repeat(args.clang_format))) - + zip(files, repeat(clang_format), + repeat('-i'))) logging.info('Formatting is done') + return 0 def find_source_files(root_path): @@ -57,12 +95,26 @@ def find_source_files(root_path): def run_clang_format_star(args): - run_clang_format(*args) + return run_clang_format(*args) + + +def run_clang_format(f, command='clang-format', arguments=''): + cmd = '{} {} {}'.format(command, arguments, f) + return subprocess.run(cmd, shell=True, check=True, stdout=subprocess.PIPE, + stderr=subprocess.STDOUT).stdout.decode() + + +def read_file(f): + with open(f) as f_content: + return f_content.read() + + +def diff_star(args): + return diff(*args) -def run_clang_format(f, command='clang-format'): - cmd = '{} -i {}'.format(command, f) - subprocess.run(cmd, shell=True, check=True) +def diff(actual, expected): + return difflib.unified_diff(actual.splitlines(), expected.splitlines()) def process_command_line(): @@ -71,8 +123,10 @@ def process_command_line(): help='Root directory of compute-samples source') parser.add_argument('--clang-format', default='clang-format', help='clang-format command') + parser.add_argument('--check', action='store_true', + help='Check formatting') return parser.parse_args() if __name__ == '__main__': - main() + sys.exit(main())