Éric Araujo | 0fb681e | 2011-07-29 12:06:13 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
Georg Brandl | ef212e0 | 2010-11-26 08:04:57 +0000 | [diff] [blame] | 2 | import re |
| 3 | import sys |
| 4 | import shutil |
Brett Cannon | a8b09fd | 2008-03-18 17:25:13 +0000 | [diff] [blame] | 5 | import os.path |
| 6 | import subprocess |
Éric Araujo | 35a7f55 | 2011-07-30 21:34:04 +0200 | [diff] [blame] | 7 | import sysconfig |
Brett Cannon | a8b09fd | 2008-03-18 17:25:13 +0000 | [diff] [blame] | 8 | |
| 9 | import reindent |
Georg Brandl | ef212e0 | 2010-11-26 08:04:57 +0000 | [diff] [blame] | 10 | import untabify |
| 11 | |
| 12 | |
Victor Stinner | d45cb04 | 2017-08-17 17:13:01 +0200 | [diff] [blame] | 13 | # Excluded directories which are copies of external libraries: |
| 14 | # don't check their coding style |
| 15 | EXCLUDE_DIRS = [os.path.join('Modules', '_ctypes', 'libffi'), |
| 16 | os.path.join('Modules', '_ctypes', 'libffi_osx'), |
| 17 | os.path.join('Modules', '_ctypes', 'libffi_msvc'), |
| 18 | os.path.join('Modules', 'expat'), |
| 19 | os.path.join('Modules', 'zlib')] |
Éric Araujo | 35a7f55 | 2011-07-30 21:34:04 +0200 | [diff] [blame] | 20 | SRCDIR = sysconfig.get_config_var('srcdir') |
| 21 | |
Victor Stinner | d45cb04 | 2017-08-17 17:13:01 +0200 | [diff] [blame] | 22 | |
Georg Brandl | ef212e0 | 2010-11-26 08:04:57 +0000 | [diff] [blame] | 23 | def n_files_str(count): |
| 24 | """Return 'N file(s)' with the proper plurality on 'file'.""" |
| 25 | return "{} file{}".format(count, "s" if count != 1 else "") |
Brett Cannon | a8b09fd | 2008-03-18 17:25:13 +0000 | [diff] [blame] | 26 | |
| 27 | |
| 28 | def status(message, modal=False, info=None): |
| 29 | """Decorator to output status info to stdout.""" |
| 30 | def decorated_fxn(fxn): |
| 31 | def call_fxn(*args, **kwargs): |
| 32 | sys.stdout.write(message + ' ... ') |
| 33 | sys.stdout.flush() |
| 34 | result = fxn(*args, **kwargs) |
| 35 | if not modal and not info: |
| 36 | print "done" |
| 37 | elif info: |
| 38 | print info(result) |
| 39 | else: |
Georg Brandl | ef212e0 | 2010-11-26 08:04:57 +0000 | [diff] [blame] | 40 | print "yes" if result else "NO" |
Brett Cannon | a8b09fd | 2008-03-18 17:25:13 +0000 | [diff] [blame] | 41 | return result |
| 42 | return call_fxn |
| 43 | return decorated_fxn |
| 44 | |
Brett Cannon | a8b09fd | 2008-03-18 17:25:13 +0000 | [diff] [blame] | 45 | |
Nick Coghlan | c8869af | 2017-03-12 19:34:16 +1000 | [diff] [blame] | 46 | def get_git_branch(): |
| 47 | """Get the symbolic name for the current git branch""" |
| 48 | cmd = "git rev-parse --abbrev-ref HEAD".split() |
| 49 | try: |
| 50 | return subprocess.check_output(cmd, stderr=subprocess.PIPE) |
| 51 | except subprocess.CalledProcessError: |
| 52 | return None |
| 53 | |
| 54 | |
| 55 | def get_git_upstream_remote(): |
| 56 | """Get the remote name to use for upstream branches |
| 57 | |
| 58 | Uses "upstream" if it exists, "origin" otherwise |
| 59 | """ |
| 60 | cmd = "git remote get-url upstream".split() |
| 61 | try: |
| 62 | subprocess.check_output(cmd, stderr=subprocess.PIPE) |
| 63 | except subprocess.CalledProcessError: |
| 64 | return "origin" |
| 65 | return "upstream" |
| 66 | |
| 67 | |
| 68 | @status("Getting base branch for PR", |
| 69 | info=lambda x: x if x is not None else "not a PR branch") |
| 70 | def get_base_branch(): |
Nick Coghlan | d6d943a | 2017-04-09 18:32:48 +1000 | [diff] [blame] | 71 | if not os.path.exists(os.path.join(SRCDIR, '.git')): |
Nick Coghlan | c8869af | 2017-03-12 19:34:16 +1000 | [diff] [blame] | 72 | # Not a git checkout, so there's no base branch |
| 73 | return None |
| 74 | version = sys.version_info |
| 75 | if version.releaselevel == 'alpha': |
| 76 | base_branch = "master" |
| 77 | else: |
| 78 | base_branch = "{0.major}.{0.minor}".format(version) |
| 79 | this_branch = get_git_branch() |
| 80 | if this_branch is None or this_branch == base_branch: |
| 81 | # Not on a git PR branch, so there's no base branch |
| 82 | return None |
| 83 | upstream_remote = get_git_upstream_remote() |
| 84 | return upstream_remote + "/" + base_branch |
| 85 | |
| 86 | |
Georg Brandl | ef212e0 | 2010-11-26 08:04:57 +0000 | [diff] [blame] | 87 | @status("Getting the list of files that have been added/changed", |
| 88 | info=lambda x: n_files_str(len(x))) |
Nick Coghlan | c8869af | 2017-03-12 19:34:16 +1000 | [diff] [blame] | 89 | def changed_files(base_branch=None): |
Benjamin Peterson | 9bbb8e2 | 2018-06-05 22:55:10 -0700 | [diff] [blame] | 90 | """Get the list of changed or added files from git.""" |
| 91 | if os.path.exists(os.path.join(SRCDIR, '.git')): |
Nick Coghlan | ee10fb9 | 2017-03-12 20:03:45 +1000 | [diff] [blame] | 92 | # We just use an existence check here as: |
| 93 | # directory = normal git checkout/clone |
| 94 | # file = git worktree directory |
Nick Coghlan | c8869af | 2017-03-12 19:34:16 +1000 | [diff] [blame] | 95 | if base_branch: |
| 96 | cmd = 'git diff --name-status ' + base_branch |
| 97 | else: |
| 98 | cmd = 'git status --porcelain' |
| 99 | filenames = [] |
| 100 | st = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE) |
| 101 | try: |
| 102 | for line in st.stdout: |
| 103 | line = line.decode().rstrip() |
| 104 | status_text, filename = line.split(None, 1) |
| 105 | status = set(status_text) |
| 106 | # modified, added or unmerged files |
| 107 | if not status.intersection('MAU'): |
| 108 | continue |
| 109 | if ' -> ' in filename: |
| 110 | # file is renamed |
| 111 | filename = filename.split(' -> ', 2)[1].strip() |
| 112 | filenames.append(filename) |
| 113 | finally: |
| 114 | st.stdout.close() |
Georg Brandl | ef212e0 | 2010-11-26 08:04:57 +0000 | [diff] [blame] | 115 | else: |
Benjamin Peterson | 9bbb8e2 | 2018-06-05 22:55:10 -0700 | [diff] [blame] | 116 | sys.exit('need a git checkout to get modified files') |
Victor Stinner | d45cb04 | 2017-08-17 17:13:01 +0200 | [diff] [blame] | 117 | |
| 118 | filenames2 = [] |
| 119 | for filename in filenames: |
| 120 | # Normalize the path to be able to match using .startswith() |
| 121 | filename = os.path.normpath(filename) |
| 122 | if any(filename.startswith(path) for path in EXCLUDE_DIRS): |
| 123 | # Exclude the file |
| 124 | continue |
| 125 | filenames2.append(filename) |
| 126 | |
| 127 | return filenames2 |
Georg Brandl | ef212e0 | 2010-11-26 08:04:57 +0000 | [diff] [blame] | 128 | |
Georg Brandl | ef212e0 | 2010-11-26 08:04:57 +0000 | [diff] [blame] | 129 | |
| 130 | def report_modified_files(file_paths): |
| 131 | count = len(file_paths) |
| 132 | if count == 0: |
| 133 | return n_files_str(count) |
| 134 | else: |
| 135 | lines = ["{}:".format(n_files_str(count))] |
| 136 | for path in file_paths: |
| 137 | lines.append(" {}".format(path)) |
| 138 | return "\n".join(lines) |
| 139 | |
| 140 | |
| 141 | @status("Fixing whitespace", info=report_modified_files) |
Brett Cannon | a8b09fd | 2008-03-18 17:25:13 +0000 | [diff] [blame] | 142 | def normalize_whitespace(file_paths): |
| 143 | """Make sure that the whitespace for .py files have been normalized.""" |
| 144 | reindent.makebackup = False # No need to create backups. |
Georg Brandl | ef212e0 | 2010-11-26 08:04:57 +0000 | [diff] [blame] | 145 | fixed = [] |
| 146 | for path in (x for x in file_paths if x.endswith('.py')): |
Éric Araujo | 35a7f55 | 2011-07-30 21:34:04 +0200 | [diff] [blame] | 147 | if reindent.check(os.path.join(SRCDIR, path)): |
Georg Brandl | ef212e0 | 2010-11-26 08:04:57 +0000 | [diff] [blame] | 148 | fixed.append(path) |
| 149 | return fixed |
| 150 | |
| 151 | |
| 152 | @status("Fixing C file whitespace", info=report_modified_files) |
| 153 | def normalize_c_whitespace(file_paths): |
| 154 | """Report if any C files """ |
| 155 | fixed = [] |
| 156 | for path in file_paths: |
Éric Araujo | 35a7f55 | 2011-07-30 21:34:04 +0200 | [diff] [blame] | 157 | abspath = os.path.join(SRCDIR, path) |
| 158 | with open(abspath, 'r') as f: |
Georg Brandl | ef212e0 | 2010-11-26 08:04:57 +0000 | [diff] [blame] | 159 | if '\t' not in f.read(): |
| 160 | continue |
Éric Araujo | 35a7f55 | 2011-07-30 21:34:04 +0200 | [diff] [blame] | 161 | untabify.process(abspath, 8, verbose=False) |
Georg Brandl | ef212e0 | 2010-11-26 08:04:57 +0000 | [diff] [blame] | 162 | fixed.append(path) |
| 163 | return fixed |
| 164 | |
| 165 | |
| 166 | ws_re = re.compile(br'\s+(\r?\n)$') |
| 167 | |
| 168 | @status("Fixing docs whitespace", info=report_modified_files) |
| 169 | def normalize_docs_whitespace(file_paths): |
| 170 | fixed = [] |
| 171 | for path in file_paths: |
Éric Araujo | 35a7f55 | 2011-07-30 21:34:04 +0200 | [diff] [blame] | 172 | abspath = os.path.join(SRCDIR, path) |
Georg Brandl | ef212e0 | 2010-11-26 08:04:57 +0000 | [diff] [blame] | 173 | try: |
Éric Araujo | 35a7f55 | 2011-07-30 21:34:04 +0200 | [diff] [blame] | 174 | with open(abspath, 'rb') as f: |
Georg Brandl | ef212e0 | 2010-11-26 08:04:57 +0000 | [diff] [blame] | 175 | lines = f.readlines() |
| 176 | new_lines = [ws_re.sub(br'\1', line) for line in lines] |
| 177 | if new_lines != lines: |
Éric Araujo | 35a7f55 | 2011-07-30 21:34:04 +0200 | [diff] [blame] | 178 | shutil.copyfile(abspath, abspath + '.bak') |
| 179 | with open(abspath, 'wb') as f: |
Georg Brandl | ef212e0 | 2010-11-26 08:04:57 +0000 | [diff] [blame] | 180 | f.writelines(new_lines) |
| 181 | fixed.append(path) |
| 182 | except Exception as err: |
| 183 | print 'Cannot fix %s: %s' % (path, err) |
| 184 | return fixed |
| 185 | |
Brett Cannon | a8b09fd | 2008-03-18 17:25:13 +0000 | [diff] [blame] | 186 | |
| 187 | @status("Docs modified", modal=True) |
| 188 | def docs_modified(file_paths): |
Georg Brandl | ef212e0 | 2010-11-26 08:04:57 +0000 | [diff] [blame] | 189 | """Report if any file in the Doc directory has been changed.""" |
| 190 | return bool(file_paths) |
| 191 | |
Brett Cannon | a8b09fd | 2008-03-18 17:25:13 +0000 | [diff] [blame] | 192 | |
| 193 | @status("Misc/ACKS updated", modal=True) |
| 194 | def credit_given(file_paths): |
| 195 | """Check if Misc/ACKS has been changed.""" |
Terry Jan Reedy | 68ad1d1 | 2013-07-21 20:57:44 -0400 | [diff] [blame] | 196 | return os.path.join('Misc', 'ACKS') in file_paths |
Brett Cannon | a8b09fd | 2008-03-18 17:25:13 +0000 | [diff] [blame] | 197 | |
Georg Brandl | ef212e0 | 2010-11-26 08:04:57 +0000 | [diff] [blame] | 198 | |
Antoine Pitrou | 36af118 | 2018-04-23 14:22:15 +0200 | [diff] [blame] | 199 | @status("Misc/NEWS.d updated with `blurb`", modal=True) |
Brett Cannon | a8b09fd | 2008-03-18 17:25:13 +0000 | [diff] [blame] | 200 | def reported_news(file_paths): |
Antoine Pitrou | 36af118 | 2018-04-23 14:22:15 +0200 | [diff] [blame] | 201 | """Check if Misc/NEWS.d has been changed.""" |
| 202 | return any(p.startswith(os.path.join('Misc', 'NEWS.d', 'next')) |
| 203 | for p in file_paths) |
Brett Cannon | a8b09fd | 2008-03-18 17:25:13 +0000 | [diff] [blame] | 204 | |
| 205 | |
| 206 | def main(): |
Nick Coghlan | c8869af | 2017-03-12 19:34:16 +1000 | [diff] [blame] | 207 | base_branch = get_base_branch() |
| 208 | file_paths = changed_files(base_branch) |
Georg Brandl | ef212e0 | 2010-11-26 08:04:57 +0000 | [diff] [blame] | 209 | python_files = [fn for fn in file_paths if fn.endswith('.py')] |
| 210 | c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))] |
Georg Brandl | 6a1184c | 2014-10-19 11:54:08 +0200 | [diff] [blame] | 211 | doc_files = [fn for fn in file_paths if fn.startswith('Doc') and |
| 212 | fn.endswith(('.rst', '.inc'))] |
Antoine Pitrou | 36af118 | 2018-04-23 14:22:15 +0200 | [diff] [blame] | 213 | misc_files = {p for p in file_paths if p.startswith('Misc')} |
Georg Brandl | ef212e0 | 2010-11-26 08:04:57 +0000 | [diff] [blame] | 214 | # PEP 8 whitespace rules enforcement. |
| 215 | normalize_whitespace(python_files) |
| 216 | # C rules enforcement. |
| 217 | normalize_c_whitespace(c_files) |
| 218 | # Doc whitespace enforcement. |
| 219 | normalize_docs_whitespace(doc_files) |
Brett Cannon | a8b09fd | 2008-03-18 17:25:13 +0000 | [diff] [blame] | 220 | # Docs updated. |
Georg Brandl | ef212e0 | 2010-11-26 08:04:57 +0000 | [diff] [blame] | 221 | docs_modified(doc_files) |
Brett Cannon | a8b09fd | 2008-03-18 17:25:13 +0000 | [diff] [blame] | 222 | # Misc/ACKS changed. |
Terry Jan Reedy | 68ad1d1 | 2013-07-21 20:57:44 -0400 | [diff] [blame] | 223 | credit_given(misc_files) |
Brett Cannon | a8b09fd | 2008-03-18 17:25:13 +0000 | [diff] [blame] | 224 | # Misc/NEWS changed. |
Terry Jan Reedy | 68ad1d1 | 2013-07-21 20:57:44 -0400 | [diff] [blame] | 225 | reported_news(misc_files) |
Brett Cannon | a8b09fd | 2008-03-18 17:25:13 +0000 | [diff] [blame] | 226 | |
| 227 | # Test suite run and passed. |
Éric Araujo | a5afa49 | 2011-08-19 08:41:00 +0200 | [diff] [blame] | 228 | if python_files or c_files: |
Ezio Melotti | 9e9cb28 | 2013-01-11 14:07:47 +0200 | [diff] [blame] | 229 | end = " and check for refleaks?" if c_files else "?" |
Éric Araujo | a5afa49 | 2011-08-19 08:41:00 +0200 | [diff] [blame] | 230 | print |
Ezio Melotti | 9e9cb28 | 2013-01-11 14:07:47 +0200 | [diff] [blame] | 231 | print "Did you run the test suite" + end |
Brett Cannon | a8b09fd | 2008-03-18 17:25:13 +0000 | [diff] [blame] | 232 | |
| 233 | |
| 234 | if __name__ == '__main__': |
| 235 | main() |