forked from HEP-FCC/FCCSW
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkformat
executable file
·81 lines (67 loc) · 2.76 KB
/
checkformat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python
from __future__ import print_function
import argparse
import os
import subprocess
import sys
sys.path.append('/cvmfs/fcc.cern.ch/sw/0.8pre/SAS/0.1.4/x86_64-slc6-clang3.6-opt/scripts')
import common
import pprint
def parse_args():
parser = argparse.ArgumentParser("checkformat", formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('filenames', metavar='filename', type=str, nargs="+", help='The files or folders you want to check')
parser.add_argument('--inplace', '-i', action='store_true', help='Do editing in place', default=False)
parser.add_argument('--force', '-f', action='store_true', help='Do not ask when editing in place', default=False)
return parser.parse_args()
def set_env():
llvm = "/cvmfs/sft.cern.ch/lcg/contrib/llvm/3.9/x86_64-slc6-gcc49-opt/"
os.environ["LD_LIBRARY_PATH"] = llvm+"lib64:"+os.environ["LD_LIBRARY_PATH"]
os.environ["PATH"] = llvm+"bin:"+os.environ["PATH"]
def run_sas(fnames):
for fname in fnames:
common.CheckFormattingRules(fname)
def confirmed_by_user(fnames):
valid = {"yes": True, "y": True, "ye": True,
"no": False, "n": False}
default = "no"
while True:
print("Please make sure you committed your changes and later amend the changes done by clang format.")
print("Files to be changed:")
pprint.pprint(fnames)
print("Override your files? [y/N]")
choice = raw_input().lower()
if default is not None and choice == '':
return valid[default]
elif choice in valid:
return valid[choice]
else:
print("Please respond with 'yes' or 'no' (or 'y' or 'n').")
def replace(fnames, force):
if force or confirmed_by_user(fnames):
for fname in fnames:
try:
subprocess.check_call(["clang-format", "-i", fname])
pass
except subprocess.CalledProcessError:
print("Problem calling clang-format.")
def scan_dirs(directories):
fnames = []
for dirname in directories:
if os.path.isfile(dirname):
fnames.append(dirname)
elif os.path.isdir(dirname):
for root, dirs, files in os.walk(dirname):
for f in files:
if os.path.splitext(f)[1] in common._SourceFilesExtensions:
fnames += [os.path.join(root, f)]
else:
raise ValueError("File / directory does not exist: " + dirname)
return fnames
if __name__ == "__main__":
args = parse_args()
fnames = scan_dirs(args.filenames)
set_env()
if not args.inplace: # run sas format checker
run_sas(fnames)
else: # replace files using clang-format
replace(fnames, args.force)