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