]> granicus.if.org Git - python/commitdiff
Do some basic refactoring. Along the way also list what files had their
authorBrett Cannon <bcannon@gmail.com>
Sun, 4 Jul 2010 22:05:34 +0000 (22:05 +0000)
committerBrett Cannon <bcannon@gmail.com>
Sun, 4 Jul 2010 22:05:34 +0000 (22:05 +0000)
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.

Tools/scripts/patchcheck.py

index 544b00eae76a4e3d70d992eb59bf9e673ae35584..7d001e8680b0d4a1e6246054ee26cfcda73d2c34 100644 (file)
@@ -5,6 +5,10 @@ import sys
 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):
     """Decorator to output status info to stdout."""
     def decorated_fxn(fxn):
@@ -26,7 +30,7 @@ def status(message, modal=False, info=None):
     return decorated_fxn
 
 @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():
     """Run ``svn status`` and return a set of files that have been
     changed/added."""
@@ -44,20 +48,30 @@ def changed_files():
             files.add(path)
     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):
     """Make sure that the whitespace for .py files have been normalized."""
     reindent.makebackup = False  # No need to create backups.
-    result = list(map(reindent.check, (x for x in file_paths if x.endswith('.py'))))
-    return sum(result)
+    fixed = []
+    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)
 def docs_modified(file_paths):
-    """Report if any files in the Docs directory."""
-    for path in file_paths:
-        if path.startswith("Doc"):
-            return True
-    return False
+    """Report if any file in the Doc directory has been changed."""
+    return bool(file_paths)
 
 @status("Misc/ACKS updated", modal=True)
 def credit_given(file_paths):
@@ -72,14 +86,18 @@ def reported_news(file_paths):
 
 def main():
     file_paths = changed_files()
-    # PEP 7/8 verification.
-    normalize_whitespace(file_paths)
+    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'))]
+    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_modified(file_paths)
+    docs_modified(docs)
     # Misc/ACKS changed.
-    credit_given(file_paths)
+    credit_given(special_files)
     # Misc/NEWS changed.
-    reported_news(file_paths)
+    reported_news(special_files)
 
     # Test suite run and passed.
     print()