mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
[3.12] GH-109408: Move the Python file whitespace check from patchcheck to pre-commit (GH-109891) (#110633)
GH-109408: Move the Python file whitespace check from patchcheck to pre-commit (GH-109891)
(cherry picked from commit 08ec4a1dbf
)
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
This commit is contained in:
parent
33450a9628
commit
948576574f
2 changed files with 16 additions and 25 deletions
|
@ -23,6 +23,15 @@ repos:
|
||||||
- id: trailing-whitespace
|
- id: trailing-whitespace
|
||||||
types_or: [c, inc, python, rst]
|
types_or: [c, inc, python, rst]
|
||||||
|
|
||||||
|
- repo: local
|
||||||
|
hooks:
|
||||||
|
- id: python-file-whitespace
|
||||||
|
name: "Check Python file whitespace"
|
||||||
|
entry: 'python Tools/patchcheck/reindent.py --nobackup --newline LF'
|
||||||
|
language: 'system'
|
||||||
|
types: [python]
|
||||||
|
exclude: '^(Lib/test/tokenizedata/|Tools/c-analyzer/cpython/_parser).*$'
|
||||||
|
|
||||||
- repo: https://github.com/sphinx-contrib/sphinx-lint
|
- repo: https://github.com/sphinx-contrib/sphinx-lint
|
||||||
rev: v0.6.8
|
rev: v0.6.8
|
||||||
hooks:
|
hooks:
|
||||||
|
|
|
@ -7,7 +7,6 @@ import os.path
|
||||||
import subprocess
|
import subprocess
|
||||||
import sysconfig
|
import sysconfig
|
||||||
|
|
||||||
import reindent
|
|
||||||
import untabify
|
import untabify
|
||||||
|
|
||||||
|
|
||||||
|
@ -184,21 +183,6 @@ _PYTHON_FILES_WITH_TABS = frozenset({
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
@status("Fixing Python file whitespace", info=report_modified_files)
|
|
||||||
def normalize_whitespace(file_paths):
|
|
||||||
"""Make sure that the whitespace for .py files have been normalized."""
|
|
||||||
reindent.makebackup = False # No need to create backups.
|
|
||||||
fixed = [
|
|
||||||
path for path in file_paths
|
|
||||||
if (
|
|
||||||
path.endswith('.py')
|
|
||||||
and path not in _PYTHON_FILES_WITH_TABS
|
|
||||||
and reindent.check(os.path.join(SRCDIR, path))
|
|
||||||
)
|
|
||||||
]
|
|
||||||
return fixed
|
|
||||||
|
|
||||||
|
|
||||||
@status("Fixing C file whitespace", info=report_modified_files)
|
@status("Fixing C file whitespace", info=report_modified_files)
|
||||||
def normalize_c_whitespace(file_paths):
|
def normalize_c_whitespace(file_paths):
|
||||||
"""Report if any C files """
|
"""Report if any C files """
|
||||||
|
@ -256,10 +240,8 @@ def ci(pull_request):
|
||||||
return
|
return
|
||||||
base_branch = get_base_branch()
|
base_branch = get_base_branch()
|
||||||
file_paths = changed_files(base_branch)
|
file_paths = changed_files(base_branch)
|
||||||
python_files = [fn for fn in file_paths if fn.endswith('.py')]
|
|
||||||
c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))]
|
c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))]
|
||||||
fixed = []
|
fixed = []
|
||||||
fixed.extend(normalize_whitespace(python_files))
|
|
||||||
fixed.extend(normalize_c_whitespace(c_files))
|
fixed.extend(normalize_c_whitespace(c_files))
|
||||||
if not fixed:
|
if not fixed:
|
||||||
print('No whitespace issues found')
|
print('No whitespace issues found')
|
||||||
|
@ -273,13 +255,10 @@ def ci(pull_request):
|
||||||
def main():
|
def main():
|
||||||
base_branch = get_base_branch()
|
base_branch = get_base_branch()
|
||||||
file_paths = changed_files(base_branch)
|
file_paths = changed_files(base_branch)
|
||||||
python_files = [fn for fn in file_paths if fn.endswith('.py')]
|
|
||||||
c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))]
|
c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))]
|
||||||
doc_files = [fn for fn in file_paths if fn.startswith('Doc') and
|
doc_files = [fn for fn in file_paths if fn.startswith('Doc') and
|
||||||
fn.endswith(('.rst', '.inc'))]
|
fn.endswith(('.rst', '.inc'))]
|
||||||
misc_files = {p for p in file_paths if p.startswith('Misc')}
|
misc_files = {p for p in file_paths if p.startswith('Misc')}
|
||||||
# PEP 8 whitespace rules enforcement.
|
|
||||||
normalize_whitespace(python_files)
|
|
||||||
# C rules enforcement.
|
# C rules enforcement.
|
||||||
normalize_c_whitespace(c_files)
|
normalize_c_whitespace(c_files)
|
||||||
# Docs updated.
|
# Docs updated.
|
||||||
|
@ -294,10 +273,13 @@ def main():
|
||||||
regenerated_pyconfig_h_in(file_paths)
|
regenerated_pyconfig_h_in(file_paths)
|
||||||
|
|
||||||
# Test suite run and passed.
|
# Test suite run and passed.
|
||||||
if python_files or c_files:
|
has_c_files = any(fn for fn in file_paths if fn.endswith(('.c', '.h')))
|
||||||
end = " and check for refleaks?" if c_files else "?"
|
has_python_files = any(fn for fn in file_paths if fn.endswith('.py'))
|
||||||
print()
|
print()
|
||||||
print("Did you run the test suite" + end)
|
if has_c_files:
|
||||||
|
print("Did you run the test suite and check for refleaks?")
|
||||||
|
elif has_python_files:
|
||||||
|
print("Did you run the test suite?")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue