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