Éric Araujo | a0e92a8 | 2011-07-26 18:01:08 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Brett Cannon | 57ee0c8 | 2017-06-24 17:59:49 -0700 | [diff] [blame] | 2 | """Check proposed changes for common issues.""" |
Georg Brandl | a9afb68 | 2010-10-21 12:49:28 +0000 | [diff] [blame] | 3 | import re |
| 4 | import sys |
| 5 | import shutil |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 6 | import os.path |
| 7 | import subprocess |
Éric Araujo | a3e072b | 2011-07-30 21:34:04 +0200 | [diff] [blame] | 8 | import sysconfig |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 9 | |
| 10 | import reindent |
Georg Brandl | a9afb68 | 2010-10-21 12:49:28 +0000 | [diff] [blame] | 11 | import untabify |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 12 | |
| 13 | |
Éric Araujo | a3e072b | 2011-07-30 21:34:04 +0200 | [diff] [blame] | 14 | SRCDIR = sysconfig.get_config_var('srcdir') |
| 15 | |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 16 | def 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 Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 20 | |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 21 | def 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 Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 33 | print("yes" if result else "NO") |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 34 | return result |
| 35 | return call_fxn |
| 36 | return decorated_fxn |
| 37 | |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 38 | |
Nadeem Vawda | 9f64f73 | 2012-02-22 11:46:41 +0200 | [diff] [blame] | 39 | def 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 | |
Nick Coghlan | 2f38625 | 2017-03-12 16:17:46 +1000 | [diff] [blame] | 49 | def get_git_branch(): |
| 50 | """Get the symbolic name for the current git branch""" |
| 51 | cmd = "git rev-parse --abbrev-ref HEAD".split() |
| 52 | try: |
| 53 | return subprocess.check_output(cmd, stderr=subprocess.DEVNULL) |
| 54 | except subprocess.CalledProcessError: |
| 55 | return None |
| 56 | |
| 57 | |
| 58 | def get_git_upstream_remote(): |
| 59 | """Get the remote name to use for upstream branches |
| 60 | |
| 61 | Uses "upstream" if it exists, "origin" otherwise |
| 62 | """ |
| 63 | cmd = "git remote get-url upstream".split() |
| 64 | try: |
| 65 | subprocess.check_output(cmd, stderr=subprocess.DEVNULL) |
| 66 | except subprocess.CalledProcessError: |
| 67 | return "origin" |
| 68 | return "upstream" |
| 69 | |
| 70 | |
| 71 | @status("Getting base branch for PR", |
| 72 | info=lambda x: x if x is not None else "not a PR branch") |
| 73 | def get_base_branch(): |
Nick Coghlan | 4c116cb | 2017-04-09 19:22:36 +1000 | [diff] [blame] | 74 | if not os.path.exists(os.path.join(SRCDIR, '.git')): |
Nick Coghlan | 2f38625 | 2017-03-12 16:17:46 +1000 | [diff] [blame] | 75 | # Not a git checkout, so there's no base branch |
| 76 | return None |
| 77 | version = sys.version_info |
| 78 | if version.releaselevel == 'alpha': |
| 79 | base_branch = "master" |
| 80 | else: |
| 81 | base_branch = "{0.major}.{0.minor}".format(version) |
| 82 | this_branch = get_git_branch() |
| 83 | if this_branch is None or this_branch == base_branch: |
| 84 | # Not on a git PR branch, so there's no base branch |
| 85 | return None |
| 86 | upstream_remote = get_git_upstream_remote() |
| 87 | return upstream_remote + "/" + base_branch |
| 88 | |
| 89 | |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 90 | @status("Getting the list of files that have been added/changed", |
Georg Brandl | a9afb68 | 2010-10-21 12:49:28 +0000 | [diff] [blame] | 91 | info=lambda x: n_files_str(len(x))) |
Nick Coghlan | 2f38625 | 2017-03-12 16:17:46 +1000 | [diff] [blame] | 92 | def changed_files(base_branch=None): |
Christian Heimes | d98c677 | 2015-04-23 11:24:14 +0200 | [diff] [blame] | 93 | """Get the list of changed or added files from Mercurial or git.""" |
| 94 | if os.path.isdir(os.path.join(SRCDIR, '.hg')): |
Nick Coghlan | 2f38625 | 2017-03-12 16:17:46 +1000 | [diff] [blame] | 95 | if base_branch is not None: |
| 96 | sys.exit('need a git checkout to check PR status') |
Christian Heimes | d98c677 | 2015-04-23 11:24:14 +0200 | [diff] [blame] | 97 | cmd = 'hg status --added --modified --no-status' |
| 98 | if mq_patches_applied(): |
| 99 | cmd += ' --rev qparent' |
| 100 | with subprocess.Popen(cmd.split(), stdout=subprocess.PIPE) as st: |
| 101 | return [x.decode().rstrip() for x in st.stdout] |
Nick Coghlan | 61a82a5 | 2017-03-12 20:00:20 +1000 | [diff] [blame] | 102 | elif os.path.exists(os.path.join(SRCDIR, '.git')): |
| 103 | # We just use an existence check here as: |
| 104 | # directory = normal git checkout/clone |
| 105 | # file = git worktree directory |
Nick Coghlan | 2f38625 | 2017-03-12 16:17:46 +1000 | [diff] [blame] | 106 | if base_branch: |
| 107 | cmd = 'git diff --name-status ' + base_branch |
| 108 | else: |
| 109 | cmd = 'git status --porcelain' |
Christian Heimes | d98c677 | 2015-04-23 11:24:14 +0200 | [diff] [blame] | 110 | filenames = [] |
| 111 | with subprocess.Popen(cmd.split(), stdout=subprocess.PIPE) as st: |
| 112 | for line in st.stdout: |
| 113 | line = line.decode().rstrip() |
Nick Coghlan | 2f38625 | 2017-03-12 16:17:46 +1000 | [diff] [blame] | 114 | status_text, filename = line.split(maxsplit=1) |
| 115 | status = set(status_text) |
Christian Heimes | d98c677 | 2015-04-23 11:24:14 +0200 | [diff] [blame] | 116 | # modified, added or unmerged files |
| 117 | if not status.intersection('MAU'): |
| 118 | continue |
Christian Heimes | d98c677 | 2015-04-23 11:24:14 +0200 | [diff] [blame] | 119 | if ' -> ' in filename: |
| 120 | # file is renamed |
| 121 | filename = filename.split(' -> ', 2)[1].strip() |
| 122 | filenames.append(filename) |
| 123 | return filenames |
| 124 | else: |
| 125 | sys.exit('need a Mercurial or git checkout to get modified files') |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 126 | |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 127 | |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 128 | def report_modified_files(file_paths): |
| 129 | count = len(file_paths) |
| 130 | if count == 0: |
| 131 | return n_files_str(count) |
| 132 | else: |
| 133 | lines = ["{}:".format(n_files_str(count))] |
| 134 | for path in file_paths: |
| 135 | lines.append(" {}".format(path)) |
| 136 | return "\n".join(lines) |
| 137 | |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 138 | |
Brett Cannon | 57ee0c8 | 2017-06-24 17:59:49 -0700 | [diff] [blame] | 139 | @status("Fixing Python file whitespace", info=report_modified_files) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 140 | def normalize_whitespace(file_paths): |
| 141 | """Make sure that the whitespace for .py files have been normalized.""" |
| 142 | reindent.makebackup = False # No need to create backups. |
Benjamin Peterson | 4177eff | 2011-06-27 18:25:06 -0500 | [diff] [blame] | 143 | fixed = [path for path in file_paths if path.endswith('.py') and |
Éric Araujo | ad548b8 | 2011-07-31 18:33:00 +0200 | [diff] [blame] | 144 | reindent.check(os.path.join(SRCDIR, path))] |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 145 | return fixed |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 146 | |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 147 | |
Georg Brandl | a9afb68 | 2010-10-21 12:49:28 +0000 | [diff] [blame] | 148 | @status("Fixing C file whitespace", info=report_modified_files) |
| 149 | def normalize_c_whitespace(file_paths): |
| 150 | """Report if any C files """ |
| 151 | fixed = [] |
| 152 | for path in file_paths: |
Éric Araujo | a3e072b | 2011-07-30 21:34:04 +0200 | [diff] [blame] | 153 | abspath = os.path.join(SRCDIR, path) |
| 154 | with open(abspath, 'r') as f: |
Georg Brandl | a9afb68 | 2010-10-21 12:49:28 +0000 | [diff] [blame] | 155 | if '\t' not in f.read(): |
| 156 | continue |
Éric Araujo | a3e072b | 2011-07-30 21:34:04 +0200 | [diff] [blame] | 157 | untabify.process(abspath, 8, verbose=False) |
Georg Brandl | a9afb68 | 2010-10-21 12:49:28 +0000 | [diff] [blame] | 158 | fixed.append(path) |
| 159 | return fixed |
| 160 | |
| 161 | |
| 162 | ws_re = re.compile(br'\s+(\r?\n)$') |
| 163 | |
| 164 | @status("Fixing docs whitespace", info=report_modified_files) |
| 165 | def normalize_docs_whitespace(file_paths): |
| 166 | fixed = [] |
| 167 | for path in file_paths: |
Éric Araujo | a3e072b | 2011-07-30 21:34:04 +0200 | [diff] [blame] | 168 | abspath = os.path.join(SRCDIR, path) |
Georg Brandl | a9afb68 | 2010-10-21 12:49:28 +0000 | [diff] [blame] | 169 | try: |
Éric Araujo | a3e072b | 2011-07-30 21:34:04 +0200 | [diff] [blame] | 170 | with open(abspath, 'rb') as f: |
Georg Brandl | a9afb68 | 2010-10-21 12:49:28 +0000 | [diff] [blame] | 171 | lines = f.readlines() |
| 172 | new_lines = [ws_re.sub(br'\1', line) for line in lines] |
| 173 | if new_lines != lines: |
Éric Araujo | a3e072b | 2011-07-30 21:34:04 +0200 | [diff] [blame] | 174 | shutil.copyfile(abspath, abspath + '.bak') |
| 175 | with open(abspath, 'wb') as f: |
Georg Brandl | a9afb68 | 2010-10-21 12:49:28 +0000 | [diff] [blame] | 176 | f.writelines(new_lines) |
| 177 | fixed.append(path) |
| 178 | except Exception as err: |
| 179 | print('Cannot fix %s: %s' % (path, err)) |
| 180 | return fixed |
| 181 | |
| 182 | |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 183 | @status("Docs modified", modal=True) |
| 184 | def docs_modified(file_paths): |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 185 | """Report if any file in the Doc directory has been changed.""" |
| 186 | return bool(file_paths) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 187 | |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 188 | |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 189 | @status("Misc/ACKS updated", modal=True) |
| 190 | def credit_given(file_paths): |
| 191 | """Check if Misc/ACKS has been changed.""" |
Terry Jan Reedy | 6e2711b | 2013-07-21 20:57:44 -0400 | [diff] [blame] | 192 | return os.path.join('Misc', 'ACKS') in file_paths |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 193 | |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 194 | |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 195 | @status("Misc/NEWS updated", modal=True) |
| 196 | def reported_news(file_paths): |
| 197 | """Check if Misc/NEWS has been changed.""" |
Terry Jan Reedy | 6e2711b | 2013-07-21 20:57:44 -0400 | [diff] [blame] | 198 | return os.path.join('Misc', 'NEWS') in file_paths |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 199 | |
Ross Lagerwall | 6c52c57 | 2012-03-11 19:21:07 +0200 | [diff] [blame] | 200 | @status("configure regenerated", modal=True, info=str) |
| 201 | def regenerated_configure(file_paths): |
| 202 | """Check if configure has been regenerated.""" |
Matthias Klose | 5ce31cc | 2012-03-14 23:17:31 +0100 | [diff] [blame] | 203 | if 'configure.ac' in file_paths: |
Ross Lagerwall | 6c52c57 | 2012-03-11 19:21:07 +0200 | [diff] [blame] | 204 | return "yes" if 'configure' in file_paths else "no" |
| 205 | else: |
| 206 | return "not needed" |
| 207 | |
| 208 | @status("pyconfig.h.in regenerated", modal=True, info=str) |
| 209 | def regenerated_pyconfig_h_in(file_paths): |
| 210 | """Check if pyconfig.h.in has been regenerated.""" |
Matthias Klose | 5ce31cc | 2012-03-14 23:17:31 +0100 | [diff] [blame] | 211 | if 'configure.ac' in file_paths: |
Ross Lagerwall | 6c52c57 | 2012-03-11 19:21:07 +0200 | [diff] [blame] | 212 | return "yes" if 'pyconfig.h.in' in file_paths else "no" |
| 213 | else: |
| 214 | return "not needed" |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 215 | |
Brett Cannon | 57ee0c8 | 2017-06-24 17:59:49 -0700 | [diff] [blame] | 216 | def travis(pull_request): |
| 217 | if pull_request == 'false': |
| 218 | print('Not a pull request; skipping') |
| 219 | return |
| 220 | base_branch = get_base_branch() |
| 221 | file_paths = changed_files(base_branch) |
| 222 | python_files = [fn for fn in file_paths if fn.endswith('.py')] |
| 223 | c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))] |
| 224 | doc_files = [fn for fn in file_paths if fn.startswith('Doc') and |
| 225 | fn.endswith(('.rst', '.inc'))] |
| 226 | fixed = [] |
| 227 | fixed.extend(normalize_whitespace(python_files)) |
| 228 | fixed.extend(normalize_c_whitespace(c_files)) |
| 229 | fixed.extend(normalize_docs_whitespace(doc_files)) |
| 230 | if not fixed: |
| 231 | print('No whitespace issues found') |
| 232 | else: |
| 233 | print(f'Please fix the {len(fixed)} file(s) with whitespace issues') |
| 234 | print('(on UNIX you can run `make patchcheck` to make the fixes)') |
| 235 | sys.exit(1) |
| 236 | |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 237 | def main(): |
Nick Coghlan | 2f38625 | 2017-03-12 16:17:46 +1000 | [diff] [blame] | 238 | base_branch = get_base_branch() |
| 239 | file_paths = changed_files(base_branch) |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 240 | python_files = [fn for fn in file_paths if fn.endswith('.py')] |
| 241 | c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))] |
Georg Brandl | 24f0717 | 2014-10-19 11:54:08 +0200 | [diff] [blame] | 242 | doc_files = [fn for fn in file_paths if fn.startswith('Doc') and |
| 243 | fn.endswith(('.rst', '.inc'))] |
Terry Jan Reedy | 6e2711b | 2013-07-21 20:57:44 -0400 | [diff] [blame] | 244 | misc_files = {os.path.join('Misc', 'ACKS'), os.path.join('Misc', 'NEWS')}\ |
| 245 | & set(file_paths) |
Brett Cannon | 058173e | 2010-07-04 22:05:34 +0000 | [diff] [blame] | 246 | # PEP 8 whitespace rules enforcement. |
| 247 | normalize_whitespace(python_files) |
Georg Brandl | a9afb68 | 2010-10-21 12:49:28 +0000 | [diff] [blame] | 248 | # C rules enforcement. |
| 249 | normalize_c_whitespace(c_files) |
| 250 | # Doc whitespace enforcement. |
| 251 | normalize_docs_whitespace(doc_files) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 252 | # Docs updated. |
Georg Brandl | a9afb68 | 2010-10-21 12:49:28 +0000 | [diff] [blame] | 253 | docs_modified(doc_files) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 254 | # Misc/ACKS changed. |
Terry Jan Reedy | 6e2711b | 2013-07-21 20:57:44 -0400 | [diff] [blame] | 255 | credit_given(misc_files) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 256 | # Misc/NEWS changed. |
Terry Jan Reedy | 6e2711b | 2013-07-21 20:57:44 -0400 | [diff] [blame] | 257 | reported_news(misc_files) |
Ross Lagerwall | 6c52c57 | 2012-03-11 19:21:07 +0200 | [diff] [blame] | 258 | # Regenerated configure, if necessary. |
| 259 | regenerated_configure(file_paths) |
| 260 | # Regenerated pyconfig.h.in, if necessary. |
| 261 | regenerated_pyconfig_h_in(file_paths) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 262 | |
| 263 | # Test suite run and passed. |
Éric Araujo | fbc5ff6 | 2011-08-12 17:50:08 +0200 | [diff] [blame] | 264 | if python_files or c_files: |
Ezio Melotti | 5e12bb7 | 2013-01-11 14:07:47 +0200 | [diff] [blame] | 265 | end = " and check for refleaks?" if c_files else "?" |
Éric Araujo | fbc5ff6 | 2011-08-12 17:50:08 +0200 | [diff] [blame] | 266 | print() |
Ezio Melotti | 5e12bb7 | 2013-01-11 14:07:47 +0200 | [diff] [blame] | 267 | print("Did you run the test suite" + end) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 268 | |
| 269 | |
| 270 | if __name__ == '__main__': |
Brett Cannon | 57ee0c8 | 2017-06-24 17:59:49 -0700 | [diff] [blame] | 271 | import argparse |
| 272 | parser = argparse.ArgumentParser(description=__doc__) |
| 273 | parser.add_argument('--travis', |
| 274 | help='Perform pass/fail checks') |
| 275 | args = parser.parse_args() |
| 276 | if args.travis: |
| 277 | travis(args.travis) |
| 278 | else: |
| 279 | main() |