Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 1 | import os.path |
| 2 | import subprocess |
| 3 | import sys |
| 4 | |
| 5 | import reindent |
| 6 | |
| 7 | |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 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 "") |
| 11 | |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 12 | def status(message, modal=False, info=None): |
| 13 | """Decorator to output status info to stdout.""" |
| 14 | def decorated_fxn(fxn): |
| 15 | def call_fxn(*args, **kwargs): |
| 16 | sys.stdout.write(message + ' ... ') |
| 17 | sys.stdout.flush() |
| 18 | result = fxn(*args, **kwargs) |
| 19 | if not modal and not info: |
| 20 | print("done") |
| 21 | elif info: |
| 22 | print(info(result)) |
| 23 | else: |
| 24 | if result: |
| 25 | print("yes") |
| 26 | else: |
| 27 | print("NO") |
| 28 | return result |
| 29 | return call_fxn |
| 30 | return decorated_fxn |
| 31 | |
| 32 | @status("Getting the list of files that have been added/changed", |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 33 | info=lambda x: n_files_str(len(x))) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 34 | def changed_files(): |
| 35 | """Run ``svn status`` and return a set of files that have been |
| 36 | changed/added.""" |
| 37 | cmd = 'svn status --quiet --non-interactive --ignore-externals' |
| 38 | svn_st = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) |
| 39 | svn_st.wait() |
Brett Cannon | 5396a66 | 2008-03-18 21:45:57 +0000 | [diff] [blame] | 40 | output = [x.decode().rstrip() for x in svn_st.stdout.readlines()] |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 41 | files = set() |
| 42 | for line in output: |
| 43 | if not line[0] in ('A', 'M'): |
| 44 | continue |
| 45 | line_parts = line.split() |
| 46 | path = line_parts[-1] |
| 47 | if os.path.isfile(path): |
| 48 | files.add(path) |
| 49 | return files |
| 50 | |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 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) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 62 | def normalize_whitespace(file_paths): |
| 63 | """Make sure that the whitespace for .py files have been normalized.""" |
| 64 | reindent.makebackup = False # No need to create backups. |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 65 | fixed = [] |
| 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 |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 70 | |
| 71 | @status("Docs modified", modal=True) |
| 72 | def docs_modified(file_paths): |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 73 | """Report if any file in the Doc directory has been changed.""" |
| 74 | return bool(file_paths) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 75 | |
| 76 | @status("Misc/ACKS updated", modal=True) |
| 77 | def credit_given(file_paths): |
| 78 | """Check if Misc/ACKS has been changed.""" |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 79 | return 'Misc/ACKS' in file_paths |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 80 | |
| 81 | @status("Misc/NEWS updated", modal=True) |
| 82 | def reported_news(file_paths): |
| 83 | """Check if Misc/NEWS has been changed.""" |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 84 | return 'Misc/NEWS' in file_paths |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 85 | |
| 86 | |
| 87 | def main(): |
| 88 | file_paths = changed_files() |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 89 | python_files = [fn for fn in file_paths if fn.endswith('.py')] |
| 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) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 95 | # Docs updated. |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 96 | docs_modified(docs) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 97 | # Misc/ACKS changed. |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 98 | credit_given(special_files) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 99 | # Misc/NEWS changed. |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 100 | reported_news(special_files) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 101 | |
| 102 | # Test suite run and passed. |
| 103 | print() |
| 104 | print("Did you run the test suite?") |
| 105 | |
| 106 | |
| 107 | if __name__ == '__main__': |
| 108 | main() |