diff options
author | Mark Benvenuto <mark.benvenuto@mongodb.com> | 2015-12-14 18:04:36 -0500 |
---|---|---|
committer | Mark Benvenuto <mark.benvenuto@mongodb.com> | 2015-12-23 13:36:10 -0500 |
commit | f5b27f93ab4fd329b8b1d781d441914e159cef05 (patch) | |
tree | 1a4838948e514f495282e24d9388fb7c8a8fe63d /buildscripts/clang_format.py | |
parent | 944ee09a2aa02f6131748258d67f8a673925c10f (diff) | |
download | mongo-f5b27f93ab4fd329b8b1d781d441914e159cef05.tar.gz |
SERVER-21981 clang_format.py should not touch files uncessarily
Diffstat (limited to 'buildscripts/clang_format.py')
-rwxr-xr-x | buildscripts/clang_format.py | 35 |
1 files changed, 21 insertions, 14 deletions
diff --git a/buildscripts/clang_format.py b/buildscripts/clang_format.py index ed07a5d7121..d6b86002769 100755 --- a/buildscripts/clang_format.py +++ b/buildscripts/clang_format.py @@ -275,36 +275,43 @@ class ClangFormat(object): return False - def lint(self, file_name): + def _lint(self, file_name, print_diff): """Check the specified file has the correct format """ - with open(file_name, 'r') as original_text: + with open(file_name, 'rb') as original_text: original_file = original_text.read() # Get formatted file as clang-format would format the file formatted_file = callo([self.path, "--style=file", file_name]) if original_file != formatted_file: - - original_lines = original_file.splitlines() - formatted_lines = formatted_file.splitlines() - result = difflib.unified_diff(original_lines, formatted_lines) - - # Take a lock to ensure diffs do not get mixed - with self.print_lock: - print("ERROR: Found diff for " + file_name) - print("To fix formatting errors, run %s --style=file -i %s" % - (self.path, file_name)) - for line in result: - print(line.rstrip()) + if print_diff: + original_lines = original_file.splitlines() + formatted_lines = formatted_file.splitlines() + result = difflib.unified_diff(original_lines, formatted_lines) + + # Take a lock to ensure diffs do not get mixed when printed to the screen + with self.print_lock: + print("ERROR: Found diff for " + file_name) + print("To fix formatting errors, run %s --style=file -i %s" % + (self.path, file_name)) + for line in result: + print(line.rstrip()) return False return True + def lint(self, file_name): + """Check the specified file has the correct format + """ + return self._lint(file_name, print_diff=True) + def format(self, file_name): """Update the format of the specified file """ + if self._lint(file_name, print_diff=False): + return True # Update the file with clang-format return not subprocess.call([self.path, "--style=file", "-i", file_name]) |