[Libreoffice-commits] core.git: bin/find-clang-format.py

Batuhan Taskaya (via logerrit) logerrit at kemper.freedesktop.org
Mon Jan 27 18:10:13 UTC 2020


 bin/find-clang-format.py |   67 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 67 insertions(+)

New commits:
commit 09680b09b2f49e7a37d8b941822f053b5179bf6d
Author:     Batuhan Taskaya <isidentical at gmail.com>
AuthorDate: Sun Jan 26 20:09:35 2020 +0300
Commit:     Noel Grandin <noel.grandin at collabora.co.uk>
CommitDate: Mon Jan 27 19:09:19 2020 +0100

    tdf#123936: bin/find-clang-format.py for finding files to be formatted
    
    - bin/find-clang-format.py created for finding ignored files that can
    be formatted with clang-format, outputs to stdout with this format:
    
            <path> (size: <diffsize>/<total file length>)
            <diffsize>: amount of additions or removals, depends on which
            one is bigger
    
    - An environment variable can be used to set threshold (CLANG_THRESHOLD),
    default for that variable is %5.
    
    - Script automatically passes given arguments to the clang-format
    
    Change-Id: I63651fdd1ed2d2354546726cac7560db4b77381f
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/87460
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.grandin at collabora.co.uk>

diff --git a/bin/find-clang-format.py b/bin/find-clang-format.py
new file mode 100755
index 000000000000..067b5b0899be
--- /dev/null
+++ b/bin/find-clang-format.py
@@ -0,0 +1,67 @@
+#!/usr/bin/env python3
+
+# This file is part of the LibreOffice project.
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+import os
+from difflib import unified_diff
+from pathlib import Path
+from subprocess import PIPE, Popen
+
+BLACKLIST = Path("solenv/clang-format/blacklist")
+THRESHOLD = os.getenv("CLANG_THRESHOLD", 5)
+CLANG_BINARY = Path(os.getenv("CLANG_FORMAT", "/opt/lo/bin/clang-format"))
+
+
+def calculate_diff_size(diff):
+    additions, removals = 0, 0
+    # ignore first 2 item in the sequnece
+    # which are +++ and ---
+    for line in diff[2:]:
+        if line.startswith("+"):
+            additions += 1
+        elif line.startswith("-"):
+            removals += 1
+    return max((additions, removals))
+
+
+def format_stream(path, *extra_args):
+    process = Popen(
+        [CLANG_BINARY, *extra_args], stdout=PIPE, stderr=PIPE, stdin=PIPE,
+    )
+    stdout, stderr = process.communicate(input=path.read_bytes())
+    if stderr:
+        print("<FAIL>", str(path))
+        print(stderr.decode())
+        print("<FAIL>")
+        exit(1)
+    stdout = stdout.decode()
+    return stdout
+
+
+def main(*args):
+    if not CLANG_BINARY.exists():
+        print("Couldn't find clang-format on {!s}".format(CLANG_BINARY))
+        exit(1)
+
+    for path in BLACKLIST.read_text().splitlines():
+        path = Path(path)
+        if not path.exists():
+            continue
+
+        original = path.read_text()
+        new = format_stream(path, *args)
+        originalsize = len(original.splitlines())
+        diff = unified_diff(original.splitlines(), new.splitlines(), n=0)
+        diffsize = calculate_diff_size(tuple(diff))
+        if diffsize <= (originalsize * 5) // 100:
+            print(path, "(size: {}/{})".format(diffsize, originalsize))
+
+
+if __name__ == "__main__":
+    import sys
+
+    main(*sys.argv[1:])


More information about the Libreoffice-commits mailing list