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 | |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 12 | |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 13 | def status(message, modal=False, info=None): |
| 14 | """Decorator to output status info to stdout.""" |
| 15 | def decorated_fxn(fxn): |
| 16 | def call_fxn(*args, **kwargs): |
| 17 | sys.stdout.write(message + ' ... ') |
| 18 | sys.stdout.flush() |
| 19 | result = fxn(*args, **kwargs) |
| 20 | if not modal and not info: |
| 21 | print("done") |
| 22 | elif info: |
| 23 | print(info(result)) |
| 24 | else: |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 25 | print("yes" if result else "NO") |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 26 | return result |
| 27 | return call_fxn |
| 28 | return decorated_fxn |
| 29 | |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 30 | |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 31 | @status("Getting the list of files that have been added/changed", |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 32 | info=lambda x: n_files_str(len(x))) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 33 | def changed_files(): |
| 34 | """Run ``svn status`` and return a set of files that have been |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 35 | changed/added. |
| 36 | """ |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 37 | cmd = 'svn status --quiet --non-interactive --ignore-externals' |
| 38 | svn_st = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) |
| 39 | svn_st.wait() |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 40 | output = (x.decode().rstrip().rsplit(None, 1)[-1] |
| 41 | for x in svn_st.stdout if x[0] in b'AM') |
| 42 | return set(path for path in output if os.path.isfile(path)) |
| 43 | |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 44 | |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 45 | def report_modified_files(file_paths): |
| 46 | count = len(file_paths) |
| 47 | if count == 0: |
| 48 | return n_files_str(count) |
| 49 | else: |
| 50 | lines = ["{}:".format(n_files_str(count))] |
| 51 | for path in file_paths: |
| 52 | lines.append(" {}".format(path)) |
| 53 | return "\n".join(lines) |
| 54 | |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 55 | |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 56 | @status("Fixing whitespace", info=report_modified_files) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 57 | def normalize_whitespace(file_paths): |
| 58 | """Make sure that the whitespace for .py files have been normalized.""" |
| 59 | reindent.makebackup = False # No need to create backups. |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 60 | fixed = [] |
| 61 | for path in (x for x in file_paths if x.endswith('.py')): |
| 62 | if reindent.check(path): |
| 63 | fixed.append(path) |
| 64 | return fixed |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 65 | |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 66 | |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 67 | @status("Docs modified", modal=True) |
| 68 | def docs_modified(file_paths): |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 69 | """Report if any file in the Doc directory has been changed.""" |
| 70 | return bool(file_paths) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 71 | |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 72 | |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 73 | @status("Misc/ACKS updated", modal=True) |
| 74 | def credit_given(file_paths): |
| 75 | """Check if Misc/ACKS has been changed.""" |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 76 | return 'Misc/ACKS' in file_paths |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 77 | |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 78 | |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 79 | @status("Misc/NEWS updated", modal=True) |
| 80 | def reported_news(file_paths): |
| 81 | """Check if Misc/NEWS has been changed.""" |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 82 | return 'Misc/NEWS' in file_paths |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 83 | |
| 84 | |
| 85 | def main(): |
| 86 | file_paths = changed_files() |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 87 | python_files = [fn for fn in file_paths if fn.endswith('.py')] |
| 88 | c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))] |
| 89 | docs = [fn for fn in file_paths if fn.startswith('Doc')] |
| 90 | special_files = {'Misc/ACKS', 'Misc/NEWS'} & set(file_paths) |
| 91 | # PEP 8 whitespace rules enforcement. |
| 92 | normalize_whitespace(python_files) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 93 | # Docs updated. |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 94 | docs_modified(docs) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 95 | # Misc/ACKS changed. |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 96 | credit_given(special_files) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 97 | # Misc/NEWS changed. |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 98 | reported_news(special_files) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 99 | |
| 100 | # Test suite run and passed. |
| 101 | print() |
| 102 | print("Did you run the test suite?") |
| 103 | |
| 104 | |
| 105 | if __name__ == '__main__': |
| 106 | main() |