55import subprocess
66import sysconfig
77
8- import reindent
9- import untabify
10-
118
129def get_python_source_dir ():
1310 src_dir = sysconfig .get_config_var ('abs_srcdir' )
@@ -16,13 +13,6 @@ def get_python_source_dir():
1613 return os .path .abspath (src_dir )
1714
1815
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- ]
2616SRCDIR = get_python_source_dir ()
2717
2818
@@ -153,62 +143,7 @@ def changed_files(base_branch=None):
153143 else :
154144 sys .exit ('need a git checkout to get modified files' )
155145
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 ))
212147
213148
214149@status ("Docs modified" , modal = True )
@@ -248,40 +183,14 @@ def regenerated_pyconfig_h_in(file_paths):
248183 return "not needed"
249184
250185
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-
271186def main ():
272187 base_branch = get_base_branch ()
273188 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' )))
278191 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 )
283192 # Docs updated.
284- docs_modified (doc_files )
193+ docs_modified (has_doc_files )
285194 # Misc/ACKS changed.
286195 credit_given (misc_files )
287196 # Misc/NEWS changed.
@@ -292,19 +201,14 @@ def main():
292201 regenerated_pyconfig_h_in (file_paths )
293202
294203 # 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?" )
299211
300212
301213if __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