blob: 7d001e8680b0d4a1e6246054ee26cfcda73d2c34 [file] [log] [blame]
Christian Heimesada8c3b2008-03-18 18:26:33 +00001import os.path
2import subprocess
3import sys
4
5import reindent
6
7
Brett Cannon058173e2010-07-04 22:05:34 +00008def n_files_str(count):
9 """Return 'N file(s)' with the proper plurality on 'file'."""
10 return "{} file{}".format(count, "s" if count != 1 else "")
11
Christian Heimesada8c3b2008-03-18 18:26:33 +000012def status(message, modal=False, info=None):
13 """Decorator to output status info to stdout."""
14 def decorated_fxn(fxn):
15 def call_fxn(*args, **kwargs):
16 sys.stdout.write(message + ' ... ')
17 sys.stdout.flush()
18 result = fxn(*args, **kwargs)
19 if not modal and not info:
20 print("done")
21 elif info:
22 print(info(result))
23 else:
24 if result:
25 print("yes")
26 else:
27 print("NO")
28 return result
29 return call_fxn
30 return decorated_fxn
31
32@status("Getting the list of files that have been added/changed",
Brett Cannon058173e2010-07-04 22:05:34 +000033 info=lambda x: n_files_str(len(x)))
Christian Heimesada8c3b2008-03-18 18:26:33 +000034def changed_files():
35 """Run ``svn status`` and return a set of files that have been
36 changed/added."""
37 cmd = 'svn status --quiet --non-interactive --ignore-externals'
38 svn_st = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
39 svn_st.wait()
Brett Cannon5396a662008-03-18 21:45:57 +000040 output = [x.decode().rstrip() for x in svn_st.stdout.readlines()]
Christian Heimesada8c3b2008-03-18 18:26:33 +000041 files = set()
42 for line in output:
43 if not line[0] in ('A', 'M'):
44 continue
45 line_parts = line.split()
46 path = line_parts[-1]
47 if os.path.isfile(path):
48 files.add(path)
49 return files
50
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
61@status("Fixing whitespace", info=report_modified_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +000062def normalize_whitespace(file_paths):
63 """Make sure that the whitespace for .py files have been normalized."""
64 reindent.makebackup = False # No need to create backups.
Brett Cannon058173e2010-07-04 22:05:34 +000065 fixed = []
66 for path in (x for x in file_paths if x.endswith('.py')):
67 if reindent.check(path):
68 fixed.append(path)
69 return fixed
Christian Heimesada8c3b2008-03-18 18:26:33 +000070
71@status("Docs modified", modal=True)
72def docs_modified(file_paths):
Brett Cannon058173e2010-07-04 22:05:34 +000073 """Report if any file in the Doc directory has been changed."""
74 return bool(file_paths)
Christian Heimesada8c3b2008-03-18 18:26:33 +000075
76@status("Misc/ACKS updated", modal=True)
77def credit_given(file_paths):
78 """Check if Misc/ACKS has been changed."""
Benjamin Peterson058e31e2009-01-16 03:54:08 +000079 return 'Misc/ACKS' in file_paths
Christian Heimesada8c3b2008-03-18 18:26:33 +000080
81@status("Misc/NEWS updated", modal=True)
82def reported_news(file_paths):
83 """Check if Misc/NEWS has been changed."""
Benjamin Peterson058e31e2009-01-16 03:54:08 +000084 return 'Misc/NEWS' in file_paths
Christian Heimesada8c3b2008-03-18 18:26:33 +000085
86
87def main():
88 file_paths = changed_files()
Brett Cannon058173e2010-07-04 22:05:34 +000089 python_files = [fn for fn in file_paths if fn.endswith('.py')]
90 c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))]
91 docs = [fn for fn in file_paths if fn.startswith('Doc')]
92 special_files = {'Misc/ACKS', 'Misc/NEWS'} & set(file_paths)
93 # PEP 8 whitespace rules enforcement.
94 normalize_whitespace(python_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +000095 # Docs updated.
Brett Cannon058173e2010-07-04 22:05:34 +000096 docs_modified(docs)
Christian Heimesada8c3b2008-03-18 18:26:33 +000097 # Misc/ACKS changed.
Brett Cannon058173e2010-07-04 22:05:34 +000098 credit_given(special_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +000099 # Misc/NEWS changed.
Brett Cannon058173e2010-07-04 22:05:34 +0000100 reported_news(special_files)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000101
102 # Test suite run and passed.
103 print()
104 print("Did you run the test suite?")
105
106
107if __name__ == '__main__':
108 main()