16
16
import mmap
17
17
import glob
18
18
import functools
19
+ import distutils .spawn
19
20
20
21
## ==============================================
21
22
## LOGGING CONFIGURATION
44
45
functools .reduce (os .path .join , [CODE_SOURCE_DIR , os .path .pardir , os .path .pardir ])
45
46
)
46
47
47
- CLANG_FORMAT = "clang-format-3.6"
48
+ CLANG_FORMAT = None
48
49
CLANG_FORMAT_FILE = os .path .join (PELOTON_DIR , ".clang-format" )
49
50
50
51
# Other directory paths used are relative to PELOTON_DIR
@@ -132,31 +133,32 @@ def check_format(file_path):
132
133
status = True
133
134
134
135
# 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 :
158
137
LOG .error ("clang-format seems not installed" )
159
138
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
+
160
162
161
163
def check_namespaces (file_path ):
162
164
# only check for src files
@@ -285,6 +287,16 @@ def validate_dir(dir_path):
285
287
#END FOR [os.walk]
286
288
#END VALIDATE_DIR(DIR_PATH)
287
289
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
+
288
300
## ==============================================
289
301
## Main Function
290
302
## ==============================================
@@ -297,6 +309,8 @@ def validate_dir(dir_path):
297
309
298
310
LOG .info ("Running source validator ..." )
299
311
LOG .info ("Peloton root : " + PELOTON_DIR )
312
+
313
+ find_clangformat ()
300
314
301
315
if args .files :
302
316
# Validate just the provided files.
0 commit comments