33
44def preprocess (compiler , file_contents ):
55 """ Get output from the preprocessing pass on a file. """
6- return subprocess .Popen (
6+ output = subprocess .Popen (
77 [compiler , '-E' , '-' ],
88 stdout = subprocess .PIPE ,
99 stderr = subprocess .PIPE ,
1010 stdin = subprocess .PIPE ).communicate (input = file_contents )[0 ]
1111
12+ def should_keep (line ):
13+ return str .strip (line ) and line [0 ] != '#'
14+
15+ return filter (should_keep , output .splitlines ())
16+
1217
1318def preprocess_file (compiler , filename ):
1419 """ Open a file and get the preprocessor output. """
1520 with open (filename , 'rb' ) as f :
1621 return preprocess (compiler , f .read ())
1722
1823
19- def remove_empty_lines (text ):
20- """ Remove empty lines from text. """
21- return '\n ' .join (filter (None , text .splitlines ()))
22-
23-
2424def file_contents_from_branch (filename , branch ):
2525 """ Get a copy of a file from another branch and return its contents. """
2626 return subprocess .check_output (
@@ -30,21 +30,16 @@ def file_contents_from_branch(filename, branch):
3030def equal_to_file_on_branch (filename , branch , compiler ):
3131 """
3232 Open a file on this branch and preprocess it. Preprocess the same file
33- from another branch, and return whether the two files have (for all intents
34- and purposes) the same contents.
33+ from another branch, and return a diff.
3534 """
3635 with open (filename , 'rb' ) as f :
3736 def p (text ):
3837 return preprocess (compiler , text )
39- return (p (f .read ()) ==
40- p (file_contents_from_branch (filename , branch )))
41-
42-
43- def process_single_file (filename , branch , compiler ):
44- """ Like equal_to_file_on_branch, but also checks the file extension. """
45- _ , ext = os .path .splitext (filename )
46- return ((ext == '.h' or ext == '.cpp' ) and
47- not equal_to_file_on_branch (filename , branch , compiler ))
38+ return difflib .unified_diff (p (f .read ()),
39+ p (file_contents_from_branch (filename , branch )),
40+ fromfile = filename ,
41+ tofile = filename ,
42+ lineterm = '' )
4843
4944
5045def is_source (filename ):
@@ -58,8 +53,9 @@ def process(tup):
5853 Check a single file, and return its name if the check fails, otherwise
5954 return None.
6055 """
61- failed = process_single_file (* tup )
62- return file if failed else None
56+ filename , branch , compiler = tup
57+ failed = '\n ' .join (equal_to_file_on_branch (filename , branch , compiler ))
58+ return failed if failed else None
6359
6460
6561def main ():
@@ -72,7 +68,7 @@ def main():
7268 '--branch' , type = str , default = 'upstream/master' ,
7369 help = 'The branch to compare' )
7470 parser .add_argument (
75- '--compiler' , type = str , default = 'gcc ' ,
71+ '--compiler' , type = str , default = 'g++ ' ,
7672 help = 'The compiler to use' )
7773 args = parser .parse_args ()
7874
@@ -85,10 +81,15 @@ def main():
8581 itertools .cycle ([args .branch ]),
8682 itertools .cycle ([args .compiler ]))
8783
88- results = filter (None , multiprocessing .Pool (10 ).map (process , zipped ))
84+ pool = multiprocessing .Pool (10 )
85+
86+ results = filter (None , pool .map (process , zipped ))
87+
88+ pool .close ()
89+ pool .join ()
8990
9091 if results :
91- print ('\n ' .join (results ))
92+ print ('\n \n ' .join (results ))
9293 return 1
9394
9495 return 0
0 commit comments