IAP GITLAB

Skip to content
Snippets Groups Projects
do-clang-format.py 2.49 KiB
Newer Older
Hans Dembinski's avatar
Hans Dembinski committed
#!/usr/bin/env python3
"""
Run clang-format with the style file in the CORSIKA repository.

Hans Dembinski's avatar
Hans Dembinski committed
By default it finds new files and files with modifications with respect to the current master and prints the filenames which need clang-formatting. Returns 1 if there are files which need modifications and 0 otherwise, so it can be used as a test.
Hans Dembinski's avatar
Hans Dembinski committed
"""
import argparse
import subprocess as subp
import os
import sys

parser = argparse.ArgumentParser(description=__doc__)
Hans Dembinski's avatar
Hans Dembinski committed
parser.add_argument('--apply', action="store_true",
    help="Apply clang-format to files which need changes.")
parser.add_argument("--all", action="store_true",
    help="Check all files below current path instead of new/modified.")
Hans Dembinski's avatar
Hans Dembinski committed

args = parser.parse_args()

filelist = []
if args.all:
    for dirpath, dirnames, filenames in os.walk("."):
ralfulrich's avatar
ralfulrich committed
        excl = False
        excl_dirs = ["ThirdParty", "build", "externals"]
        for excl_dir in excl_dirs:
            if excl_dir in dirpath:
                excl = True
        if excl: continue
Hans Dembinski's avatar
Hans Dembinski committed
        for f in filenames:
ralfulrich's avatar
ralfulrich committed
            if f.endswith(".hpp") or f.endswith(".cpp"):
Hans Dembinski's avatar
Hans Dembinski committed
                filename = os.path.join(dirpath, f)
                if not os.path.islink(filename):
                    filelist.append(filename)
    if not filelist:
Hans Dembinski's avatar
Hans Dembinski committed
        raise SystemExit("Error: You specified --all, but file list is empty. "
                         "Did you run from the build directory?")
Hans Dembinski's avatar
Hans Dembinski committed
else:
    cmd = "git diff master --name-status"
    for line in subp.check_output(cmd, shell=True).decode("utf8").strip().split("\n"):
        if line.startswith("D"): continue
        if line.startswith("R"):
            filelist.append(line.split()[-1])
        else:
            filelist.append(line[1:].lstrip())
Hans Dembinski's avatar
Hans Dembinski committed

    cmd = "git ls-files --exclude-standard --others"
    filelist += subp.check_output(cmd, shell=True).decode("utf8").strip().split("\n")
Hans Dembinski's avatar
Hans Dembinski committed
    filelist = [x for x in filelist
                if "ThirdParty" not in x and (x.endswith(".h") or x.endswith(".cc"))]
Hans Dembinski's avatar
Hans Dembinski committed

cmd = "clang-format"
if "CLANG_FORMAT" in os.environ:
  cmd = os.environ["CLANG_FORMAT"]
cmd +=  " -style=file"
Hans Dembinski's avatar
Hans Dembinski committed
if args.apply:
    for filename in filelist:
        subp.check_call(cmd.split() + ["-i", filename])
else:
    # only print files which need formatting
    files_need_formatting = 0
    for filename in filelist:
        a = open(filename, "rb").read()
        b = subp.check_output(cmd.split() + [filename])
        if a != b:
            files_need_formatting += 1
            print(filename)
    sys.exit(1 if files_need_formatting > 0 else 0)