Georg Brandl | a9afb68 | 2010-10-21 12:49:28 +0000 | [diff] [blame] | 1 | import re |
| 2 | import sys |
| 3 | import shutil |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 4 | import os.path |
| 5 | import subprocess |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 6 | |
| 7 | import reindent |
Georg Brandl | a9afb68 | 2010-10-21 12:49:28 +0000 | [diff] [blame] | 8 | import untabify |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 9 | |
| 10 | |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 11 | def n_files_str(count): |
| 12 | """Return 'N file(s)' with the proper plurality on 'file'.""" |
| 13 | return "{} file{}".format(count, "s" if count != 1 else "") |
| 14 | |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 15 | |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 16 | def status(message, modal=False, info=None): |
| 17 | """Decorator to output status info to stdout.""" |
| 18 | def decorated_fxn(fxn): |
| 19 | def call_fxn(*args, **kwargs): |
| 20 | sys.stdout.write(message + ' ... ') |
| 21 | sys.stdout.flush() |
| 22 | result = fxn(*args, **kwargs) |
| 23 | if not modal and not info: |
| 24 | print("done") |
| 25 | elif info: |
| 26 | print(info(result)) |
| 27 | else: |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 28 | print("yes" if result else "NO") |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 29 | return result |
| 30 | return call_fxn |
| 31 | return decorated_fxn |
| 32 | |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 33 | |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 34 | @status("Getting the list of files that have been added/changed", |
Georg Brandl | a9afb68 | 2010-10-21 12:49:28 +0000 | [diff] [blame] | 35 | info=lambda x: n_files_str(len(x))) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 36 | def changed_files(): |
Georg Brandl | a9afb68 | 2010-10-21 12:49:28 +0000 | [diff] [blame] | 37 | """Get the list of changed or added files from the VCS.""" |
| 38 | if os.path.isdir('.hg'): |
Georg Brandl | a9afb68 | 2010-10-21 12:49:28 +0000 | [diff] [blame] | 39 | cmd = 'hg status --added --modified --no-status' |
Georg Brandl | a9afb68 | 2010-10-21 12:49:28 +0000 | [diff] [blame] | 40 | else: |
| 41 | sys.exit('need a checkout to get modified files') |
| 42 | |
| 43 | st = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE) |
Éric Araujo | 1e600dc | 2010-11-22 03:13:47 +0000 | [diff] [blame] | 44 | try: |
| 45 | st.wait() |
Benjamin Peterson | 4177eff | 2011-06-27 18:25:06 -0500 | [diff] [blame^] | 46 | return [x.decode().rstrip() for x in st.stdout] |
Éric Araujo | 1e600dc | 2010-11-22 03:13:47 +0000 | [diff] [blame] | 47 | finally: |
| 48 | st.stdout.close() |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 49 | |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 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 | |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 61 | |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 62 | @status("Fixing whitespace", info=report_modified_files) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 63 | def normalize_whitespace(file_paths): |
| 64 | """Make sure that the whitespace for .py files have been normalized.""" |
| 65 | reindent.makebackup = False # No need to create backups. |
Benjamin Peterson | 4177eff | 2011-06-27 18:25:06 -0500 | [diff] [blame^] | 66 | fixed = [path for path in file_paths if path.endswith('.py') and |
| 67 | reindent.check(path)] |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 68 | return fixed |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 69 | |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 70 | |
Georg Brandl | a9afb68 | 2010-10-21 12:49:28 +0000 | [diff] [blame] | 71 | @status("Fixing C file whitespace", info=report_modified_files) |
| 72 | def normalize_c_whitespace(file_paths): |
| 73 | """Report if any C files """ |
| 74 | fixed = [] |
| 75 | for path in file_paths: |
| 76 | with open(path, 'r') as f: |
| 77 | if '\t' not in f.read(): |
| 78 | continue |
| 79 | untabify.process(path, 8, verbose=False) |
| 80 | fixed.append(path) |
| 81 | return fixed |
| 82 | |
| 83 | |
| 84 | ws_re = re.compile(br'\s+(\r?\n)$') |
| 85 | |
| 86 | @status("Fixing docs whitespace", info=report_modified_files) |
| 87 | def normalize_docs_whitespace(file_paths): |
| 88 | fixed = [] |
| 89 | for path in file_paths: |
| 90 | try: |
| 91 | with open(path, 'rb') as f: |
| 92 | lines = f.readlines() |
| 93 | new_lines = [ws_re.sub(br'\1', line) for line in lines] |
| 94 | if new_lines != lines: |
| 95 | shutil.copyfile(path, path + '.bak') |
| 96 | with open(path, 'wb') as f: |
| 97 | f.writelines(new_lines) |
| 98 | fixed.append(path) |
| 99 | except Exception as err: |
| 100 | print('Cannot fix %s: %s' % (path, err)) |
| 101 | return fixed |
| 102 | |
| 103 | |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 104 | @status("Docs modified", modal=True) |
| 105 | def docs_modified(file_paths): |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 106 | """Report if any file in the Doc directory has been changed.""" |
| 107 | return bool(file_paths) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 108 | |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 109 | |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 110 | @status("Misc/ACKS updated", modal=True) |
| 111 | def credit_given(file_paths): |
| 112 | """Check if Misc/ACKS has been changed.""" |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 113 | return 'Misc/ACKS' in file_paths |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 114 | |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 115 | |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 116 | @status("Misc/NEWS updated", modal=True) |
| 117 | def reported_news(file_paths): |
| 118 | """Check if Misc/NEWS has been changed.""" |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 119 | return 'Misc/NEWS' in file_paths |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 120 | |
| 121 | |
| 122 | def main(): |
| 123 | file_paths = changed_files() |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 124 | python_files = [fn for fn in file_paths if fn.endswith('.py')] |
| 125 | c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))] |
Georg Brandl | a9afb68 | 2010-10-21 12:49:28 +0000 | [diff] [blame] | 126 | doc_files = [fn for fn in file_paths if fn.startswith('Doc')] |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 127 | special_files = {'Misc/ACKS', 'Misc/NEWS'} & set(file_paths) |
| 128 | # PEP 8 whitespace rules enforcement. |
| 129 | normalize_whitespace(python_files) |
Georg Brandl | a9afb68 | 2010-10-21 12:49:28 +0000 | [diff] [blame] | 130 | # C rules enforcement. |
| 131 | normalize_c_whitespace(c_files) |
| 132 | # Doc whitespace enforcement. |
| 133 | normalize_docs_whitespace(doc_files) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 134 | # Docs updated. |
Georg Brandl | a9afb68 | 2010-10-21 12:49:28 +0000 | [diff] [blame] | 135 | docs_modified(doc_files) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 136 | # Misc/ACKS changed. |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 137 | credit_given(special_files) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 138 | # Misc/NEWS changed. |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 139 | reported_news(special_files) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 140 | |
| 141 | # Test suite run and passed. |
| 142 | print() |
| 143 | print("Did you run the test suite?") |
| 144 | |
| 145 | |
| 146 | if __name__ == '__main__': |
| 147 | main() |