blob: e767edabd912cb9d0a985643ba17b4331e158da9 [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'):
39 vcs = 'hg'
40 cmd = 'hg status --added --modified --no-status'
41 elif os.path.isdir('.svn'):
42 vcs = 'svn'
43 cmd = 'svn status --quiet --non-interactive --ignore-externals'
44 else:
45 sys.exit('need a checkout to get modified files')
46
47 st = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
Éric Araujo1e600dc2010-11-22 03:13:47 +000048 try:
49 st.wait()
50 if vcs == 'hg':
51 return [x.decode().rstrip() for x in st.stdout]
52 else:
53 output = (x.decode().rstrip().rsplit(None, 1)[-1]
54 for x in st.stdout if x[0] in b'AM')
Georg Brandla9afb682010-10-21 12:49:28 +000055 return set(path for path in output if os.path.isfile(path))
Éric Araujo1e600dc2010-11-22 03:13:47 +000056 finally:
57 st.stdout.close()
Florent Xiclunae4a33802010-08-09 12:24:20 +000058
Christian Heimesada8c3b2008-03-18 18:26:33 +000059
Brett Cannon058173e2010-07-04 22:05:34 +000060def report_modified_files(file_paths):
61 count = len(file_paths)
62 if count == 0:
63 return n_files_str(count)
64 else:
65 lines = ["{}:".format(n_files_str(count))]
66 for path in file_paths:
67 lines.append(" {}".format(path))
68 return "\n".join(lines)
69
Florent Xiclunae4a33802010-08-09 12:24:20 +000070
Brett Cannon058173e2010-07-04 22:05:34 +000071@status("Fixing whitespace", info=report_modified_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +000072def normalize_whitespace(file_paths):
73 """Make sure that the whitespace for .py files have been normalized."""
74 reindent.makebackup = False # No need to create backups.
Brett Cannon058173e2010-07-04 22:05:34 +000075 fixed = []
76 for path in (x for x in file_paths if x.endswith('.py')):
77 if reindent.check(path):
78 fixed.append(path)
79 return fixed
Christian Heimesada8c3b2008-03-18 18:26:33 +000080
Florent Xiclunae4a33802010-08-09 12:24:20 +000081
Georg Brandla9afb682010-10-21 12:49:28 +000082@status("Fixing C file whitespace", info=report_modified_files)
83def normalize_c_whitespace(file_paths):
84 """Report if any C files """
85 fixed = []
86 for path in file_paths:
87 with open(path, 'r') as f:
88 if '\t' not in f.read():
89 continue
90 untabify.process(path, 8, verbose=False)
91 fixed.append(path)
92 return fixed
93
94
95ws_re = re.compile(br'\s+(\r?\n)$')
96
97@status("Fixing docs whitespace", info=report_modified_files)
98def normalize_docs_whitespace(file_paths):
99 fixed = []
100 for path in file_paths:
101 try:
102 with open(path, 'rb') as f:
103 lines = f.readlines()
104 new_lines = [ws_re.sub(br'\1', line) for line in lines]
105 if new_lines != lines:
106 shutil.copyfile(path, path + '.bak')
107 with open(path, 'wb') as f:
108 f.writelines(new_lines)
109 fixed.append(path)
110 except Exception as err:
111 print('Cannot fix %s: %s' % (path, err))
112 return fixed
113
114
Christian Heimesada8c3b2008-03-18 18:26:33 +0000115@status("Docs modified", modal=True)
116def docs_modified(file_paths):
Brett Cannon058173e2010-07-04 22:05:34 +0000117 """Report if any file in the Doc directory has been changed."""
118 return bool(file_paths)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000119
Florent Xiclunae4a33802010-08-09 12:24:20 +0000120
Christian Heimesada8c3b2008-03-18 18:26:33 +0000121@status("Misc/ACKS updated", modal=True)
122def credit_given(file_paths):
123 """Check if Misc/ACKS has been changed."""
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000124 return 'Misc/ACKS' in file_paths
Christian Heimesada8c3b2008-03-18 18:26:33 +0000125
Florent Xiclunae4a33802010-08-09 12:24:20 +0000126
Christian Heimesada8c3b2008-03-18 18:26:33 +0000127@status("Misc/NEWS updated", modal=True)
128def reported_news(file_paths):
129 """Check if Misc/NEWS has been changed."""
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000130 return 'Misc/NEWS' in file_paths
Christian Heimesada8c3b2008-03-18 18:26:33 +0000131
132
133def main():
134 file_paths = changed_files()
Brett Cannon058173e2010-07-04 22:05:34 +0000135 python_files = [fn for fn in file_paths if fn.endswith('.py')]
136 c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))]
Georg Brandla9afb682010-10-21 12:49:28 +0000137 doc_files = [fn for fn in file_paths if fn.startswith('Doc')]
Brett Cannon058173e2010-07-04 22:05:34 +0000138 special_files = {'Misc/ACKS', 'Misc/NEWS'} & set(file_paths)
139 # PEP 8 whitespace rules enforcement.
140 normalize_whitespace(python_files)
Georg Brandla9afb682010-10-21 12:49:28 +0000141 # C rules enforcement.
142 normalize_c_whitespace(c_files)
143 # Doc whitespace enforcement.
144 normalize_docs_whitespace(doc_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000145 # Docs updated.
Georg Brandla9afb682010-10-21 12:49:28 +0000146 docs_modified(doc_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000147 # Misc/ACKS changed.
Brett Cannon058173e2010-07-04 22:05:34 +0000148 credit_given(special_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000149 # Misc/NEWS changed.
Brett Cannon058173e2010-07-04 22:05:34 +0000150 reported_news(special_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000151
152 # Test suite run and passed.
153 print()
154 print("Did you run the test suite?")
155
156
157if __name__ == '__main__':
158 main()