Mercurial > cpython-lru_cache-weakref
comparison Tools/scripts/patchcheck.py @ 62798:7ffd2a44942c
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.
| author | Brett Cannon <bcannon@gmail.com> |
|---|---|
| date | Sun, 04 Jul 2010 22:05:34 +0000 |
| parents | a04b1c7167ba |
| children | 5ae390ac6c61 |
comparison
equal
deleted
inserted
replaced
| 62796:46abde071fff | 62798:7ffd2a44942c |
|---|---|
| 2 import subprocess | 2 import subprocess |
| 3 import sys | 3 import sys |
| 4 | 4 |
| 5 import reindent | 5 import reindent |
| 6 | 6 |
| 7 | |
| 8 def n_files_str(count): | |
| 9 """Return 'N file(s)' with the proper plurality on 'file'.""" | |
| 10 return "{} file{}".format(count, "s" if count != 1 else "") | |
| 7 | 11 |
| 8 def status(message, modal=False, info=None): | 12 def status(message, modal=False, info=None): |
| 9 """Decorator to output status info to stdout.""" | 13 """Decorator to output status info to stdout.""" |
| 10 def decorated_fxn(fxn): | 14 def decorated_fxn(fxn): |
| 11 def call_fxn(*args, **kwargs): | 15 def call_fxn(*args, **kwargs): |
| 24 return result | 28 return result |
| 25 return call_fxn | 29 return call_fxn |
| 26 return decorated_fxn | 30 return decorated_fxn |
| 27 | 31 |
| 28 @status("Getting the list of files that have been added/changed", | 32 @status("Getting the list of files that have been added/changed", |
| 29 info=lambda x: "%s files" % len(x)) | 33 info=lambda x: n_files_str(len(x))) |
| 30 def changed_files(): | 34 def changed_files(): |
| 31 """Run ``svn status`` and return a set of files that have been | 35 """Run ``svn status`` and return a set of files that have been |
| 32 changed/added.""" | 36 changed/added.""" |
| 33 cmd = 'svn status --quiet --non-interactive --ignore-externals' | 37 cmd = 'svn status --quiet --non-interactive --ignore-externals' |
| 34 svn_st = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) | 38 svn_st = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) |
| 42 path = line_parts[-1] | 46 path = line_parts[-1] |
| 43 if os.path.isfile(path): | 47 if os.path.isfile(path): |
| 44 files.add(path) | 48 files.add(path) |
| 45 return files | 49 return files |
| 46 | 50 |
| 47 @status("Fixing whitespace", info=lambda x: "%s files" % x) | 51 def report_modified_files(file_paths): |
| 52 count = len(file_paths) | |
| 53 if count == 0: | |
| 54 return n_files_str(count) | |
| 55 else: | |
| 56 lines = ["{}:".format(n_files_str(count))] | |
| 57 for path in file_paths: | |
| 58 lines.append(" {}".format(path)) | |
| 59 return "\n".join(lines) | |
| 60 | |
| 61 @status("Fixing whitespace", info=report_modified_files) | |
| 48 def normalize_whitespace(file_paths): | 62 def normalize_whitespace(file_paths): |
| 49 """Make sure that the whitespace for .py files have been normalized.""" | 63 """Make sure that the whitespace for .py files have been normalized.""" |
| 50 reindent.makebackup = False # No need to create backups. | 64 reindent.makebackup = False # No need to create backups. |
| 51 result = list(map(reindent.check, (x for x in file_paths if x.endswith('.py')))) | 65 fixed = [] |
| 52 return sum(result) | 66 for path in (x for x in file_paths if x.endswith('.py')): |
| 67 if reindent.check(path): | |
| 68 fixed.append(path) | |
| 69 return fixed | |
| 53 | 70 |
| 54 @status("Docs modified", modal=True) | 71 @status("Docs modified", modal=True) |
| 55 def docs_modified(file_paths): | 72 def docs_modified(file_paths): |
| 56 """Report if any files in the Docs directory.""" | 73 """Report if any file in the Doc directory has been changed.""" |
| 57 for path in file_paths: | 74 return bool(file_paths) |
| 58 if path.startswith("Doc"): | |
| 59 return True | |
| 60 return False | |
| 61 | 75 |
| 62 @status("Misc/ACKS updated", modal=True) | 76 @status("Misc/ACKS updated", modal=True) |
| 63 def credit_given(file_paths): | 77 def credit_given(file_paths): |
| 64 """Check if Misc/ACKS has been changed.""" | 78 """Check if Misc/ACKS has been changed.""" |
| 65 return 'Misc/ACKS' in file_paths | 79 return 'Misc/ACKS' in file_paths |
| 70 return 'Misc/NEWS' in file_paths | 84 return 'Misc/NEWS' in file_paths |
| 71 | 85 |
| 72 | 86 |
| 73 def main(): | 87 def main(): |
| 74 file_paths = changed_files() | 88 file_paths = changed_files() |
| 75 # PEP 7/8 verification. | 89 python_files = [fn for fn in file_paths if fn.endswith('.py')] |
| 76 normalize_whitespace(file_paths) | 90 c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))] |
| 91 docs = [fn for fn in file_paths if fn.startswith('Doc')] | |
| 92 special_files = {'Misc/ACKS', 'Misc/NEWS'} & set(file_paths) | |
| 93 # PEP 8 whitespace rules enforcement. | |
| 94 normalize_whitespace(python_files) | |
| 77 # Docs updated. | 95 # Docs updated. |
| 78 docs_modified(file_paths) | 96 docs_modified(docs) |
| 79 # Misc/ACKS changed. | 97 # Misc/ACKS changed. |
| 80 credit_given(file_paths) | 98 credit_given(special_files) |
| 81 # Misc/NEWS changed. | 99 # Misc/NEWS changed. |
| 82 reported_news(file_paths) | 100 reported_news(special_files) |
| 83 | 101 |
| 84 # Test suite run and passed. | 102 # Test suite run and passed. |
| 85 print() | 103 print() |
| 86 print("Did you run the test suite?") | 104 print("Did you run the test suite?") |
| 87 | 105 |
