Skip to content

Commit d099def

Browse files
miss-islingtonhugovkAlexWaygoodAA-Turner
authored
[3.11] gh-109408: Add the docs whitespace check from patchcheck to pre-commit (GH-109854) (#110595)
gh-109408: Add the docs whitespace check from patchcheck to pre-commit (GH-109854) (cherry picked from commit 7426ed0) Co-authored-by: Hugo van Kemenade <[email protected]> Co-authored-by: Alex Waygood <[email protected]> Co-authored-by: Adam Turner <[email protected]>
1 parent 2943bae commit d099def

File tree

2 files changed

+13
-33
lines changed

2 files changed

+13
-33
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ repos:
1414
exclude: ^Lib/test/test_tomllib/
1515
- id: check-yaml
1616
- id: trailing-whitespace
17-
types_or: [c, python, rst]
17+
types_or: [c, inc, python, rst]
1818

1919
- repo: https://github.com/sphinx-contrib/sphinx-lint
2020
rev: v0.6.8

Tools/scripts/patchcheck.py

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ def get_python_source_dir():
3030

3131
def n_files_str(count):
3232
"""Return 'N file(s)' with the proper plurality on 'file'."""
33-
return "{} file{}".format(count, "s" if count != 1 else "")
33+
s = "s" if count != 1 else ""
34+
return f"{count} file{s}"
3435

3536

3637
def status(message, modal=False, info=None):
@@ -84,7 +85,7 @@ def get_git_remote_default_branch(remote_name):
8485
8586
It is typically called 'main', but may differ
8687
"""
87-
cmd = "git remote show {}".format(remote_name).split()
88+
cmd = f"git remote show {remote_name}".split()
8889
env = os.environ.copy()
8990
env['LANG'] = 'C'
9091
try:
@@ -170,9 +171,9 @@ def report_modified_files(file_paths):
170171
if count == 0:
171172
return n_files_str(count)
172173
else:
173-
lines = ["{}:".format(n_files_str(count))]
174+
lines = [f"{n_files_str(count)}:"]
174175
for path in file_paths:
175-
lines.append(" {}".format(path))
176+
lines.append(f" {path}")
176177
return "\n".join(lines)
177178

178179

@@ -199,27 +200,6 @@ def normalize_c_whitespace(file_paths):
199200
return fixed
200201

201202

202-
ws_re = re.compile(br'\s+(\r?\n)$')
203-
204-
@status("Fixing docs whitespace", info=report_modified_files)
205-
def normalize_docs_whitespace(file_paths):
206-
fixed = []
207-
for path in file_paths:
208-
abspath = os.path.join(SRCDIR, path)
209-
try:
210-
with open(abspath, 'rb') as f:
211-
lines = f.readlines()
212-
new_lines = [ws_re.sub(br'\1', line) for line in lines]
213-
if new_lines != lines:
214-
shutil.copyfile(abspath, abspath + '.bak')
215-
with open(abspath, 'wb') as f:
216-
f.writelines(new_lines)
217-
fixed.append(path)
218-
except Exception as err:
219-
print('Cannot fix %s: %s' % (path, err))
220-
return fixed
221-
222-
223203
@status("Docs modified", modal=True)
224204
def docs_modified(file_paths):
225205
"""Report if any file in the Doc directory has been changed."""
@@ -238,6 +218,7 @@ def reported_news(file_paths):
238218
return any(p.startswith(os.path.join('Misc', 'NEWS.d', 'next'))
239219
for p in file_paths)
240220

221+
241222
@status("configure regenerated", modal=True, info=str)
242223
def regenerated_configure(file_paths):
243224
"""Check if configure has been regenerated."""
@@ -246,6 +227,7 @@ def regenerated_configure(file_paths):
246227
else:
247228
return "not needed"
248229

230+
249231
@status("pyconfig.h.in regenerated", modal=True, info=str)
250232
def regenerated_pyconfig_h_in(file_paths):
251233
"""Check if pyconfig.h.in has been regenerated."""
@@ -254,6 +236,7 @@ def regenerated_pyconfig_h_in(file_paths):
254236
else:
255237
return "not needed"
256238

239+
257240
def ci(pull_request):
258241
if pull_request == 'false':
259242
print('Not a pull request; skipping')
@@ -262,19 +245,18 @@ def ci(pull_request):
262245
file_paths = changed_files(base_branch)
263246
python_files = [fn for fn in file_paths if fn.endswith('.py')]
264247
c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))]
265-
doc_files = [fn for fn in file_paths if fn.startswith('Doc') and
266-
fn.endswith(('.rst', '.inc'))]
267248
fixed = []
268249
fixed.extend(normalize_whitespace(python_files))
269250
fixed.extend(normalize_c_whitespace(c_files))
270-
fixed.extend(normalize_docs_whitespace(doc_files))
271251
if not fixed:
272252
print('No whitespace issues found')
273253
else:
274-
print(f'Please fix the {len(fixed)} file(s) with whitespace issues')
275-
print('(on UNIX you can run `make patchcheck` to make the fixes)')
254+
count = len(fixed)
255+
print(f'Please fix the {n_files_str(count)} with whitespace issues')
256+
print('(on Unix you can run `make patchcheck` to make the fixes)')
276257
sys.exit(1)
277258

259+
278260
def main():
279261
base_branch = get_base_branch()
280262
file_paths = changed_files(base_branch)
@@ -287,8 +269,6 @@ def main():
287269
normalize_whitespace(python_files)
288270
# C rules enforcement.
289271
normalize_c_whitespace(c_files)
290-
# Doc whitespace enforcement.
291-
normalize_docs_whitespace(doc_files)
292272
# Docs updated.
293273
docs_modified(doc_files)
294274
# Misc/ACKS changed.

0 commit comments

Comments
 (0)