Skip to content

Commit 70cb187

Browse files
authored
Check the whitespace of pull requests on Travis (pythonGH-2367)
1 parent 13e96cc commit 70cb187

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ before_script:
8989
fi
9090
9191
script:
92+
# Using the built Python as patchcheck.py is built around the idea of using
93+
# a checkout-build of CPython to know things like what base branch the changes
94+
# should be compared against.
95+
# Only run on Linux as the check only needs to be run once.
96+
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then ./python Tools/scripts/patchcheck.py --travis $TRAVIS_PULL_REQUEST; fi
9297
# `-r -w` implicitly provided through `make buildbottest`.
9398
- make buildbottest TESTOPTS="-j4 -uall,-cpu,-tzdata"
9499

Tools/scripts/patchcheck.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env python3
2+
"""Check proposed changes for common issues."""
23
import re
34
import sys
45
import shutil
@@ -135,7 +136,7 @@ def report_modified_files(file_paths):
135136
return "\n".join(lines)
136137

137138

138-
@status("Fixing whitespace", info=report_modified_files)
139+
@status("Fixing Python file whitespace", info=report_modified_files)
139140
def normalize_whitespace(file_paths):
140141
"""Make sure that the whitespace for .py files have been normalized."""
141142
reindent.makebackup = False # No need to create backups.
@@ -212,6 +213,27 @@ def regenerated_pyconfig_h_in(file_paths):
212213
else:
213214
return "not needed"
214215

216+
def travis(pull_request):
217+
if pull_request == 'false':
218+
print('Not a pull request; skipping')
219+
return
220+
base_branch = get_base_branch()
221+
file_paths = changed_files(base_branch)
222+
python_files = [fn for fn in file_paths if fn.endswith('.py')]
223+
c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))]
224+
doc_files = [fn for fn in file_paths if fn.startswith('Doc') and
225+
fn.endswith(('.rst', '.inc'))]
226+
fixed = []
227+
fixed.extend(normalize_whitespace(python_files))
228+
fixed.extend(normalize_c_whitespace(c_files))
229+
fixed.extend(normalize_docs_whitespace(doc_files))
230+
if not fixed:
231+
print('No whitespace issues found')
232+
else:
233+
print(f'Please fix the {len(fixed)} file(s) with whitespace issues')
234+
print('(on UNIX you can run `make patchcheck` to make the fixes)')
235+
sys.exit(1)
236+
215237
def main():
216238
base_branch = get_base_branch()
217239
file_paths = changed_files(base_branch)
@@ -246,4 +268,12 @@ def main():
246268

247269

248270
if __name__ == '__main__':
249-
main()
271+
import argparse
272+
parser = argparse.ArgumentParser(description=__doc__)
273+
parser.add_argument('--travis',
274+
help='Perform pass/fail checks')
275+
args = parser.parse_args()
276+
if args.travis:
277+
travis(args.travis)
278+
else:
279+
main()

0 commit comments

Comments
 (0)