blob: 6f9821bdd658ae429c7346156ea38934589cb972 [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
Nadeem Vawda9f64f732012-02-22 11:46:41 +020039def mq_patches_applied():
40 """Check if there are any applied MQ patches."""
41 cmd = 'hg qapplied'
42 with subprocess.Popen(cmd.split(),
43 stdout=subprocess.PIPE,
44 stderr=subprocess.PIPE) as st:
45 bstdout, _ = st.communicate()
46 return st.returncode == 0 and bstdout
47
48
Christian Heimesada8c3b2008-03-18 18:26:33 +000049@status("Getting the list of files that have been added/changed",
Georg Brandla9afb682010-10-21 12:49:28 +000050 info=lambda x: n_files_str(len(x)))
Christian Heimesada8c3b2008-03-18 18:26:33 +000051def changed_files():
Éric Araujo56ec5fe2011-07-31 18:41:25 +020052 """Get the list of changed or added files from Mercurial."""
53 if not os.path.isdir(os.path.join(SRCDIR, '.hg')):
Georg Brandla9afb682010-10-21 12:49:28 +000054 sys.exit('need a checkout to get modified files')
55
Éric Araujo56ec5fe2011-07-31 18:41:25 +020056 cmd = 'hg status --added --modified --no-status'
Nadeem Vawda67211492012-02-22 11:53:09 +020057 if mq_patches_applied():
58 cmd += ' --rev qparent'
Éric Araujo56ec5fe2011-07-31 18:41:25 +020059 with subprocess.Popen(cmd.split(), stdout=subprocess.PIPE) as st:
Benjamin Peterson4177eff2011-06-27 18:25:06 -050060 return [x.decode().rstrip() for x in st.stdout]
Florent Xiclunae4a33802010-08-09 12:24:20 +000061
Christian Heimesada8c3b2008-03-18 18:26:33 +000062
Brett Cannon058173e2010-07-04 22:05:34 +000063def report_modified_files(file_paths):
64 count = len(file_paths)
65 if count == 0:
66 return n_files_str(count)
67 else:
68 lines = ["{}:".format(n_files_str(count))]
69 for path in file_paths:
70 lines.append(" {}".format(path))
71 return "\n".join(lines)
72
Florent Xiclunae4a33802010-08-09 12:24:20 +000073
Brett Cannon058173e2010-07-04 22:05:34 +000074@status("Fixing whitespace", info=report_modified_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +000075def normalize_whitespace(file_paths):
76 """Make sure that the whitespace for .py files have been normalized."""
77 reindent.makebackup = False # No need to create backups.
Benjamin Peterson4177eff2011-06-27 18:25:06 -050078 fixed = [path for path in file_paths if path.endswith('.py') and
Éric Araujoad548b82011-07-31 18:33:00 +020079 reindent.check(os.path.join(SRCDIR, path))]
Brett Cannon058173e2010-07-04 22:05:34 +000080 return fixed
Christian Heimesada8c3b2008-03-18 18:26:33 +000081
Florent Xiclunae4a33802010-08-09 12:24:20 +000082
Georg Brandla9afb682010-10-21 12:49:28 +000083@status("Fixing C file whitespace", info=report_modified_files)
84def normalize_c_whitespace(file_paths):
85 """Report if any C files """
86 fixed = []
87 for path in file_paths:
Éric Araujoa3e072b2011-07-30 21:34:04 +020088 abspath = os.path.join(SRCDIR, path)
89 with open(abspath, 'r') as f:
Georg Brandla9afb682010-10-21 12:49:28 +000090 if '\t' not in f.read():
91 continue
Éric Araujoa3e072b2011-07-30 21:34:04 +020092 untabify.process(abspath, 8, verbose=False)
Georg Brandla9afb682010-10-21 12:49:28 +000093 fixed.append(path)
94 return fixed
95
96
97ws_re = re.compile(br'\s+(\r?\n)$')
98
99@status("Fixing docs whitespace", info=report_modified_files)
100def normalize_docs_whitespace(file_paths):
101 fixed = []
102 for path in file_paths:
Éric Araujoa3e072b2011-07-30 21:34:04 +0200103 abspath = os.path.join(SRCDIR, path)
Georg Brandla9afb682010-10-21 12:49:28 +0000104 try:
Éric Araujoa3e072b2011-07-30 21:34:04 +0200105 with open(abspath, 'rb') as f:
Georg Brandla9afb682010-10-21 12:49:28 +0000106 lines = f.readlines()
107 new_lines = [ws_re.sub(br'\1', line) for line in lines]
108 if new_lines != lines:
Éric Araujoa3e072b2011-07-30 21:34:04 +0200109 shutil.copyfile(abspath, abspath + '.bak')
110 with open(abspath, 'wb') as f:
Georg Brandla9afb682010-10-21 12:49:28 +0000111 f.writelines(new_lines)
112 fixed.append(path)
113 except Exception as err:
114 print('Cannot fix %s: %s' % (path, err))
115 return fixed
116
117
Christian Heimesada8c3b2008-03-18 18:26:33 +0000118@status("Docs modified", modal=True)
119def docs_modified(file_paths):
Brett Cannon058173e2010-07-04 22:05:34 +0000120 """Report if any file in the Doc directory has been changed."""
121 return bool(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/ACKS updated", modal=True)
125def credit_given(file_paths):
126 """Check if Misc/ACKS has been changed."""
Terry Jan Reedy6e2711b2013-07-21 20:57:44 -0400127 return os.path.join('Misc', 'ACKS') in file_paths
Christian Heimesada8c3b2008-03-18 18:26:33 +0000128
Florent Xiclunae4a33802010-08-09 12:24:20 +0000129
Christian Heimesada8c3b2008-03-18 18:26:33 +0000130@status("Misc/NEWS updated", modal=True)
131def reported_news(file_paths):
132 """Check if Misc/NEWS has been changed."""
Terry Jan Reedy6e2711b2013-07-21 20:57:44 -0400133 return os.path.join('Misc', 'NEWS') in file_paths
Christian Heimesada8c3b2008-03-18 18:26:33 +0000134
Ross Lagerwall6c52c572012-03-11 19:21:07 +0200135@status("configure regenerated", modal=True, info=str)
136def regenerated_configure(file_paths):
137 """Check if configure has been regenerated."""
Matthias Klose5ce31cc2012-03-14 23:17:31 +0100138 if 'configure.ac' in file_paths:
Ross Lagerwall6c52c572012-03-11 19:21:07 +0200139 return "yes" if 'configure' in file_paths else "no"
140 else:
141 return "not needed"
142
143@status("pyconfig.h.in regenerated", modal=True, info=str)
144def regenerated_pyconfig_h_in(file_paths):
145 """Check if pyconfig.h.in has been regenerated."""
Matthias Klose5ce31cc2012-03-14 23:17:31 +0100146 if 'configure.ac' in file_paths:
Ross Lagerwall6c52c572012-03-11 19:21:07 +0200147 return "yes" if 'pyconfig.h.in' in file_paths else "no"
148 else:
149 return "not needed"
Christian Heimesada8c3b2008-03-18 18:26:33 +0000150
151def main():
152 file_paths = changed_files()
Brett Cannon058173e2010-07-04 22:05:34 +0000153 python_files = [fn for fn in file_paths if fn.endswith('.py')]
154 c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))]
Georg Brandla9afb682010-10-21 12:49:28 +0000155 doc_files = [fn for fn in file_paths if fn.startswith('Doc')]
Terry Jan Reedy6e2711b2013-07-21 20:57:44 -0400156 misc_files = {os.path.join('Misc', 'ACKS'), os.path.join('Misc', 'NEWS')}\
157 & set(file_paths)
Brett Cannon058173e2010-07-04 22:05:34 +0000158 # PEP 8 whitespace rules enforcement.
159 normalize_whitespace(python_files)
Georg Brandla9afb682010-10-21 12:49:28 +0000160 # C rules enforcement.
161 normalize_c_whitespace(c_files)
162 # Doc whitespace enforcement.
163 normalize_docs_whitespace(doc_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000164 # Docs updated.
Georg Brandla9afb682010-10-21 12:49:28 +0000165 docs_modified(doc_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000166 # Misc/ACKS changed.
Terry Jan Reedy6e2711b2013-07-21 20:57:44 -0400167 credit_given(misc_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000168 # Misc/NEWS changed.
Terry Jan Reedy6e2711b2013-07-21 20:57:44 -0400169 reported_news(misc_files)
Ross Lagerwall6c52c572012-03-11 19:21:07 +0200170 # Regenerated configure, if necessary.
171 regenerated_configure(file_paths)
172 # Regenerated pyconfig.h.in, if necessary.
173 regenerated_pyconfig_h_in(file_paths)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000174
175 # Test suite run and passed.
Éric Araujofbc5ff62011-08-12 17:50:08 +0200176 if python_files or c_files:
Ezio Melotti5e12bb72013-01-11 14:07:47 +0200177 end = " and check for refleaks?" if c_files else "?"
Éric Araujofbc5ff62011-08-12 17:50:08 +0200178 print()
Ezio Melotti5e12bb72013-01-11 14:07:47 +0200179 print("Did you run the test suite" + end)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000180
181
182if __name__ == '__main__':
183 main()