3
3
4
4
def preprocess (compiler , file_contents ):
5
5
""" Get output from the preprocessing pass on a file. """
6
- return subprocess .Popen (
6
+ output = subprocess .Popen (
7
7
[compiler , '-E' , '-' ],
8
8
stdout = subprocess .PIPE ,
9
9
stderr = subprocess .PIPE ,
10
10
stdin = subprocess .PIPE ).communicate (input = file_contents )[0 ]
11
11
12
+ def should_keep (line ):
13
+ return str .strip (line ) and line [0 ] != '#'
14
+
15
+ return filter (should_keep , output .splitlines ())
16
+
12
17
13
18
def preprocess_file (compiler , filename ):
14
19
""" Open a file and get the preprocessor output. """
15
20
with open (filename , 'rb' ) as f :
16
21
return preprocess (compiler , f .read ())
17
22
18
23
19
- def remove_empty_lines (text ):
20
- """ Remove empty lines from text. """
21
- return '\n ' .join (filter (None , text .splitlines ()))
22
-
23
-
24
24
def file_contents_from_branch (filename , branch ):
25
25
""" Get a copy of a file from another branch and return its contents. """
26
26
return subprocess .check_output (
@@ -30,21 +30,16 @@ def file_contents_from_branch(filename, branch):
30
30
def equal_to_file_on_branch (filename , branch , compiler ):
31
31
"""
32
32
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.
35
34
"""
36
35
with open (filename , 'rb' ) as f :
37
36
def p (text ):
38
37
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 = '' )
48
43
49
44
50
45
def is_source (filename ):
@@ -58,8 +53,9 @@ def process(tup):
58
53
Check a single file, and return its name if the check fails, otherwise
59
54
return None.
60
55
"""
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
63
59
64
60
65
61
def main ():
@@ -72,7 +68,7 @@ def main():
72
68
'--branch' , type = str , default = 'upstream/master' ,
73
69
help = 'The branch to compare' )
74
70
parser .add_argument (
75
- '--compiler' , type = str , default = 'gcc ' ,
71
+ '--compiler' , type = str , default = 'g++ ' ,
76
72
help = 'The compiler to use' )
77
73
args = parser .parse_args ()
78
74
@@ -85,10 +81,15 @@ def main():
85
81
itertools .cycle ([args .branch ]),
86
82
itertools .cycle ([args .compiler ]))
87
83
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 ()
89
90
90
91
if results :
91
- print ('\n ' .join (results ))
92
+ print ('\n \n ' .join (results ))
92
93
return 1
93
94
94
95
return 0
0 commit comments