blob: caffc9d4c44031bedccf9488ddcad3cf59c00696 [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)
48 st.wait()
49 if vcs == 'hg':
50 return [x.decode().rstrip() for x in st.stdout]
51 else:
52 output = (x.decode().rstrip().rsplit(None, 1)[-1]
53 for x in st.stdout if x[0] in b'AM')
54 return set(path for path in output if os.path.isfile(path))
Florent Xiclunae4a33802010-08-09 12:24:20 +000055
Christian Heimesada8c3b2008-03-18 18:26:33 +000056
Brett Cannon058173e2010-07-04 22:05:34 +000057def report_modified_files(file_paths):
58 count = len(file_paths)
59 if count == 0:
60 return n_files_str(count)
61 else:
62 lines = ["{}:".format(n_files_str(count))]
63 for path in file_paths:
64 lines.append(" {}".format(path))
65 return "\n".join(lines)
66
Florent Xiclunae4a33802010-08-09 12:24:20 +000067
Brett Cannon058173e2010-07-04 22:05:34 +000068@status("Fixing whitespace", info=report_modified_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +000069def normalize_whitespace(file_paths):
70 """Make sure that the whitespace for .py files have been normalized."""
71 reindent.makebackup = False # No need to create backups.
Brett Cannon058173e2010-07-04 22:05:34 +000072 fixed = []
73 for path in (x for x in file_paths if x.endswith('.py')):
74 if reindent.check(path):
75 fixed.append(path)
76 return fixed
Christian Heimesada8c3b2008-03-18 18:26:33 +000077
Florent Xiclunae4a33802010-08-09 12:24:20 +000078
Georg Brandla9afb682010-10-21 12:49:28 +000079@status("Fixing C file whitespace", info=report_modified_files)
80def normalize_c_whitespace(file_paths):
81 """Report if any C files """
82 fixed = []
83 for path in file_paths:
84 with open(path, 'r') as f:
85 if '\t' not in f.read():
86 continue
87 untabify.process(path, 8, verbose=False)
88 fixed.append(path)
89 return fixed
90
91
92ws_re = re.compile(br'\s+(\r?\n)$')
93
94@status("Fixing docs whitespace", info=report_modified_files)
95def normalize_docs_whitespace(file_paths):
96 fixed = []
97 for path in file_paths:
98 try:
99 with open(path, 'rb') as f:
100 lines = f.readlines()
101 new_lines = [ws_re.sub(br'\1', line) for line in lines]
102 if new_lines != lines:
103 shutil.copyfile(path, path + '.bak')
104 with open(path, 'wb') as f:
105 f.writelines(new_lines)
106 fixed.append(path)
107 except Exception as err:
108 print('Cannot fix %s: %s' % (path, err))
109 return fixed
110
111
Christian Heimesada8c3b2008-03-18 18:26:33 +0000112@status("Docs modified", modal=True)
113def docs_modified(file_paths):
Brett Cannon058173e2010-07-04 22:05:34 +0000114 """Report if any file in the Doc directory has been changed."""
115 return bool(file_paths)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000116
Florent Xiclunae4a33802010-08-09 12:24:20 +0000117
Christian Heimesada8c3b2008-03-18 18:26:33 +0000118@status("Misc/ACKS updated", modal=True)
119def credit_given(file_paths):
120 """Check if Misc/ACKS has been changed."""
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000121 return 'Misc/ACKS' in file_paths
Christian Heimesada8c3b2008-03-18 18:26:33 +0000122
Florent Xiclunae4a33802010-08-09 12:24:20 +0000123
Christian Heimesada8c3b2008-03-18 18:26:33 +0000124@status("Misc/NEWS updated", modal=True)
125def reported_news(file_paths):
126 """Check if Misc/NEWS has been changed."""
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000127 return 'Misc/NEWS' in file_paths
Christian Heimesada8c3b2008-03-18 18:26:33 +0000128
129
130def main():
131 file_paths = changed_files()
Brett Cannon058173e2010-07-04 22:05:34 +0000132 python_files = [fn for fn in file_paths if fn.endswith('.py')]
133 c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))]
Georg Brandla9afb682010-10-21 12:49:28 +0000134 doc_files = [fn for fn in file_paths if fn.startswith('Doc')]
Brett Cannon058173e2010-07-04 22:05:34 +0000135 special_files = {'Misc/ACKS', 'Misc/NEWS'} & set(file_paths)
136 # PEP 8 whitespace rules enforcement.
137 normalize_whitespace(python_files)
Georg Brandla9afb682010-10-21 12:49:28 +0000138 # C rules enforcement.
139 normalize_c_whitespace(c_files)
140 # Doc whitespace enforcement.
141 normalize_docs_whitespace(doc_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000142 # Docs updated.
Georg Brandla9afb682010-10-21 12:49:28 +0000143 docs_modified(doc_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000144 # Misc/ACKS changed.
Brett Cannon058173e2010-07-04 22:05:34 +0000145 credit_given(special_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000146 # Misc/NEWS changed.
Brett Cannon058173e2010-07-04 22:05:34 +0000147 reported_news(special_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000148
149 # Test suite run and passed.
150 print()
151 print("Did you run the test suite?")
152
153
154if __name__ == '__main__':
155 main()