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/.
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 ---
24 if line
.startswith("+"):
26 elif line
.startswith("-"):
28 return max((additions
, removals
))
31 def format_stream(path
, *extra_args
):
33 [CLANG_BINARY
, *extra_args
], stdout
=PIPE
, stderr
=PIPE
, stdin
=PIPE
,
35 stdout
, stderr
= process
.communicate(input=path
.read_bytes())
37 print("<FAIL>", str(path
))
38 print(stderr
.decode())
41 stdout
= stdout
.decode()
46 if not CLANG_BINARY
.exists():
47 print("Couldn't find clang-format on {!s}".format(CLANG_BINARY
))
50 for path
in EXCLUDELIST
.read_text().splitlines():
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__":