É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(): |
Éric Araujo | 56ec5fe | 2011-07-31 18:41:25 +0200 | [diff] [blame] | 42 | """Get the list of changed or added files from Mercurial.""" |
| 43 | if not os.path.isdir(os.path.join(SRCDIR, '.hg')): |
Georg Brandl | a9afb68 | 2010-10-21 12:49:28 +0000 | [diff] [blame] | 44 | sys.exit('need a checkout to get modified files') |
| 45 | |
Éric Araujo | 56ec5fe | 2011-07-31 18:41:25 +0200 | [diff] [blame] | 46 | cmd = 'hg status --added --modified --no-status' |
| 47 | with subprocess.Popen(cmd.split(), stdout=subprocess.PIPE) as st: |
Benjamin Peterson | 4177eff | 2011-06-27 18:25:06 -0500 | [diff] [blame] | 48 | return [x.decode().rstrip() for x in st.stdout] |
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 |
Éric Araujo | ad548b8 | 2011-07-31 18:33:00 +0200 | [diff] [blame] | 67 | reindent.check(os.path.join(SRCDIR, 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: |
Éric Araujo | a3e072b | 2011-07-30 21:34:04 +0200 | [diff] [blame] | 76 | abspath = os.path.join(SRCDIR, path) |
| 77 | with open(abspath, 'r') as f: |
Georg Brandl | a9afb68 | 2010-10-21 12:49:28 +0000 | [diff] [blame] | 78 | if '\t' not in f.read(): |
| 79 | continue |
Éric Araujo | a3e072b | 2011-07-30 21:34:04 +0200 | [diff] [blame] | 80 | untabify.process(abspath, 8, verbose=False) |
Georg Brandl | a9afb68 | 2010-10-21 12:49:28 +0000 | [diff] [blame] | 81 | fixed.append(path) |
| 82 | return fixed |
| 83 | |
| 84 | |
| 85 | ws_re = re.compile(br'\s+(\r?\n)$') |
| 86 | |
| 87 | @status("Fixing docs whitespace", info=report_modified_files) |
| 88 | def normalize_docs_whitespace(file_paths): |
| 89 | fixed = [] |
| 90 | for path in file_paths: |
Éric Araujo | a3e072b | 2011-07-30 21:34:04 +0200 | [diff] [blame] | 91 | abspath = os.path.join(SRCDIR, path) |
Georg Brandl | a9afb68 | 2010-10-21 12:49:28 +0000 | [diff] [blame] | 92 | try: |
Éric Araujo | a3e072b | 2011-07-30 21:34:04 +0200 | [diff] [blame] | 93 | with open(abspath, 'rb') as f: |
Georg Brandl | a9afb68 | 2010-10-21 12:49:28 +0000 | [diff] [blame] | 94 | lines = f.readlines() |
| 95 | new_lines = [ws_re.sub(br'\1', line) for line in lines] |
| 96 | if new_lines != lines: |
Éric Araujo | a3e072b | 2011-07-30 21:34:04 +0200 | [diff] [blame] | 97 | shutil.copyfile(abspath, abspath + '.bak') |
| 98 | with open(abspath, 'wb') as f: |
Georg Brandl | a9afb68 | 2010-10-21 12:49:28 +0000 | [diff] [blame] | 99 | f.writelines(new_lines) |
| 100 | fixed.append(path) |
| 101 | except Exception as err: |
| 102 | print('Cannot fix %s: %s' % (path, err)) |
| 103 | return fixed |
| 104 | |
| 105 | |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 106 | @status("Docs modified", modal=True) |
| 107 | def docs_modified(file_paths): |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 108 | """Report if any file in the Doc directory has been changed.""" |
| 109 | return bool(file_paths) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 110 | |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 111 | |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 112 | @status("Misc/ACKS updated", modal=True) |
| 113 | def credit_given(file_paths): |
| 114 | """Check if Misc/ACKS has been changed.""" |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 115 | return 'Misc/ACKS' in file_paths |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 116 | |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 117 | |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 118 | @status("Misc/NEWS updated", modal=True) |
| 119 | def reported_news(file_paths): |
| 120 | """Check if Misc/NEWS has been changed.""" |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 121 | return 'Misc/NEWS' in file_paths |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 122 | |
| 123 | |
| 124 | def main(): |
| 125 | file_paths = changed_files() |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 126 | python_files = [fn for fn in file_paths if fn.endswith('.py')] |
| 127 | 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] | 128 | doc_files = [fn for fn in file_paths if fn.startswith('Doc')] |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 129 | special_files = {'Misc/ACKS', 'Misc/NEWS'} & set(file_paths) |
| 130 | # PEP 8 whitespace rules enforcement. |
| 131 | normalize_whitespace(python_files) |
Georg Brandl | a9afb68 | 2010-10-21 12:49:28 +0000 | [diff] [blame] | 132 | # C rules enforcement. |
| 133 | normalize_c_whitespace(c_files) |
| 134 | # Doc whitespace enforcement. |
| 135 | normalize_docs_whitespace(doc_files) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 136 | # Docs updated. |
Georg Brandl | a9afb68 | 2010-10-21 12:49:28 +0000 | [diff] [blame] | 137 | docs_modified(doc_files) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 138 | # Misc/ACKS changed. |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 139 | credit_given(special_files) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 140 | # Misc/NEWS changed. |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 141 | reported_news(special_files) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 142 | |
| 143 | # Test suite run and passed. |
Éric Araujo | fbc5ff6 | 2011-08-12 17:50:08 +0200 | [diff] [blame] | 144 | if python_files or c_files: |
| 145 | print() |
| 146 | print("Did you run the test suite?") |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 147 | |
| 148 | |
| 149 | if __name__ == '__main__': |
| 150 | main() |