Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit 19e3f93

Browse files
tcm-marcelapavlo
authored andcommitted
[formatter/validator] Find installed version of clang-format
1 parent 3bea6df commit 19e3f93

File tree

2 files changed

+59
-30
lines changed

2 files changed

+59
-30
lines changed

script/formatting/formatter.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import sys
1313
import datetime
1414
import subprocess
15+
import distutils.spawn
16+
1517

1618
## ==============================================
1719
## CONFIGURATION
@@ -32,7 +34,7 @@
3234
DEFAULT_DIRS.append(PELOTON_SRC_DIR)
3335
DEFAULT_DIRS.append(PELOTON_TESTS_DIR)
3436

35-
CLANG_FORMAT = "clang-format-3.6"
37+
CLANG_FORMAT = None
3638
CLANG_FORMAT_FILE = os.path.join(PELOTON_DIR, ".clang-format")
3739

3840
## ==============================================
@@ -112,13 +114,13 @@ def format_file(file_path, update_header, clang_format_code):
112114
fd.write(file_data)
113115

114116
elif clang_format_code:
115-
try:
116-
formatting_command = CLANG_FORMAT + " -style=file -i " + file_path
117-
LOG.info(formatting_command)
118-
subprocess.call([CLANG_FORMAT, "-style=file", "-i", file_path])
119-
except OSError as e:
117+
if CLANG_FORMAT is None:
120118
LOG.error("clang-format seems not installed")
121119
exit("clang-format seems not installed")
120+
121+
formatting_command = CLANG_FORMAT + " -style=file -i " + file_path
122+
LOG.info(formatting_command)
123+
subprocess.call([CLANG_FORMAT, "-style=file", "-i", file_path])
122124

123125
#END WITH
124126

@@ -140,6 +142,17 @@ def format_dir(dir_path, update_header, clang_format_code):
140142
#END FOR [os.walk]
141143
#END ADD_HEADERS_DIR(DIR_PATH)
142144

145+
#find clang-format executable
146+
def find_clangformat():
147+
global CLANG_FORMAT
148+
#check for possible clang-format versions
149+
for exe in ["clang-format", "clang-format-3.6", "clang-format-3.7", "clang-format-3.8"]:
150+
path = distutils.spawn.find_executable(exe)
151+
if not path is None:
152+
CLANG_FORMAT = path
153+
return
154+
155+
143156
## ==============================================
144157
## Main Function
145158
## ==============================================
@@ -155,6 +168,8 @@ def format_dir(dir_path, update_header, clang_format_code):
155168
help='Files or directories to (recursively) apply the actions to')
156169

157170
args = parser.parse_args()
171+
172+
find_clangformat()
158173

159174
if args.staged_files:
160175
targets = [os.path.abspath(os.path.join(PELOTON_DIR, f)) for f in subprocess.check_output(["git", "diff", "--name-only", "HEAD", "--cached", "--diff-filter=d"]).split()]

script/validators/source_validator.py

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import mmap
1717
import glob
1818
import functools
19+
import distutils.spawn
1920

2021
## ==============================================
2122
## LOGGING CONFIGURATION
@@ -44,7 +45,7 @@
4445
functools.reduce(os.path.join, [CODE_SOURCE_DIR, os.path.pardir, os.path.pardir])
4546
)
4647

47-
CLANG_FORMAT = "clang-format-3.6"
48+
CLANG_FORMAT = None
4849
CLANG_FORMAT_FILE = os.path.join(PELOTON_DIR, ".clang-format")
4950

5051
# Other directory paths used are relative to PELOTON_DIR
@@ -132,31 +133,32 @@ def check_format(file_path):
132133
status = True
133134

134135
# Run clang-format on the file
135-
try:
136-
clang_format_cmd = [CLANG_FORMAT, "-style=file", file_path]
137-
formatted_src = subprocess.check_output(clang_format_cmd).splitlines(True)
138-
# Load source file
139-
with open(file_path, "r") as file:
140-
src = file.readlines()
141-
142-
# Do the diff
143-
d = difflib.Differ()
144-
diff = d.compare(src, formatted_src)
145-
line_num = 0
146-
for line in diff:
147-
code = line[:2]
148-
if code in (" ", "- "):
149-
line_num += 1
150-
if code == '- ':
151-
if status:
152-
LOG.info("Invalid formatting in file : " + file_path)
153-
LOG.info(" Line %d: %s" % (line_num, line[2:].strip()))
154-
status = False
155-
156-
return status
157-
except OSError as e:
136+
if CLANG_FORMAT is None:
158137
LOG.error("clang-format seems not installed")
159138
exit()
139+
140+
clang_format_cmd = [CLANG_FORMAT, "-style=file", file_path]
141+
formatted_src = subprocess.check_output(clang_format_cmd).splitlines(True)
142+
# Load source file
143+
with open(file_path, "r") as file:
144+
src = file.readlines()
145+
146+
# Do the diff
147+
d = difflib.Differ()
148+
diff = d.compare(src, formatted_src)
149+
line_num = 0
150+
for line in diff:
151+
code = line[:2]
152+
if code in (" ", "- "):
153+
line_num += 1
154+
if code == '- ':
155+
if status:
156+
LOG.info("Invalid formatting in file : " + file_path)
157+
LOG.info(" Line %d: %s" % (line_num, line[2:].strip()))
158+
status = False
159+
160+
return status
161+
160162

161163
def check_namespaces(file_path):
162164
# only check for src files
@@ -285,6 +287,16 @@ def validate_dir(dir_path):
285287
#END FOR [os.walk]
286288
#END VALIDATE_DIR(DIR_PATH)
287289

290+
#find clang-format executable
291+
def find_clangformat():
292+
global CLANG_FORMAT
293+
#check for possible clang-format versions
294+
for exe in ["clang-format", "clang-format-3.6", "clang-format-3.7", "clang-format-3.8"]:
295+
path = distutils.spawn.find_executable(exe)
296+
if not path is None:
297+
CLANG_FORMAT = path
298+
return
299+
288300
## ==============================================
289301
## Main Function
290302
## ==============================================
@@ -297,6 +309,8 @@ def validate_dir(dir_path):
297309

298310
LOG.info("Running source validator ...")
299311
LOG.info("Peloton root : " + PELOTON_DIR)
312+
313+
find_clangformat()
300314

301315
if args.files:
302316
# Validate just the provided files.

0 commit comments

Comments
 (0)