mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
[3.12] gh-109408: Add the docs whitespace check from patchcheck to pre-commit (GH-109854) (#110594)
gh-109408: Add the docs whitespace check from patchcheck to pre-commit (GH-109854)
(cherry picked from commit 7426ed0347
)
Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
This commit is contained in:
parent
e73210c023
commit
0ffbde4e05
2 changed files with 13 additions and 33 deletions
|
@ -17,7 +17,7 @@ repos:
|
||||||
types: [python]
|
types: [python]
|
||||||
exclude: Lib/test/tokenizedata/coding20731.py
|
exclude: Lib/test/tokenizedata/coding20731.py
|
||||||
- id: trailing-whitespace
|
- id: trailing-whitespace
|
||||||
types_or: [c, python, rst]
|
types_or: [c, inc, python, rst]
|
||||||
|
|
||||||
- repo: https://github.com/sphinx-contrib/sphinx-lint
|
- repo: https://github.com/sphinx-contrib/sphinx-lint
|
||||||
rev: v0.6.8
|
rev: v0.6.8
|
||||||
|
|
|
@ -30,7 +30,8 @@ SRCDIR = get_python_source_dir()
|
||||||
|
|
||||||
def n_files_str(count):
|
def n_files_str(count):
|
||||||
"""Return 'N file(s)' with the proper plurality on 'file'."""
|
"""Return 'N file(s)' with the proper plurality on 'file'."""
|
||||||
return "{} file{}".format(count, "s" if count != 1 else "")
|
s = "s" if count != 1 else ""
|
||||||
|
return f"{count} file{s}"
|
||||||
|
|
||||||
|
|
||||||
def status(message, modal=False, info=None):
|
def status(message, modal=False, info=None):
|
||||||
|
@ -84,7 +85,7 @@ def get_git_remote_default_branch(remote_name):
|
||||||
|
|
||||||
It is typically called 'main', but may differ
|
It is typically called 'main', but may differ
|
||||||
"""
|
"""
|
||||||
cmd = "git remote show {}".format(remote_name).split()
|
cmd = f"git remote show {remote_name}".split()
|
||||||
env = os.environ.copy()
|
env = os.environ.copy()
|
||||||
env['LANG'] = 'C'
|
env['LANG'] = 'C'
|
||||||
try:
|
try:
|
||||||
|
@ -171,9 +172,9 @@ def report_modified_files(file_paths):
|
||||||
if count == 0:
|
if count == 0:
|
||||||
return n_files_str(count)
|
return n_files_str(count)
|
||||||
else:
|
else:
|
||||||
lines = ["{}:".format(n_files_str(count))]
|
lines = [f"{n_files_str(count)}:"]
|
||||||
for path in file_paths:
|
for path in file_paths:
|
||||||
lines.append(" {}".format(path))
|
lines.append(f" {path}")
|
||||||
return "\n".join(lines)
|
return "\n".join(lines)
|
||||||
|
|
||||||
|
|
||||||
|
@ -212,27 +213,6 @@ def normalize_c_whitespace(file_paths):
|
||||||
return fixed
|
return fixed
|
||||||
|
|
||||||
|
|
||||||
ws_re = re.compile(br'\s+(\r?\n)$')
|
|
||||||
|
|
||||||
@status("Fixing docs whitespace", info=report_modified_files)
|
|
||||||
def normalize_docs_whitespace(file_paths):
|
|
||||||
fixed = []
|
|
||||||
for path in file_paths:
|
|
||||||
abspath = os.path.join(SRCDIR, path)
|
|
||||||
try:
|
|
||||||
with open(abspath, 'rb') as f:
|
|
||||||
lines = f.readlines()
|
|
||||||
new_lines = [ws_re.sub(br'\1', line) for line in lines]
|
|
||||||
if new_lines != lines:
|
|
||||||
shutil.copyfile(abspath, abspath + '.bak')
|
|
||||||
with open(abspath, 'wb') as f:
|
|
||||||
f.writelines(new_lines)
|
|
||||||
fixed.append(path)
|
|
||||||
except Exception as err:
|
|
||||||
print('Cannot fix %s: %s' % (path, err))
|
|
||||||
return fixed
|
|
||||||
|
|
||||||
|
|
||||||
@status("Docs modified", modal=True)
|
@status("Docs modified", modal=True)
|
||||||
def docs_modified(file_paths):
|
def docs_modified(file_paths):
|
||||||
"""Report if any file in the Doc directory has been changed."""
|
"""Report if any file in the Doc directory has been changed."""
|
||||||
|
@ -251,6 +231,7 @@ def reported_news(file_paths):
|
||||||
return any(p.startswith(os.path.join('Misc', 'NEWS.d', 'next'))
|
return any(p.startswith(os.path.join('Misc', 'NEWS.d', 'next'))
|
||||||
for p in file_paths)
|
for p in file_paths)
|
||||||
|
|
||||||
|
|
||||||
@status("configure regenerated", modal=True, info=str)
|
@status("configure regenerated", modal=True, info=str)
|
||||||
def regenerated_configure(file_paths):
|
def regenerated_configure(file_paths):
|
||||||
"""Check if configure has been regenerated."""
|
"""Check if configure has been regenerated."""
|
||||||
|
@ -259,6 +240,7 @@ def regenerated_configure(file_paths):
|
||||||
else:
|
else:
|
||||||
return "not needed"
|
return "not needed"
|
||||||
|
|
||||||
|
|
||||||
@status("pyconfig.h.in regenerated", modal=True, info=str)
|
@status("pyconfig.h.in regenerated", modal=True, info=str)
|
||||||
def regenerated_pyconfig_h_in(file_paths):
|
def regenerated_pyconfig_h_in(file_paths):
|
||||||
"""Check if pyconfig.h.in has been regenerated."""
|
"""Check if pyconfig.h.in has been regenerated."""
|
||||||
|
@ -267,6 +249,7 @@ def regenerated_pyconfig_h_in(file_paths):
|
||||||
else:
|
else:
|
||||||
return "not needed"
|
return "not needed"
|
||||||
|
|
||||||
|
|
||||||
def ci(pull_request):
|
def ci(pull_request):
|
||||||
if pull_request == 'false':
|
if pull_request == 'false':
|
||||||
print('Not a pull request; skipping')
|
print('Not a pull request; skipping')
|
||||||
|
@ -275,19 +258,18 @@ def ci(pull_request):
|
||||||
file_paths = changed_files(base_branch)
|
file_paths = changed_files(base_branch)
|
||||||
python_files = [fn for fn in file_paths if fn.endswith('.py')]
|
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
|
|
||||||
fn.endswith(('.rst', '.inc'))]
|
|
||||||
fixed = []
|
fixed = []
|
||||||
fixed.extend(normalize_whitespace(python_files))
|
fixed.extend(normalize_whitespace(python_files))
|
||||||
fixed.extend(normalize_c_whitespace(c_files))
|
fixed.extend(normalize_c_whitespace(c_files))
|
||||||
fixed.extend(normalize_docs_whitespace(doc_files))
|
|
||||||
if not fixed:
|
if not fixed:
|
||||||
print('No whitespace issues found')
|
print('No whitespace issues found')
|
||||||
else:
|
else:
|
||||||
print(f'Please fix the {len(fixed)} file(s) with whitespace issues')
|
count = len(fixed)
|
||||||
print('(on UNIX you can run `make patchcheck` to make the fixes)')
|
print(f'Please fix the {n_files_str(count)} with whitespace issues')
|
||||||
|
print('(on Unix you can run `make patchcheck` to make the fixes)')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
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)
|
||||||
|
@ -300,8 +282,6 @@ def main():
|
||||||
normalize_whitespace(python_files)
|
normalize_whitespace(python_files)
|
||||||
# C rules enforcement.
|
# C rules enforcement.
|
||||||
normalize_c_whitespace(c_files)
|
normalize_c_whitespace(c_files)
|
||||||
# Doc whitespace enforcement.
|
|
||||||
normalize_docs_whitespace(doc_files)
|
|
||||||
# Docs updated.
|
# Docs updated.
|
||||||
docs_modified(doc_files)
|
docs_modified(doc_files)
|
||||||
# Misc/ACKS changed.
|
# Misc/ACKS changed.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue