Newer
Older
#!/usr/bin/env python3
"""
Run clang-format with the style file in the CORSIKA repository.
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.
"""
import argparse
import subprocess as subp
import os
import sys
do_progress = False
try:
from progress.bar import ChargingBar
do_progress = True
except ImportError as e:
do_progress = False
# no progress bar
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.")
parser.add_argument("--docker", action="store_true",
help="Use corsika/devel:clang-8 container to run clang-format. Make sure you are in group \"docker\". ")
excludeDirs = [r"^(\./)?modules/", r"^(\./)?externals/", r"^(\./)?build", r"^(\./)?install",
r"(\./)?\.git", r"^(\./)?corsika/framework/units",
r"^(\./)?Random123/"]
filelist = []
if args.all:
for dirpath, dirnames, filenames in os.walk("."):
if (f.endswith(".hpp") or f.endswith(".cpp") or f.endswith(".inl")):
filename = os.path.join(dirpath, f)
if not os.path.islink(filename):
filelist.append(filename)
if not filelist:
raise SystemExit("Error: You specified --all, but file list is empty. "
"Did you run from the build directory?")
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())
filelist2 = subp.check_output(cmd, shell=True).decode("utf8").strip().split("\n")
filelist += filelist2
# some cleanup
filelist_clean = []
for f in filelist:
if not (f.endswith(".hpp") or f.endswith(".cpp") or f.endswith(".inl")):
continue
if os.path.islink(f):
continue
excl = False
for excl_dir in excludeDirs:
if re.findall(excl_dir, f):
excl = True
break
if excl:
continue
filelist_clean.append(f)
filelist = filelist_clean
cmd = "clang-format"
if "CLANG_FORMAT" in os.environ:
cmd = os.environ["CLANG_FORMAT"]
if args.docker:
USER=os.environ["USER"]
UID=os.getuid()
GID=os.getgid()
PWD=os.getcwd()
# note, currently in container it is clang-8
cmd = "docker container run --rm -v {}:/corsika -w /corsika -u {}:{} corsika/devel:clang-8 clang-format-8".format(PWD,UID,GID)
cmd += " -style=file"
version = subp.check_output(cmd.split() + ["--version"]).decode("utf-8")
print (version)
print ("Note: the clang-format version has an impact on the result. Make sure you are consistent with current CI. Consider \'--docker\' option.")
bar = None
if do_progress:
bar = ChargingBar('Processing', max=len(filelist))
b = open(filename, "rb").read()
if a != b:
changed.append(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)