blob: b01f77cec6ce816e84a9f47bfb36a9e87698299e [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
Éric Araujoa3e072b2011-07-30 21:34:04 +02007import sysconfig
Christian Heimesada8c3b2008-03-18 18:26:33 +00008
9import reindent
Georg Brandla9afb682010-10-21 12:49:28 +000010import untabify
Christian Heimesada8c3b2008-03-18 18:26:33 +000011
12
Éric Araujoa3e072b2011-07-30 21:34:04 +020013SRCDIR = sysconfig.get_config_var('srcdir')
14
15
Brett Cannon058173e2010-07-04 22:05:34 +000016def 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 Xiclunae4a33802010-08-09 12:24:20 +000020
Christian Heimesada8c3b2008-03-18 18:26:33 +000021def 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 Xiclunae4a33802010-08-09 12:24:20 +000033 print("yes" if result else "NO")
Christian Heimesada8c3b2008-03-18 18:26:33 +000034 return result
35 return call_fxn
36 return decorated_fxn
37
Florent Xiclunae4a33802010-08-09 12:24:20 +000038
Christian Heimesada8c3b2008-03-18 18:26:33 +000039@status("Getting the list of files that have been added/changed",
Georg Brandla9afb682010-10-21 12:49:28 +000040 info=lambda x: n_files_str(len(x)))
Christian Heimesada8c3b2008-03-18 18:26:33 +000041def changed_files():
Georg Brandla9afb682010-10-21 12:49:28 +000042 """Get the list of changed or added files from the VCS."""
Éric Araujoa3e072b2011-07-30 21:34:04 +020043 if os.path.isdir(os.path.join(SRCDIR, '.hg')):
Georg Brandla9afb682010-10-21 12:49:28 +000044 cmd = 'hg status --added --modified --no-status'
Georg Brandla9afb682010-10-21 12:49:28 +000045 else:
46 sys.exit('need a checkout to get modified files')
47
48 st = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
Éric Araujo1e600dc2010-11-22 03:13:47 +000049 try:
50 st.wait()
Benjamin Peterson4177eff2011-06-27 18:25:06 -050051 return [x.decode().rstrip() for x in st.stdout]
Éric Araujo1e600dc2010-11-22 03:13:47 +000052 finally:
53 st.stdout.close()
Florent Xiclunae4a33802010-08-09 12:24:20 +000054
Christian Heimesada8c3b2008-03-18 18:26:33 +000055
Brett Cannon058173e2010-07-04 22:05:34 +000056def report_modified_files(file_paths):
57 count = len(file_paths)
58 if count == 0:
59 return n_files_str(count)
60 else:
61 lines = ["{}:".format(n_files_str(count))]
62 for path in file_paths:
63 lines.append(" {}".format(path))
64 return "\n".join(lines)
65
Florent Xiclunae4a33802010-08-09 12:24:20 +000066
Brett Cannon058173e2010-07-04 22:05:34 +000067@status("Fixing whitespace", info=report_modified_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +000068def normalize_whitespace(file_paths):
69 """Make sure that the whitespace for .py files have been normalized."""
70 reindent.makebackup = False # No need to create backups.
Benjamin Peterson4177eff2011-06-27 18:25:06 -050071 fixed = [path for path in file_paths if path.endswith('.py') and
Éric Araujoad548b82011-07-31 18:33:00 +020072 reindent.check(os.path.join(SRCDIR, path))]
Brett Cannon058173e2010-07-04 22:05:34 +000073 return fixed
Christian Heimesada8c3b2008-03-18 18:26:33 +000074
Florent Xiclunae4a33802010-08-09 12:24:20 +000075
Georg Brandla9afb682010-10-21 12:49:28 +000076@status("Fixing C file whitespace", info=report_modified_files)
77def normalize_c_whitespace(file_paths):
78 """Report if any C files """
79 fixed = []
80 for path in file_paths:
Éric Araujoa3e072b2011-07-30 21:34:04 +020081 abspath = os.path.join(SRCDIR, path)
82 with open(abspath, 'r') as f:
Georg Brandla9afb682010-10-21 12:49:28 +000083 if '\t' not in f.read():
84 continue
Éric Araujoa3e072b2011-07-30 21:34:04 +020085 untabify.process(abspath, 8, verbose=False)
Georg Brandla9afb682010-10-21 12:49:28 +000086 fixed.append(path)
87 return fixed
88
89
90ws_re = re.compile(br'\s+(\r?\n)$')
91
92@status("Fixing docs whitespace", info=report_modified_files)
93def normalize_docs_whitespace(file_paths):
94 fixed = []
95 for path in file_paths:
Éric Araujoa3e072b2011-07-30 21:34:04 +020096 abspath = os.path.join(SRCDIR, path)
Georg Brandla9afb682010-10-21 12:49:28 +000097 try:
Éric Araujoa3e072b2011-07-30 21:34:04 +020098 with open(abspath, 'rb') as f:
Georg Brandla9afb682010-10-21 12:49:28 +000099 lines = f.readlines()
100 new_lines = [ws_re.sub(br'\1', line) for line in lines]
101 if new_lines != lines:
Éric Araujoa3e072b2011-07-30 21:34:04 +0200102 shutil.copyfile(abspath, abspath + '.bak')
103 with open(abspath, 'wb') as f:
Georg Brandla9afb682010-10-21 12:49:28 +0000104 f.writelines(new_lines)
105 fixed.append(path)
106 except Exception as err:
107 print('Cannot fix %s: %s' % (path, err))
108 return fixed
109
110
Christian Heimesada8c3b2008-03-18 18:26:33 +0000111@status("Docs modified", modal=True)
112def docs_modified(file_paths):
Brett Cannon058173e2010-07-04 22:05:34 +0000113 """Report if any file in the Doc directory has been changed."""
114 return bool(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/ACKS updated", modal=True)
118def credit_given(file_paths):
119 """Check if Misc/ACKS has been changed."""
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000120 return 'Misc/ACKS' in file_paths
Christian Heimesada8c3b2008-03-18 18:26:33 +0000121
Florent Xiclunae4a33802010-08-09 12:24:20 +0000122
Christian Heimesada8c3b2008-03-18 18:26:33 +0000123@status("Misc/NEWS updated", modal=True)
124def reported_news(file_paths):
125 """Check if Misc/NEWS has been changed."""
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000126 return 'Misc/NEWS' in file_paths
Christian Heimesada8c3b2008-03-18 18:26:33 +0000127
128
129def main():
130 file_paths = changed_files()
Brett Cannon058173e2010-07-04 22:05:34 +0000131 python_files = [fn for fn in file_paths if fn.endswith('.py')]
132 c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))]
Georg Brandla9afb682010-10-21 12:49:28 +0000133 doc_files = [fn for fn in file_paths if fn.startswith('Doc')]
Brett Cannon058173e2010-07-04 22:05:34 +0000134 special_files = {'Misc/ACKS', 'Misc/NEWS'} & set(file_paths)
135 # PEP 8 whitespace rules enforcement.
136 normalize_whitespace(python_files)
Georg Brandla9afb682010-10-21 12:49:28 +0000137 # C rules enforcement.
138 normalize_c_whitespace(c_files)
139 # Doc whitespace enforcement.
140 normalize_docs_whitespace(doc_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000141 # Docs updated.
Georg Brandla9afb682010-10-21 12:49:28 +0000142 docs_modified(doc_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000143 # Misc/ACKS changed.
Brett Cannon058173e2010-07-04 22:05:34 +0000144 credit_given(special_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000145 # Misc/NEWS changed.
Brett Cannon058173e2010-07-04 22:05:34 +0000146 reported_news(special_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000147
148 # Test suite run and passed.
149 print()
150 print("Did you run the test suite?")
151
152
153if __name__ == '__main__':
154 main()