1
1
#!/usr/bin/env python3
2
2
"""Check proposed changes for common issues."""
3
- import re
4
3
import sys
5
- import shutil
6
4
import os .path
7
5
import subprocess
8
6
import sysconfig
9
7
10
- import reindent
11
- import untabify
12
-
13
-
14
8
def get_python_source_dir ():
15
9
src_dir = sysconfig .get_config_var ('abs_srcdir' )
16
10
if not src_dir :
17
11
src_dir = sysconfig .get_config_var ('srcdir' )
18
12
return os .path .abspath (src_dir )
19
-
20
-
21
- # Excluded directories which are copies of external libraries:
22
- # don't check their coding style
23
- EXCLUDE_DIRS = [os .path .join ('Modules' , '_ctypes' , 'libffi_osx' ),
24
- os .path .join ('Modules' , '_ctypes' , 'libffi_msvc' ),
25
- os .path .join ('Modules' , '_decimal' , 'libmpdec' ),
26
- os .path .join ('Modules' , 'expat' ),
27
- os .path .join ('Modules' , 'zlib' )]
28
13
SRCDIR = get_python_source_dir ()
29
14
30
15
@@ -154,50 +139,8 @@ def changed_files(base_branch=None):
154
139
else :
155
140
sys .exit ('need a git checkout to get modified files' )
156
141
157
- filenames2 = []
158
- for filename in filenames :
159
- # Normalize the path to be able to match using .startswith()
160
- filename = os .path .normpath (filename )
161
- if any (filename .startswith (path ) for path in EXCLUDE_DIRS ):
162
- # Exclude the file
163
- continue
164
- filenames2 .append (filename )
165
-
166
- return filenames2
167
-
168
-
169
- def report_modified_files (file_paths ):
170
- count = len (file_paths )
171
- if count == 0 :
172
- return n_files_str (count )
173
- else :
174
- lines = [f"{ n_files_str (count )} :" ]
175
- for path in file_paths :
176
- lines .append (f" { path } " )
177
- return "\n " .join (lines )
178
-
179
-
180
- @status ("Fixing Python file whitespace" , info = report_modified_files )
181
- def normalize_whitespace (file_paths ):
182
- """Make sure that the whitespace for .py files have been normalized."""
183
- reindent .makebackup = False # No need to create backups.
184
- fixed = [path for path in file_paths if path .endswith ('.py' ) and
185
- reindent .check (os .path .join (SRCDIR , path ))]
186
- return fixed
187
-
188
-
189
- @status ("Fixing C file whitespace" , info = report_modified_files )
190
- def normalize_c_whitespace (file_paths ):
191
- """Report if any C files """
192
- fixed = []
193
- for path in file_paths :
194
- abspath = os .path .join (SRCDIR , path )
195
- with open (abspath , 'r' ) as f :
196
- if '\t ' not in f .read ():
197
- continue
198
- untabify .process (abspath , 8 , verbose = False )
199
- fixed .append (path )
200
- return fixed
142
+ # Normalize the path to be able to match using str.startswith()
143
+ return list (map (os .path .normpath , filenames ))
201
144
202
145
203
146
@status ("Docs modified" , modal = True )
@@ -237,38 +180,12 @@ def regenerated_pyconfig_h_in(file_paths):
237
180
return "not needed"
238
181
239
182
240
- def ci (pull_request ):
241
- if pull_request == 'false' :
242
- print ('Not a pull request; skipping' )
243
- return
244
- base_branch = get_base_branch ()
245
- file_paths = changed_files (base_branch )
246
- python_files = [fn for fn in file_paths if fn .endswith ('.py' )]
247
- c_files = [fn for fn in file_paths if fn .endswith (('.c' , '.h' ))]
248
- fixed = []
249
- fixed .extend (normalize_whitespace (python_files ))
250
- fixed .extend (normalize_c_whitespace (c_files ))
251
- if not fixed :
252
- print ('No whitespace issues found' )
253
- else :
254
- count = len (fixed )
255
- print (f'Please fix the { n_files_str (count )} with whitespace issues' )
256
- print ('(on Unix you can run `make patchcheck` to make the fixes)' )
257
- sys .exit (1 )
258
-
259
-
260
183
def main ():
261
184
base_branch = get_base_branch ()
262
185
file_paths = changed_files (base_branch )
263
- python_files = [fn for fn in file_paths if fn .endswith ('.py' )]
264
- c_files = [fn for fn in file_paths if fn .endswith (('.c' , '.h' ))]
265
186
doc_files = [fn for fn in file_paths if fn .startswith ('Doc' ) and
266
187
fn .endswith (('.rst' , '.inc' ))]
267
188
misc_files = {p for p in file_paths if p .startswith ('Misc' )}
268
- # PEP 8 whitespace rules enforcement.
269
- normalize_whitespace (python_files )
270
- # C rules enforcement.
271
- normalize_c_whitespace (c_files )
272
189
# Docs updated.
273
190
docs_modified (doc_files )
274
191
# Misc/ACKS changed.
@@ -281,19 +198,14 @@ def main():
281
198
regenerated_pyconfig_h_in (file_paths )
282
199
283
200
# Test suite run and passed.
284
- if python_files or c_files :
285
- end = " and check for refleaks?" if c_files else "?"
286
- print ()
287
- print ("Did you run the test suite" + end )
201
+ has_c_files = any (fn for fn in file_paths if fn .endswith (('.c' , '.h' )))
202
+ has_python_files = any (fn for fn in file_paths if fn .endswith ('.py' ))
203
+ print ()
204
+ if has_c_files :
205
+ print ("Did you run the test suite and check for refleaks?" )
206
+ elif has_python_files :
207
+ print ("Did you run the test suite?" )
288
208
289
209
290
210
if __name__ == '__main__' :
291
- import argparse
292
- parser = argparse .ArgumentParser (description = __doc__ )
293
- parser .add_argument ('--ci' ,
294
- help = 'Perform pass/fail checks' )
295
- args = parser .parse_args ()
296
- if args .ci :
297
- ci (args .ci )
298
- else :
299
- main ()
211
+ main ()
0 commit comments