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