tdf#131510 fix German autocorrect list for 'daß'
[LibreOffice.git] / bin / find-clang-format.py
blob713174114488bd08dde686876c59b2796b7907ec
1 #!/usr/bin/env python3
3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 import os
10 from difflib import unified_diff
11 from pathlib import Path
12 from subprocess import PIPE, Popen
14 EXCLUDELIST = Path("solenv/clang-format/excludelist")
15 THRESHOLD = os.getenv("CLANG_THRESHOLD", 5)
16 CLANG_BINARY = Path(os.getenv("CLANG_FORMAT", "/opt/lo/bin/clang-format"))
19 def calculate_diff_size(diff):
20 additions, removals = 0, 0
21 # ignore first 2 item in the sequence
22 # which are +++ and ---
23 for line in diff[2:]:
24 if line.startswith("+"):
25 additions += 1
26 elif line.startswith("-"):
27 removals += 1
28 return max((additions, removals))
31 def format_stream(path, *extra_args):
32 process = Popen(
33 [CLANG_BINARY, *extra_args], stdout=PIPE, stderr=PIPE, stdin=PIPE,
35 stdout, stderr = process.communicate(input=path.read_bytes())
36 if stderr:
37 print("<FAIL>", str(path))
38 print(stderr.decode())
39 print("<FAIL>")
40 exit(1)
41 stdout = stdout.decode()
42 return stdout
45 def main(*args):
46 if not CLANG_BINARY.exists():
47 print("Couldn't find clang-format on {!s}".format(CLANG_BINARY))
48 exit(1)
50 for path in EXCLUDELIST.read_text().splitlines():
51 path = Path(path)
52 if not path.exists():
53 continue
55 original = path.read_text()
56 new = format_stream(path, *args)
57 originalsize = len(original.splitlines())
58 diff = unified_diff(original.splitlines(), new.splitlines(), n=0)
59 diffsize = calculate_diff_size(tuple(diff))
60 if diffsize <= (originalsize * 5) // 100:
61 print(path, "(size: {}/{})".format(diffsize, originalsize))
64 if __name__ == "__main__":
65 import sys
67 main(*sys.argv[1:])