Navigation Menu

Skip to content

Commit

Permalink
Check formatting without files modification
Browse files Browse the repository at this point in the history
Change-Id: I1145214874ec9ed7d18e93173e9462f096025d35
  • Loading branch information
ltowarek committed Dec 4, 2018
1 parent f58d215 commit 3c97367
Showing 1 changed file with 61 additions and 7 deletions.
68 changes: 61 additions & 7 deletions scripts/format.py
Expand Up @@ -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

Expand All @@ -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):
Expand All @@ -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():
Expand All @@ -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())

0 comments on commit 3c97367

Please sign in to comment.