2021-01-31 21:50:21 +01:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2021-04-14 10:22:02 +02:00
|
|
|
import difflib
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2021-01-09 08:08:50 +01:00
|
|
|
|
|
|
|
clang_format = sys.argv[1]
|
|
|
|
workspace = os.environ.get("BUILD_WORKSPACE_DIRECTORY", "")
|
|
|
|
want_fix = bool(workspace)
|
|
|
|
|
|
|
|
found_bad = False
|
|
|
|
for path in sys.argv[2:]:
|
|
|
|
with open(path) as file:
|
|
|
|
orig = file.read()
|
|
|
|
new = subprocess.check_output(
|
|
|
|
# [clang_format, "--style={'BasedOnStyle': 'google', 'IndentWidth': 4}", path]
|
|
|
|
[clang_format, "--style=google", path]
|
|
|
|
).decode("utf-8")
|
|
|
|
if orig != new:
|
|
|
|
if want_fix:
|
2021-01-09 23:48:20 +01:00
|
|
|
with open(os.path.join(workspace, path), "w", newline="\n") as file:
|
2021-01-09 08:08:50 +01:00
|
|
|
file.write(new)
|
|
|
|
print("fixed", path)
|
|
|
|
else:
|
|
|
|
print(f"Bad formatting in {path}")
|
|
|
|
print(
|
|
|
|
"\n".join(
|
|
|
|
difflib.unified_diff(
|
|
|
|
orig.splitlines(),
|
|
|
|
new.splitlines(),
|
|
|
|
fromfile="bad",
|
|
|
|
tofile="good",
|
|
|
|
lineterm="",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
found_bad = True
|
|
|
|
|
|
|
|
if found_bad:
|
2021-01-31 21:50:21 +01:00
|
|
|
sys.exit(1)
|