blob: 8b0d3cdb1e28cde28836d7f0760a0bbc808c54e2 [file] [log] [blame]
Éric Araujoa0e92a82011-07-26 18:01:08 +02001#!/usr/bin/env python3
Georg Brandla9afb682010-10-21 12:49:28 +00002import re
3import sys
4import shutil
Christian Heimesada8c3b2008-03-18 18:26:33 +00005import os.path
6import subprocess
Christian Heimesada8c3b2008-03-18 18:26:33 +00007
8import reindent
Georg Brandla9afb682010-10-21 12:49:28 +00009import untabify
Christian Heimesada8c3b2008-03-18 18:26:33 +000010
11
Brett Cannon058173e2010-07-04 22:05:34 +000012def 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 "")
15
Florent Xiclunae4a33802010-08-09 12:24:20 +000016
Christian Heimesada8c3b2008-03-18 18:26:33 +000017def 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:
Florent Xiclunae4a33802010-08-09 12:24:20 +000029 print("yes" if result else "NO")
Christian Heimesada8c3b2008-03-18 18:26:33 +000030 return result
31 return call_fxn
32 return decorated_fxn
33
Florent Xiclunae4a33802010-08-09 12:24:20 +000034
Christian Heimesada8c3b2008-03-18 18:26:33 +000035@status("Getting the list of files that have been added/changed",
Georg Brandla9afb682010-10-21 12:49:28 +000036 info=lambda x: n_files_str(len(x)))
Christian Heimesada8c3b2008-03-18 18:26:33 +000037def changed_files():
Georg Brandla9afb682010-10-21 12:49:28 +000038 """Get the list of changed or added files from the VCS."""
39 if os.path.isdir('.hg'):
Georg Brandla9afb682010-10-21 12:49:28 +000040 cmd = 'hg status --added --modified --no-status'
Georg Brandla9afb682010-10-21 12:49:28 +000041 else:
42 sys.exit('need a checkout to get modified files')
43
44 st = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
Éric Araujo1e600dc2010-11-22 03:13:47 +000045 try:
46 st.wait()
Benjamin Peterson4177eff2011-06-27 18:25:06 -050047 return [x.decode().rstrip() for x in st.stdout]
Éric Araujo1e600dc2010-11-22 03:13:47 +000048 finally:
49 st.stdout.close()
Florent Xiclunae4a33802010-08-09 12:24:20 +000050
Christian Heimesada8c3b2008-03-18 18:26:33 +000051
Brett Cannon058173e2010-07-04 22:05:34 +000052def report_modified_files(file_paths):
53 count = len(file_paths)
54 if count == 0:
55 return n_files_str(count)
56 else:
57 lines = ["{}:".format(n_files_str(count))]
58 for path in file_paths:
59 lines.append(" {}".format(path))
60 return "\n".join(lines)
61
Florent Xiclunae4a33802010-08-09 12:24:20 +000062
Brett Cannon058173e2010-07-04 22:05:34 +000063@status("Fixing whitespace", info=report_modified_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +000064def normalize_whitespace(file_paths):
65 """Make sure that the whitespace for .py files have been normalized."""
66 reindent.makebackup = False # No need to create backups.
Benjamin Peterson4177eff2011-06-27 18:25:06 -050067 fixed = [path for path in file_paths if path.endswith('.py') and
68 reindent.check(path)]
Brett Cannon058173e2010-07-04 22:05:34 +000069 return fixed
Christian Heimesada8c3b2008-03-18 18:26:33 +000070
Florent Xiclunae4a33802010-08-09 12:24:20 +000071
Georg Brandla9afb682010-10-21 12:49:28 +000072@status("Fixing C file whitespace", info=report_modified_files)
73def normalize_c_whitespace(file_paths):
74 """Report if any C files """
75 fixed = []
76 for path in file_paths:
77 with open(path, 'r') as f:
78 if '\t' not in f.read():
79 continue
80 untabify.process(path, 8, verbose=False)
81 fixed.append(path)
82 return fixed
83
84
85ws_re = re.compile(br'\s+(\r?\n)$')
86
87@status("Fixing docs whitespace", info=report_modified_files)
88def normalize_docs_whitespace(file_paths):
89 fixed = []
90 for path in file_paths:
91 try:
92 with open(path, 'rb') as f:
93 lines = f.readlines()
94 new_lines = [ws_re.sub(br'\1', line) for line in lines]
95 if new_lines != lines:
96 shutil.copyfile(path, path + '.bak')
97 with open(path, 'wb') as f:
98 f.writelines(new_lines)
99 fixed.append(path)
100 except Exception as err:
101 print('Cannot fix %s: %s' % (path, err))
102 return fixed
103
104
Christian Heimesada8c3b2008-03-18 18:26:33 +0000105@status("Docs modified", modal=True)
106def docs_modified(file_paths):
Brett Cannon058173e2010-07-04 22:05:34 +0000107 """Report if any file in the Doc directory has been changed."""
108 return bool(file_paths)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000109
Florent Xiclunae4a33802010-08-09 12:24:20 +0000110
Christian Heimesada8c3b2008-03-18 18:26:33 +0000111@status("Misc/ACKS updated", modal=True)
112def credit_given(file_paths):
113 """Check if Misc/ACKS has been changed."""
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000114 return 'Misc/ACKS' in file_paths
Christian Heimesada8c3b2008-03-18 18:26:33 +0000115
Florent Xiclunae4a33802010-08-09 12:24:20 +0000116
Christian Heimesada8c3b2008-03-18 18:26:33 +0000117@status("Misc/NEWS updated", modal=True)
118def reported_news(file_paths):
119 """Check if Misc/NEWS has been changed."""
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000120 return 'Misc/NEWS' in file_paths
Christian Heimesada8c3b2008-03-18 18:26:33 +0000121
122
123def main():
124 file_paths = changed_files()
Brett Cannon058173e2010-07-04 22:05:34 +0000125 python_files = [fn for fn in file_paths if fn.endswith('.py')]
126 c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))]
Georg Brandla9afb682010-10-21 12:49:28 +0000127 doc_files = [fn for fn in file_paths if fn.startswith('Doc')]
Brett Cannon058173e2010-07-04 22:05:34 +0000128 special_files = {'Misc/ACKS', 'Misc/NEWS'} & set(file_paths)
129 # PEP 8 whitespace rules enforcement.
130 normalize_whitespace(python_files)
Georg Brandla9afb682010-10-21 12:49:28 +0000131 # C rules enforcement.
132 normalize_c_whitespace(c_files)
133 # Doc whitespace enforcement.
134 normalize_docs_whitespace(doc_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000135 # Docs updated.
Georg Brandla9afb682010-10-21 12:49:28 +0000136 docs_modified(doc_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000137 # Misc/ACKS changed.
Brett Cannon058173e2010-07-04 22:05:34 +0000138 credit_given(special_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000139 # Misc/NEWS changed.
Brett Cannon058173e2010-07-04 22:05:34 +0000140 reported_news(special_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000141
142 # Test suite run and passed.
143 print()
144 print("Did you run the test suite?")
145
146
147if __name__ == '__main__':
148 main()