| 1 | n/a | #!/usr/bin/env python3 |
|---|
| 2 | n/a | import re |
|---|
| 3 | n/a | import sys |
|---|
| 4 | n/a | import shutil |
|---|
| 5 | n/a | import os.path |
|---|
| 6 | n/a | import subprocess |
|---|
| 7 | n/a | import sysconfig |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | import reindent |
|---|
| 10 | n/a | import untabify |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | SRCDIR = sysconfig.get_config_var('srcdir') |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | def n_files_str(count): |
|---|
| 17 | n/a | """Return 'N file(s)' with the proper plurality on 'file'.""" |
|---|
| 18 | n/a | return "{} file{}".format(count, "s" if count != 1 else "") |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | def status(message, modal=False, info=None): |
|---|
| 22 | n/a | """Decorator to output status info to stdout.""" |
|---|
| 23 | n/a | def decorated_fxn(fxn): |
|---|
| 24 | n/a | def call_fxn(*args, **kwargs): |
|---|
| 25 | n/a | sys.stdout.write(message + ' ... ') |
|---|
| 26 | n/a | sys.stdout.flush() |
|---|
| 27 | n/a | result = fxn(*args, **kwargs) |
|---|
| 28 | n/a | if not modal and not info: |
|---|
| 29 | n/a | print("done") |
|---|
| 30 | n/a | elif info: |
|---|
| 31 | n/a | print(info(result)) |
|---|
| 32 | n/a | else: |
|---|
| 33 | n/a | print("yes" if result else "NO") |
|---|
| 34 | n/a | return result |
|---|
| 35 | n/a | return call_fxn |
|---|
| 36 | n/a | return decorated_fxn |
|---|
| 37 | n/a | |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | def mq_patches_applied(): |
|---|
| 40 | n/a | """Check if there are any applied MQ patches.""" |
|---|
| 41 | n/a | cmd = 'hg qapplied' |
|---|
| 42 | n/a | with subprocess.Popen(cmd.split(), |
|---|
| 43 | n/a | stdout=subprocess.PIPE, |
|---|
| 44 | n/a | stderr=subprocess.PIPE) as st: |
|---|
| 45 | n/a | bstdout, _ = st.communicate() |
|---|
| 46 | n/a | return st.returncode == 0 and bstdout |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | @status("Getting the list of files that have been added/changed", |
|---|
| 50 | n/a | info=lambda x: n_files_str(len(x))) |
|---|
| 51 | n/a | def changed_files(): |
|---|
| 52 | n/a | """Get the list of changed or added files from Mercurial or git.""" |
|---|
| 53 | n/a | if os.path.isdir(os.path.join(SRCDIR, '.hg')): |
|---|
| 54 | n/a | cmd = 'hg status --added --modified --no-status' |
|---|
| 55 | n/a | if mq_patches_applied(): |
|---|
| 56 | n/a | cmd += ' --rev qparent' |
|---|
| 57 | n/a | with subprocess.Popen(cmd.split(), stdout=subprocess.PIPE) as st: |
|---|
| 58 | n/a | return [x.decode().rstrip() for x in st.stdout] |
|---|
| 59 | n/a | elif os.path.isdir(os.path.join(SRCDIR, '.git')): |
|---|
| 60 | n/a | cmd = 'git status --porcelain' |
|---|
| 61 | n/a | filenames = [] |
|---|
| 62 | n/a | with subprocess.Popen(cmd.split(), stdout=subprocess.PIPE) as st: |
|---|
| 63 | n/a | for line in st.stdout: |
|---|
| 64 | n/a | line = line.decode().rstrip() |
|---|
| 65 | n/a | status = set(line[:2]) |
|---|
| 66 | n/a | # modified, added or unmerged files |
|---|
| 67 | n/a | if not status.intersection('MAU'): |
|---|
| 68 | n/a | continue |
|---|
| 69 | n/a | filename = line[3:] |
|---|
| 70 | n/a | if ' -> ' in filename: |
|---|
| 71 | n/a | # file is renamed |
|---|
| 72 | n/a | filename = filename.split(' -> ', 2)[1].strip() |
|---|
| 73 | n/a | filenames.append(filename) |
|---|
| 74 | n/a | return filenames |
|---|
| 75 | n/a | else: |
|---|
| 76 | n/a | sys.exit('need a Mercurial or git checkout to get modified files') |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | |
|---|
| 79 | n/a | def report_modified_files(file_paths): |
|---|
| 80 | n/a | count = len(file_paths) |
|---|
| 81 | n/a | if count == 0: |
|---|
| 82 | n/a | return n_files_str(count) |
|---|
| 83 | n/a | else: |
|---|
| 84 | n/a | lines = ["{}:".format(n_files_str(count))] |
|---|
| 85 | n/a | for path in file_paths: |
|---|
| 86 | n/a | lines.append(" {}".format(path)) |
|---|
| 87 | n/a | return "\n".join(lines) |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | |
|---|
| 90 | n/a | @status("Fixing whitespace", info=report_modified_files) |
|---|
| 91 | n/a | def normalize_whitespace(file_paths): |
|---|
| 92 | n/a | """Make sure that the whitespace for .py files have been normalized.""" |
|---|
| 93 | n/a | reindent.makebackup = False # No need to create backups. |
|---|
| 94 | n/a | fixed = [path for path in file_paths if path.endswith('.py') and |
|---|
| 95 | n/a | reindent.check(os.path.join(SRCDIR, path))] |
|---|
| 96 | n/a | return fixed |
|---|
| 97 | n/a | |
|---|
| 98 | n/a | |
|---|
| 99 | n/a | @status("Fixing C file whitespace", info=report_modified_files) |
|---|
| 100 | n/a | def normalize_c_whitespace(file_paths): |
|---|
| 101 | n/a | """Report if any C files """ |
|---|
| 102 | n/a | fixed = [] |
|---|
| 103 | n/a | for path in file_paths: |
|---|
| 104 | n/a | abspath = os.path.join(SRCDIR, path) |
|---|
| 105 | n/a | with open(abspath, 'r') as f: |
|---|
| 106 | n/a | if '\t' not in f.read(): |
|---|
| 107 | n/a | continue |
|---|
| 108 | n/a | untabify.process(abspath, 8, verbose=False) |
|---|
| 109 | n/a | fixed.append(path) |
|---|
| 110 | n/a | return fixed |
|---|
| 111 | n/a | |
|---|
| 112 | n/a | |
|---|
| 113 | n/a | ws_re = re.compile(br'\s+(\r?\n)$') |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | @status("Fixing docs whitespace", info=report_modified_files) |
|---|
| 116 | n/a | def normalize_docs_whitespace(file_paths): |
|---|
| 117 | n/a | fixed = [] |
|---|
| 118 | n/a | for path in file_paths: |
|---|
| 119 | n/a | abspath = os.path.join(SRCDIR, path) |
|---|
| 120 | n/a | try: |
|---|
| 121 | n/a | with open(abspath, 'rb') as f: |
|---|
| 122 | n/a | lines = f.readlines() |
|---|
| 123 | n/a | new_lines = [ws_re.sub(br'\1', line) for line in lines] |
|---|
| 124 | n/a | if new_lines != lines: |
|---|
| 125 | n/a | shutil.copyfile(abspath, abspath + '.bak') |
|---|
| 126 | n/a | with open(abspath, 'wb') as f: |
|---|
| 127 | n/a | f.writelines(new_lines) |
|---|
| 128 | n/a | fixed.append(path) |
|---|
| 129 | n/a | except Exception as err: |
|---|
| 130 | n/a | print('Cannot fix %s: %s' % (path, err)) |
|---|
| 131 | n/a | return fixed |
|---|
| 132 | n/a | |
|---|
| 133 | n/a | |
|---|
| 134 | n/a | @status("Docs modified", modal=True) |
|---|
| 135 | n/a | def docs_modified(file_paths): |
|---|
| 136 | n/a | """Report if any file in the Doc directory has been changed.""" |
|---|
| 137 | n/a | return bool(file_paths) |
|---|
| 138 | n/a | |
|---|
| 139 | n/a | |
|---|
| 140 | n/a | @status("Misc/ACKS updated", modal=True) |
|---|
| 141 | n/a | def credit_given(file_paths): |
|---|
| 142 | n/a | """Check if Misc/ACKS has been changed.""" |
|---|
| 143 | n/a | return os.path.join('Misc', 'ACKS') in file_paths |
|---|
| 144 | n/a | |
|---|
| 145 | n/a | |
|---|
| 146 | n/a | @status("Misc/NEWS updated", modal=True) |
|---|
| 147 | n/a | def reported_news(file_paths): |
|---|
| 148 | n/a | """Check if Misc/NEWS has been changed.""" |
|---|
| 149 | n/a | return os.path.join('Misc', 'NEWS') in file_paths |
|---|
| 150 | n/a | |
|---|
| 151 | n/a | @status("configure regenerated", modal=True, info=str) |
|---|
| 152 | n/a | def regenerated_configure(file_paths): |
|---|
| 153 | n/a | """Check if configure has been regenerated.""" |
|---|
| 154 | n/a | if 'configure.ac' in file_paths: |
|---|
| 155 | n/a | return "yes" if 'configure' in file_paths else "no" |
|---|
| 156 | n/a | else: |
|---|
| 157 | n/a | return "not needed" |
|---|
| 158 | n/a | |
|---|
| 159 | n/a | @status("pyconfig.h.in regenerated", modal=True, info=str) |
|---|
| 160 | n/a | def regenerated_pyconfig_h_in(file_paths): |
|---|
| 161 | n/a | """Check if pyconfig.h.in has been regenerated.""" |
|---|
| 162 | n/a | if 'configure.ac' in file_paths: |
|---|
| 163 | n/a | return "yes" if 'pyconfig.h.in' in file_paths else "no" |
|---|
| 164 | n/a | else: |
|---|
| 165 | n/a | return "not needed" |
|---|
| 166 | n/a | |
|---|
| 167 | n/a | def main(): |
|---|
| 168 | n/a | file_paths = changed_files() |
|---|
| 169 | n/a | python_files = [fn for fn in file_paths if fn.endswith('.py')] |
|---|
| 170 | n/a | c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))] |
|---|
| 171 | n/a | doc_files = [fn for fn in file_paths if fn.startswith('Doc') and |
|---|
| 172 | n/a | fn.endswith(('.rst', '.inc'))] |
|---|
| 173 | n/a | misc_files = {os.path.join('Misc', 'ACKS'), os.path.join('Misc', 'NEWS')}\ |
|---|
| 174 | n/a | & set(file_paths) |
|---|
| 175 | n/a | # PEP 8 whitespace rules enforcement. |
|---|
| 176 | n/a | normalize_whitespace(python_files) |
|---|
| 177 | n/a | # C rules enforcement. |
|---|
| 178 | n/a | normalize_c_whitespace(c_files) |
|---|
| 179 | n/a | # Doc whitespace enforcement. |
|---|
| 180 | n/a | normalize_docs_whitespace(doc_files) |
|---|
| 181 | n/a | # Docs updated. |
|---|
| 182 | n/a | docs_modified(doc_files) |
|---|
| 183 | n/a | # Misc/ACKS changed. |
|---|
| 184 | n/a | credit_given(misc_files) |
|---|
| 185 | n/a | # Misc/NEWS changed. |
|---|
| 186 | n/a | reported_news(misc_files) |
|---|
| 187 | n/a | # Regenerated configure, if necessary. |
|---|
| 188 | n/a | regenerated_configure(file_paths) |
|---|
| 189 | n/a | # Regenerated pyconfig.h.in, if necessary. |
|---|
| 190 | n/a | regenerated_pyconfig_h_in(file_paths) |
|---|
| 191 | n/a | |
|---|
| 192 | n/a | # Test suite run and passed. |
|---|
| 193 | n/a | if python_files or c_files: |
|---|
| 194 | n/a | end = " and check for refleaks?" if c_files else "?" |
|---|
| 195 | n/a | print() |
|---|
| 196 | n/a | print("Did you run the test suite" + end) |
|---|
| 197 | n/a | |
|---|
| 198 | n/a | |
|---|
| 199 | n/a | if __name__ == '__main__': |
|---|
| 200 | n/a | main() |
|---|