11#!/usr/bin/env python3
22"""Check proposed changes for common issues."""
3- import re
43import sys
5- import shutil
64import os .path
75import subprocess
86import sysconfig
97
10- import untabify
11-
12-
138def get_python_source_dir ():
149 src_dir = sysconfig .get_config_var ('abs_srcdir' )
1510 if not src_dir :
1611 src_dir = sysconfig .get_config_var ('srcdir' )
1712 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- ]
2713SRCDIR = get_python_source_dir ()
2814
2915
@@ -154,47 +140,8 @@ def changed_files(base_branch=None):
154140 else :
155141 sys .exit ('need a git checkout to get modified files' )
156142
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 ))
198145
199146
200147@status ("Docs modified" , modal = True )
@@ -234,33 +181,12 @@ def regenerated_pyconfig_h_in(file_paths):
234181 return "not needed"
235182
236183
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-
255184def main ():
256185 base_branch = get_base_branch ()
257186 file_paths = changed_files (base_branch )
258- c_files = [fn for fn in file_paths if fn .endswith (('.c' , '.h' ))]
259187 doc_files = [fn for fn in file_paths if fn .startswith ('Doc' ) and
260188 fn .endswith (('.rst' , '.inc' ))]
261189 misc_files = {p for p in file_paths if p .startswith ('Misc' )}
262- # C rules enforcement.
263- normalize_c_whitespace (c_files )
264190 # Docs updated.
265191 docs_modified (doc_files )
266192 # Misc/ACKS changed.
@@ -283,12 +209,4 @@ def main():
283209
284210
285211if __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