Commit 058173e2 authored by Brett Cannon's avatar Brett Cannon

Do some basic refactoring. Along the way also list what files had their

whitespace cleaned up and do the proper plurality for "N file(s)" based on N.

Refactoring closes issue8912. Thanks to Éric Araujo for the patch.
parent 5cca695e
...@@ -5,6 +5,10 @@ import sys ...@@ -5,6 +5,10 @@ import sys
import reindent import reindent
def n_files_str(count):
"""Return 'N file(s)' with the proper plurality on 'file'."""
return "{} file{}".format(count, "s" if count != 1 else "")
def status(message, modal=False, info=None): def status(message, modal=False, info=None):
"""Decorator to output status info to stdout.""" """Decorator to output status info to stdout."""
def decorated_fxn(fxn): def decorated_fxn(fxn):
...@@ -26,7 +30,7 @@ def status(message, modal=False, info=None): ...@@ -26,7 +30,7 @@ def status(message, modal=False, info=None):
return decorated_fxn return decorated_fxn
@status("Getting the list of files that have been added/changed", @status("Getting the list of files that have been added/changed",
info=lambda x: "%s files" % len(x)) info=lambda x: n_files_str(len(x)))
def changed_files(): def changed_files():
"""Run ``svn status`` and return a set of files that have been """Run ``svn status`` and return a set of files that have been
changed/added.""" changed/added."""
...@@ -44,20 +48,30 @@ def changed_files(): ...@@ -44,20 +48,30 @@ def changed_files():
files.add(path) files.add(path)
return files return files
@status("Fixing whitespace", info=lambda x: "%s files" % x) def report_modified_files(file_paths):
count = len(file_paths)
if count == 0:
return n_files_str(count)
else:
lines = ["{}:".format(n_files_str(count))]
for path in file_paths:
lines.append(" {}".format(path))
return "\n".join(lines)
@status("Fixing whitespace", info=report_modified_files)
def normalize_whitespace(file_paths): def normalize_whitespace(file_paths):
"""Make sure that the whitespace for .py files have been normalized.""" """Make sure that the whitespace for .py files have been normalized."""
reindent.makebackup = False # No need to create backups. reindent.makebackup = False # No need to create backups.
result = list(map(reindent.check, (x for x in file_paths if x.endswith('.py')))) fixed = []
return sum(result) for path in (x for x in file_paths if x.endswith('.py')):
if reindent.check(path):
fixed.append(path)
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 files in the Docs directory.""" """Report if any file in the Doc directory has been changed."""
for path in file_paths: return bool(file_paths)
if path.startswith("Doc"):
return True
return False
@status("Misc/ACKS updated", modal=True) @status("Misc/ACKS updated", modal=True)
def credit_given(file_paths): def credit_given(file_paths):
...@@ -72,14 +86,18 @@ def reported_news(file_paths): ...@@ -72,14 +86,18 @@ def reported_news(file_paths):
def main(): def main():
file_paths = changed_files() file_paths = changed_files()
# PEP 7/8 verification. python_files = [fn for fn in file_paths if fn.endswith('.py')]
normalize_whitespace(file_paths) c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))]
docs = [fn for fn in file_paths if fn.startswith('Doc')]
special_files = {'Misc/ACKS', 'Misc/NEWS'} & set(file_paths)
# PEP 8 whitespace rules enforcement.
normalize_whitespace(python_files)
# Docs updated. # Docs updated.
docs_modified(file_paths) docs_modified(docs)
# Misc/ACKS changed. # Misc/ACKS changed.
credit_given(file_paths) credit_given(special_files)
# Misc/NEWS changed. # Misc/NEWS changed.
reported_news(file_paths) reported_news(special_files)
# Test suite run and passed. # Test suite run and passed.
print() print()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment