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 untabify
11
-
12
-
13
8
def get_python_source_dir ():
14
9
src_dir = sysconfig .get_config_var ('abs_srcdir' )
15
10
if not src_dir :
16
11
src_dir = sysconfig .get_config_var ('srcdir' )
17
12
return os .path .abspath (src_dir )
18
-
19
-
20
- # Excluded directories which are copies of external libraries:
21
- # don't check their coding style
22
- EXCLUDE_DIRS = [
23
- os .path .join ('Modules' , '_decimal' , 'libmpdec' ),
24
- os .path .join ('Modules' , 'expat' ),
25
- os .path .join ('Modules' , 'zlib' ),
26
- ]
27
13
SRCDIR = get_python_source_dir ()
28
14
29
15
@@ -154,47 +140,8 @@ def changed_files(base_branch=None):
154
140
else :
155
141
sys .exit ('need a git checkout to get modified files' )
156
142
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
- #: Python files that have tabs by design:
181
- _PYTHON_FILES_WITH_TABS = frozenset ({
182
- 'Tools/c-analyzer/cpython/_parser.py' ,
183
- })
184
-
185
-
186
- @status ("Fixing C file whitespace" , info = report_modified_files )
187
- def normalize_c_whitespace (file_paths ):
188
- """Report if any C files """
189
- fixed = []
190
- for path in file_paths :
191
- abspath = os .path .join (SRCDIR , path )
192
- with open (abspath , 'r' ) as f :
193
- if '\t ' not in f .read ():
194
- continue
195
- untabify .process (abspath , 8 , verbose = False )
196
- fixed .append (path )
197
- return fixed
143
+ # Normalize the path to be able to match using str.startswith()
144
+ return list (map (os .path .normpath , filenames ))
198
145
199
146
200
147
@status ("Docs modified" , modal = True )
@@ -234,33 +181,12 @@ def regenerated_pyconfig_h_in(file_paths):
234
181
return "not needed"
235
182
236
183
237
- def ci (pull_request ):
238
- if pull_request == 'false' :
239
- print ('Not a pull request; skipping' )
240
- return
241
- base_branch = get_base_branch ()
242
- file_paths = changed_files (base_branch )
243
- c_files = [fn for fn in file_paths if fn .endswith (('.c' , '.h' ))]
244
- fixed = []
245
- fixed .extend (normalize_c_whitespace (c_files ))
246
- if not fixed :
247
- print ('No whitespace issues found' )
248
- else :
249
- count = len (fixed )
250
- print (f'Please fix the { n_files_str (count )} with whitespace issues' )
251
- print ('(on Unix you can run `make patchcheck` to make the fixes)' )
252
- sys .exit (1 )
253
-
254
-
255
184
def main ():
256
185
base_branch = get_base_branch ()
257
186
file_paths = changed_files (base_branch )
258
- c_files = [fn for fn in file_paths if fn .endswith (('.c' , '.h' ))]
259
187
doc_files = [fn for fn in file_paths if fn .startswith ('Doc' ) and
260
188
fn .endswith (('.rst' , '.inc' ))]
261
189
misc_files = {p for p in file_paths if p .startswith ('Misc' )}
262
- # C rules enforcement.
263
- normalize_c_whitespace (c_files )
264
190
# Docs updated.
265
191
docs_modified (doc_files )
266
192
# Misc/ACKS changed.
@@ -283,12 +209,4 @@ def main():
283
209
284
210
285
211
if __name__ == '__main__' :
286
- import argparse
287
- parser = argparse .ArgumentParser (description = __doc__ )
288
- parser .add_argument ('--ci' ,
289
- help = 'Perform pass/fail checks' )
290
- args = parser .parse_args ()
291
- if args .ci :
292
- ci (args .ci )
293
- else :
294
- main ()
212
+ main ()
0 commit comments