blob: d42bc8a4bf1946d4aaeaeaebc6cee0b46847dd3c [file] [log] [blame]
Georg Brandla9afb682010-10-21 12:49:28 +00001import re
2import sys
3import shutil
Christian Heimesada8c3b2008-03-18 18:26:33 +00004import os.path
5import subprocess
Christian Heimesada8c3b2008-03-18 18:26:33 +00006
7import reindent
Georg Brandla9afb682010-10-21 12:49:28 +00008import untabify
Christian Heimesada8c3b2008-03-18 18:26:33 +00009
10
Brett Cannon058173e2010-07-04 22:05:34 +000011def 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 Xiclunae4a33802010-08-09 12:24:20 +000015
Christian Heimesada8c3b2008-03-18 18:26:33 +000016def 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 Xiclunae4a33802010-08-09 12:24:20 +000028 print("yes" if result else "NO")
Christian Heimesada8c3b2008-03-18 18:26:33 +000029 return result
30 return call_fxn
31 return decorated_fxn
32
Florent Xiclunae4a33802010-08-09 12:24:20 +000033
Christian Heimesada8c3b2008-03-18 18:26:33 +000034@status("Getting the list of files that have been added/changed",
Georg Brandla9afb682010-10-21 12:49:28 +000035 info=lambda x: n_files_str(len(x)))
Christian Heimesada8c3b2008-03-18 18:26:33 +000036def changed_files():
Georg Brandla9afb682010-10-21 12:49:28 +000037 """Get the list of changed or added files from the VCS."""
38 if os.path.isdir('.hg'):
Georg Brandla9afb682010-10-21 12:49:28 +000039 cmd = 'hg status --added --modified --no-status'
Georg Brandla9afb682010-10-21 12:49:28 +000040 else:
41 sys.exit('need a checkout to get modified files')
42
43 st = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
Éric Araujo1e600dc2010-11-22 03:13:47 +000044 try:
45 st.wait()
Benjamin Peterson4177eff2011-06-27 18:25:06 -050046 return [x.decode().rstrip() for x in st.stdout]
Éric Araujo1e600dc2010-11-22 03:13:47 +000047 finally:
48 st.stdout.close()
Florent Xiclunae4a33802010-08-09 12:24:20 +000049
Christian Heimesada8c3b2008-03-18 18:26:33 +000050
Brett Cannon058173e2010-07-04 22:05:34 +000051def 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 Xiclunae4a33802010-08-09 12:24:20 +000061
Brett Cannon058173e2010-07-04 22:05:34 +000062@status("Fixing whitespace", info=report_modified_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +000063def 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 Peterson4177eff2011-06-27 18:25:06 -050066 fixed = [path for path in file_paths if path.endswith('.py') and
67 reindent.check(path)]
Brett Cannon058173e2010-07-04 22:05:34 +000068 return fixed
Christian Heimesada8c3b2008-03-18 18:26:33 +000069
Florent Xiclunae4a33802010-08-09 12:24:20 +000070
Georg Brandla9afb682010-10-21 12:49:28 +000071@status("Fixing C file whitespace", info=report_modified_files)
72def 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
84ws_re = re.compile(br'\s+(\r?\n)$')
85
86@status("Fixing docs whitespace", info=report_modified_files)
87def 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 Heimesada8c3b2008-03-18 18:26:33 +0000104@status("Docs modified", modal=True)
105def docs_modified(file_paths):
Brett Cannon058173e2010-07-04 22:05:34 +0000106 """Report if any file in the Doc directory has been changed."""
107 return bool(file_paths)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000108
Florent Xiclunae4a33802010-08-09 12:24:20 +0000109
Christian Heimesada8c3b2008-03-18 18:26:33 +0000110@status("Misc/ACKS updated", modal=True)
111def credit_given(file_paths):
112 """Check if Misc/ACKS has been changed."""
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000113 return 'Misc/ACKS' in file_paths
Christian Heimesada8c3b2008-03-18 18:26:33 +0000114
Florent Xiclunae4a33802010-08-09 12:24:20 +0000115
Christian Heimesada8c3b2008-03-18 18:26:33 +0000116@status("Misc/NEWS updated", modal=True)
117def reported_news(file_paths):
118 """Check if Misc/NEWS has been changed."""
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000119 return 'Misc/NEWS' in file_paths
Christian Heimesada8c3b2008-03-18 18:26:33 +0000120
121
122def main():
123 file_paths = changed_files()
Brett Cannon058173e2010-07-04 22:05:34 +0000124 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 Brandla9afb682010-10-21 12:49:28 +0000126 doc_files = [fn for fn in file_paths if fn.startswith('Doc')]
Brett Cannon058173e2010-07-04 22:05:34 +0000127 special_files = {'Misc/ACKS', 'Misc/NEWS'} & set(file_paths)
128 # PEP 8 whitespace rules enforcement.
129 normalize_whitespace(python_files)
Georg Brandla9afb682010-10-21 12:49:28 +0000130 # C rules enforcement.
131 normalize_c_whitespace(c_files)
132 # Doc whitespace enforcement.
133 normalize_docs_whitespace(doc_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000134 # Docs updated.
Georg Brandla9afb682010-10-21 12:49:28 +0000135 docs_modified(doc_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000136 # Misc/ACKS changed.
Brett Cannon058173e2010-07-04 22:05:34 +0000137 credit_given(special_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000138 # Misc/NEWS changed.
Brett Cannon058173e2010-07-04 22:05:34 +0000139 reported_news(special_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000140
141 # Test suite run and passed.
142 print()
143 print("Did you run the test suite?")
144
145
146if __name__ == '__main__':
147 main()