Skip to content

Commit 0ffbde4

Browse files
miss-islingtonhugovkAlexWaygoodAA-Turner
authored
[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 7426ed0) 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>
1 parent e73210c commit 0ffbde4

File tree

2 files changed

+13
-33
lines changed

2 files changed

+13
-33
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ repos:
1717
types: [python]
1818
exclude: Lib/test/tokenizedata/coding20731.py
1919
- id: trailing-whitespace
20-
types_or: [c, python, rst]
20+
types_or: [c, inc, python, rst]
2121

2222
- repo: https://github.com/sphinx-contrib/sphinx-lint
2323
rev: v0.6.8

Tools/patchcheck/patchcheck.py

+12-32
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:
@@ -171,9 +172,9 @@ def report_modified_files(file_paths):
171172
if count == 0:
172173
return n_files_str(count)
173174
else:
174-
lines = ["{}:".format(n_files_str(count))]
175+
lines = [f"{n_files_str(count)}:"]
175176
for path in file_paths:
176-
lines.append(" {}".format(path))
177+
lines.append(f" {path}")
177178
return "\n".join(lines)
178179

179180

@@ -212,27 +213,6 @@ def normalize_c_whitespace(file_paths):
212213
return fixed
213214

214215

215-
ws_re = re.compile(br'\s+(\r?\n)$')
216-
217-
@status("Fixing docs whitespace", info=report_modified_files)
218-
def normalize_docs_whitespace(file_paths):
219-
fixed = []
220-
for path in file_paths:
221-
abspath = os.path.join(SRCDIR, path)
222-
try:
223-
with open(abspath, 'rb') as f:
224-
lines = f.readlines()
225-
new_lines = [ws_re.sub(br'\1', line) for line in lines]
226-
if new_lines != lines:
227-
shutil.copyfile(abspath, abspath + '.bak')
228-
with open(abspath, 'wb') as f:
229-
f.writelines(new_lines)
230-
fixed.append(path)
231-
except Exception as err:
232-
print('Cannot fix %s: %s' % (path, err))
233-
return fixed
234-
235-
236216
@status("Docs modified", modal=True)
237217
def docs_modified(file_paths):
238218
"""Report if any file in the Doc directory has been changed."""
@@ -251,6 +231,7 @@ def reported_news(file_paths):
251231
return any(p.startswith(os.path.join('Misc', 'NEWS.d', 'next'))
252232
for p in file_paths)
253233

234+
254235
@status("configure regenerated", modal=True, info=str)
255236
def regenerated_configure(file_paths):
256237
"""Check if configure has been regenerated."""
@@ -259,6 +240,7 @@ def regenerated_configure(file_paths):
259240
else:
260241
return "not needed"
261242

243+
262244
@status("pyconfig.h.in regenerated", modal=True, info=str)
263245
def regenerated_pyconfig_h_in(file_paths):
264246
"""Check if pyconfig.h.in has been regenerated."""
@@ -267,6 +249,7 @@ def regenerated_pyconfig_h_in(file_paths):
267249
else:
268250
return "not needed"
269251

252+
270253
def ci(pull_request):
271254
if pull_request == 'false':
272255
print('Not a pull request; skipping')
@@ -275,19 +258,18 @@ def ci(pull_request):
275258
file_paths = changed_files(base_branch)
276259
python_files = [fn for fn in file_paths if fn.endswith('.py')]
277260
c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))]
278-
doc_files = [fn for fn in file_paths if fn.startswith('Doc') and
279-
fn.endswith(('.rst', '.inc'))]
280261
fixed = []
281262
fixed.extend(normalize_whitespace(python_files))
282263
fixed.extend(normalize_c_whitespace(c_files))
283-
fixed.extend(normalize_docs_whitespace(doc_files))
284264
if not fixed:
285265
print('No whitespace issues found')
286266
else:
287-
print(f'Please fix the {len(fixed)} file(s) with whitespace issues')
288-
print('(on UNIX you can run `make patchcheck` to make the fixes)')
267+
count = len(fixed)
268+
print(f'Please fix the {n_files_str(count)} with whitespace issues')
269+
print('(on Unix you can run `make patchcheck` to make the fixes)')
289270
sys.exit(1)
290271

272+
291273
def main():
292274
base_branch = get_base_branch()
293275
file_paths = changed_files(base_branch)
@@ -300,8 +282,6 @@ def main():
300282
normalize_whitespace(python_files)
301283
# C rules enforcement.
302284
normalize_c_whitespace(c_files)
303-
# Doc whitespace enforcement.
304-
normalize_docs_whitespace(doc_files)
305285
# Docs updated.
306286
docs_modified(doc_files)
307287
# Misc/ACKS changed.

0 commit comments

Comments
 (0)