Skip to content
Prev Previous commit
Next Next commit
Merge branch 'main' into patchcheck/c-file-whitespace
# Conflicts:
#	Tools/patchcheck/patchcheck.py
  • Loading branch information
AA-Turner committed Oct 10, 2023
commit 986f2a3c7beab9884a40ff0ed5d9e0d45b471870
59 changes: 8 additions & 51 deletions Tools/patchcheck/patchcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import subprocess
import sysconfig

import reindent


SRCDIR = sysconfig.get_config_var('srcdir')
def get_python_source_dir():
src_dir = sysconfig.get_config_var('abs_srcdir')
if not src_dir:
src_dir = sysconfig.get_config_var('srcdir')
return os.path.abspath(src_dir)
SRCDIR = get_python_source_dir()


def n_files_str(count):
Expand Down Expand Up @@ -162,42 +164,6 @@ def report_modified_files(file_paths):
})


@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


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)
def docs_modified(file_paths):
"""Report if any file in the Doc directory has been changed."""
Expand Down Expand Up @@ -241,12 +207,7 @@ def ci(pull_request):
return
base_branch = get_base_branch()
file_paths = changed_files(base_branch)
python_files = [fn for fn in file_paths if fn.endswith('.py')]
doc_files = [fn for fn in file_paths if fn.startswith('Doc') and
fn.endswith(('.rst', '.inc'))]
fixed = []
fixed.extend(normalize_whitespace(python_files))
fixed.extend(normalize_docs_whitespace(doc_files))
if not fixed:
print('No whitespace issues found')
else:
Expand All @@ -259,14 +220,9 @@ def ci(pull_request):
def main():
base_branch = get_base_branch()
file_paths = changed_files(base_branch)
python_files = [fn for fn in file_paths if fn.endswith('.py')]
doc_files = [fn for fn in file_paths if fn.startswith('Doc') and
fn.endswith(('.rst', '.inc'))]
misc_files = {p for p in file_paths if p.startswith('Misc')}
# PEP 8 whitespace rules enforcement.
normalize_whitespace(python_files)
# Doc whitespace enforcement.
normalize_docs_whitespace(doc_files)
# Docs updated.
docs_modified(doc_files)
# Misc/ACKS changed.
Expand All @@ -280,10 +236,11 @@ def main():

# Test suite run and passed.
has_c_files = any(fn for fn in file_paths if fn.endswith(('.c', '.h')))
has_python_files = any(fn for fn in file_paths if fn.endswith('.py'))
print()
if has_c_files:
print("Did you run the test suite and check for refleaks?")
elif python_files:
elif has_python_files:
print("Did you run the test suite?")
Comment on lines +278 to +208
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's so much clearer like this! 👍



Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.