#! /usr/bin/python2.7 import re import sys import shutil import os.path import subprocess import sysconfig import reindent import untabify # Excluded directories which are copies of external libraries: # don't check their coding style EXCLUDE_DIRS = [os.path.join('Modules', '_ctypes', 'libffi'), os.path.join('Modules', '_ctypes', 'libffi_osx'), os.path.join('Modules', '_ctypes', 'libffi_msvc'), os.path.join('Modules', 'expat'), os.path.join('Modules', 'zlib')] SRCDIR = sysconfig.get_config_var('srcdir') def n_files_str(count): """Return 'N file(s)' with the proper plurality on 'file'.""" return "{} file{}".format(count, "s" if count != 1 else "") def status(message, modal=False, info=None): """Decorator to output status info to stdout.""" def decorated_fxn(fxn): def call_fxn(*args, **kwargs): sys.stdout.write(message + ' ... ') sys.stdout.flush() result = fxn(*args, **kwargs) if not modal and not info: print "done" elif info: print info(result) else: print "yes" if result else "NO" return result return call_fxn return decorated_fxn def get_git_branch(): """Get the symbolic name for the current git branch""" cmd = "git rev-parse --abbrev-ref HEAD".split() try: return subprocess.check_output(cmd, stderr=subprocess.PIPE) except subprocess.CalledProcessError: return None def get_git_upstream_remote(): """Get the remote name to use for upstream branches Uses "upstream" if it exists, "origin" otherwise """ cmd = "git remote get-url upstream".split() try: subprocess.check_output(cmd, stderr=subprocess.PIPE) except subprocess.CalledProcessError: return "origin" return "upstream" @status("Getting base branch for PR", info=lambda x: x if x is not None else "not a PR branch") def get_base_branch(): if not os.path.exists(os.path.join(SRCDIR, '.git')): # Not a git checkout, so there's no base branch return None version = sys.version_info if version.releaselevel == 'alpha': base_branch = "master" else: base_branch = "{0.major}.{0.minor}".format(version) this_branch = get_git_branch() if this_branch is None or this_branch == base_branch: # Not on a git PR branch, so there's no base branch return None upstream_remote = get_git_upstream_remote() return upstream_remote + "/" + base_branch @status("Getting the list of files that have been added/changed", info=lambda x: n_files_str(len(x))) def changed_files(base_branch=None): """Get the list of changed or added files from git.""" if os.path.exists(os.path.join(SRCDIR, '.git')): # We just use an existence check here as: # directory = normal git checkout/clone # file = git worktree directory if base_branch: cmd = 'git diff --name-status ' + base_branch else: cmd = 'git status --porcelain' filenames = [] st = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE) try: for line in st.stdout: line = line.decode().rstrip() status_text, filename = line.split(None, 1) status = set(status_text) # modified, added or unmerged files if not status.intersection('MAU'): continue if ' -> ' in filename: # file is renamed filename = filename.split(' -> ', 2)[1].strip() filenames.append(filename) finally: st.stdout.close() else: sys.exit('need a git checkout to get modified files') filenames2 = [] for filename in filenames: # Normalize the path to be able to match using .startswith() filename = os.path.normpath(filename) if any(filename.startswith(path) for path in EXCLUDE_DIRS): # Exclude the file continue filenames2.append(filename) return filenames2 def report_modified_files(file_paths): count = len(file_paths) if count == 0: return n_files_str(count) else: lines = ["{}:".format(n_files_str(count))] for path in file_paths: lines.append(" {}".format(path)) return "\n".join(lines) @status("Fixing whitespace", info=report_modified_files) def normalize_whitespace(file_paths): """Make sure that the whitespace for .py files have been normalized.""" reindent.makebackup = False # No need to create backups. fixed = [] for path in (x for x in file_paths if x.endswith('.py')): if reindent.check(os.path.join(SRCDIR, path)): fixed.append(path) return fixed @status("Fixing C file whitespace", info=report_modified_files) def normalize_c_whitespace(file_paths): """Report if any C files """ fixed = [] for path in file_paths: abspath = os.path.join(SRCDIR, path) with open(abspath, 'r') as f: if '\t' not in f.read(): continue untabify.process(abspath, 8, verbose=False) fixed.append(path) return fixed ws_re = re.compile(br'\s+(\r?\n)$') @status("Fixing docs whitespace", info=report_modified_files) def normalize_docs_whitespace(file_paths): fixed = [] for path in file_paths: abspath = os.path.join(SRCDIR, path) try: with open(abspath, 'rb') as f: lines = f.readlines() new_lines = [ws_re.sub(br'\1', line) for line in lines] if new_lines != lines: shutil.copyfile(abspath, abspath + '.bak') with open(abspath, 'wb') as f: f.writelines(new_lines) fixed.append(path) except Exception as err: print 'Cannot fix %s: %s' % (path, err) return fixed @status("Docs modified", modal=True) def docs_modified(file_paths): """Report if any file in the Doc directory has been changed.""" return bool(file_paths) @status("Misc/ACKS updated", modal=True) def credit_given(file_paths): """Check if Misc/ACKS has been changed.""" return os.path.join('Misc', 'ACKS') in file_paths @status("Misc/NEWS.d updated with `blurb`", modal=True) def reported_news(file_paths): """Check if Misc/NEWS.d has been changed.""" return any(p.startswith(os.path.join('Misc', 'NEWS.d', 'next')) for p in file_paths) def main(): base_branch = get_base_branch() file_paths = changed_files(base_branch) python_files = [fn for fn in file_paths if fn.endswith('.py')] c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))] doc_files = [fn for fn in file_paths if fn.startswith('Doc') and fn.endswith(('.rst', '.inc'))] misc_files = {p for p in file_paths if p.startswith('Misc')} # PEP 8 whitespace rules enforcement. normalize_whitespace(python_files) # C rules enforcement. normalize_c_whitespace(c_files) # Doc whitespace enforcement. normalize_docs_whitespace(doc_files) # Docs updated. docs_modified(doc_files) # Misc/ACKS changed. credit_given(misc_files) # Misc/NEWS changed. reported_news(misc_files) # Test suite run and passed. if python_files or c_files: end = " and check for refleaks?" if c_files else "?" print print "Did you run the test suite" + end if __name__ == '__main__': main()
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
analyze_dxp.py | File | 4.11 KB | 0755 |
|
analyze_dxp.pyc | File | 4.64 KB | 0644 |
|
analyze_dxp.pyo | File | 4.64 KB | 0644 |
|
byext.py | File | 3.85 KB | 0755 |
|
byext.pyc | File | 4.42 KB | 0644 |
|
byext.pyo | File | 4.42 KB | 0644 |
|
byteyears.py | File | 1.6 KB | 0755 |
|
byteyears.pyc | File | 1.37 KB | 0644 |
|
byteyears.pyo | File | 1.37 KB | 0644 |
|
checkappend.py | File | 4.55 KB | 0755 |
|
checkappend.pyc | File | 4.77 KB | 0644 |
|
checkappend.pyo | File | 4.77 KB | 0644 |
|
checkpip.py | File | 757 B | 0755 |
|
checkpip.pyc | File | 1.02 KB | 0644 |
|
checkpip.pyo | File | 1.02 KB | 0644 |
|
checkpyc.py | File | 1.96 KB | 0755 |
|
checkpyc.pyc | File | 1.93 KB | 0644 |
|
checkpyc.pyo | File | 1.93 KB | 0644 |
|
classfix.py | File | 5.81 KB | 0755 |
|
classfix.pyc | File | 4.09 KB | 0644 |
|
classfix.pyo | File | 4.09 KB | 0644 |
|
cleanfuture.py | File | 8.38 KB | 0755 |
|
cleanfuture.pyc | File | 7.22 KB | 0644 |
|
cleanfuture.pyo | File | 7.19 KB | 0644 |
|
combinerefs.py | File | 4.28 KB | 0755 |
|
combinerefs.pyc | File | 4.16 KB | 0644 |
|
combinerefs.pyo | File | 4.12 KB | 0644 |
|
copytime.py | File | 663 B | 0755 |
|
copytime.pyc | File | 937 B | 0644 |
|
copytime.pyo | File | 937 B | 0644 |
|
crlf.py | File | 610 B | 0755 |
|
crlf.pyc | File | 855 B | 0644 |
|
crlf.pyo | File | 855 B | 0644 |
|
cvsfiles.py | File | 1.74 KB | 0755 |
|
cvsfiles.pyc | File | 2.11 KB | 0644 |
|
cvsfiles.pyo | File | 2.11 KB | 0644 |
|
db2pickle.py | File | 3.49 KB | 0755 |
|
db2pickle.pyc | File | 3.42 KB | 0644 |
|
db2pickle.pyo | File | 3.42 KB | 0644 |
|
diff.py | File | 2.02 KB | 0755 |
|
diff.pyc | File | 2.33 KB | 0644 |
|
diff.pyo | File | 2.33 KB | 0644 |
|
dutree.py | File | 1.58 KB | 0755 |
|
dutree.pyc | File | 2.18 KB | 0644 |
|
dutree.pyo | File | 2.18 KB | 0644 |
|
eptags.py | File | 1.45 KB | 0755 |
|
eptags.pyc | File | 1.83 KB | 0644 |
|
eptags.pyo | File | 1.83 KB | 0644 |
|
find_recursionlimit.py | File | 3.39 KB | 0755 |
|
find_recursionlimit.pyc | File | 5.54 KB | 0644 |
|
find_recursionlimit.pyo | File | 5.54 KB | 0644 |
|
finddiv.py | File | 2.46 KB | 0755 |
|
finddiv.pyc | File | 3.22 KB | 0644 |
|
finddiv.pyo | File | 3.22 KB | 0644 |
|
findlinksto.py | File | 1.04 KB | 0755 |
|
findlinksto.pyc | File | 1.39 KB | 0644 |
|
findlinksto.pyo | File | 1.39 KB | 0644 |
|
findnocoding.py | File | 2.74 KB | 0755 |
|
findnocoding.pyc | File | 3.13 KB | 0644 |
|
findnocoding.pyo | File | 3.13 KB | 0644 |
|
fixcid.py | File | 9.76 KB | 0755 |
|
fixcid.pyc | File | 7.67 KB | 0644 |
|
fixcid.pyo | File | 7.67 KB | 0644 |
|
fixdiv.py | File | 13.52 KB | 0755 |
|
fixdiv.pyc | File | 13.52 KB | 0644 |
|
fixdiv.pyo | File | 13.44 KB | 0644 |
|
fixheader.py | File | 1.16 KB | 0755 |
|
fixheader.pyc | File | 1.44 KB | 0644 |
|
fixheader.pyo | File | 1.44 KB | 0644 |
|
fixnotice.py | File | 2.98 KB | 0755 |
|
fixnotice.pyc | File | 3.42 KB | 0644 |
|
fixnotice.pyo | File | 3.42 KB | 0644 |
|
fixps.py | File | 893 B | 0755 |
|
fixps.pyc | File | 969 B | 0644 |
|
fixps.pyo | File | 969 B | 0644 |
|
google.py | File | 519 B | 0755 |
|
google.pyc | File | 792 B | 0644 |
|
google.pyo | File | 792 B | 0644 |
|
gprof2html.py | File | 2.12 KB | 0755 |
|
gprof2html.pyc | File | 2.22 KB | 0644 |
|
gprof2html.pyo | File | 2.22 KB | 0644 |
|
h2py.py | File | 5.81 KB | 0755 |
|
h2py.pyc | File | 4.29 KB | 0644 |
|
h2py.pyo | File | 4.29 KB | 0644 |
|
hotshotmain.py | File | 1.45 KB | 0755 |
|
hotshotmain.pyc | File | 1.82 KB | 0644 |
|
hotshotmain.pyo | File | 1.82 KB | 0644 |
|
ifdef.py | File | 3.63 KB | 0755 |
|
ifdef.pyc | File | 2.21 KB | 0644 |
|
ifdef.pyo | File | 2.21 KB | 0644 |
|
lfcr.py | File | 618 B | 0755 |
|
lfcr.pyc | File | 880 B | 0644 |
|
lfcr.pyo | File | 880 B | 0644 |
|
linktree.py | File | 2.37 KB | 0755 |
|
linktree.pyc | File | 1.98 KB | 0644 |
|
linktree.pyo | File | 1.98 KB | 0644 |
|
lll.py | File | 742 B | 0755 |
|
lll.pyc | File | 942 B | 0644 |
|
lll.pyo | File | 942 B | 0644 |
|
logmerge.py | File | 5.44 KB | 0755 |
|
logmerge.pyc | File | 4.96 KB | 0644 |
|
logmerge.pyo | File | 4.96 KB | 0644 |
|
mailerdaemon.py | File | 7.76 KB | 0755 |
|
mailerdaemon.pyc | File | 7.19 KB | 0644 |
|
mailerdaemon.pyo | File | 7.19 KB | 0644 |
|
md5sum.py | File | 2.33 KB | 0755 |
|
md5sum.pyc | File | 2.85 KB | 0644 |
|
md5sum.pyo | File | 2.85 KB | 0644 |
|
methfix.py | File | 5.33 KB | 0755 |
|
methfix.pyc | File | 4.03 KB | 0644 |
|
methfix.pyo | File | 4.03 KB | 0644 |
|
mkreal.py | File | 1.59 KB | 0755 |
|
mkreal.pyc | File | 1.93 KB | 0644 |
|
mkreal.pyo | File | 1.93 KB | 0644 |
|
ndiff.py | File | 3.72 KB | 0755 |
|
ndiff.pyc | File | 3.77 KB | 0644 |
|
ndiff.pyo | File | 3.77 KB | 0644 |
|
nm2def.py | File | 2.39 KB | 0755 |
|
nm2def.pyc | File | 2.89 KB | 0644 |
|
nm2def.pyo | File | 2.89 KB | 0644 |
|
objgraph.py | File | 5.88 KB | 0755 |
|
objgraph.pyc | File | 4.82 KB | 0644 |
|
objgraph.pyo | File | 4.82 KB | 0644 |
|
parseentities.py | File | 1.68 KB | 0755 |
|
parseentities.pyc | File | 2.03 KB | 0644 |
|
parseentities.pyo | File | 2.03 KB | 0644 |
|
patchcheck.py | File | 7.5 KB | 0755 |
|
patchcheck.pyc | File | 8.91 KB | 0644 |
|
patchcheck.pyo | File | 8.91 KB | 0644 |
|
pathfix.py | File | 4.23 KB | 0755 |
|
pathfix.pyc | File | 3.75 KB | 0644 |
|
pathfix.pyo | File | 3.75 KB | 0644 |
|
pdeps.py | File | 3.84 KB | 0755 |
|
pdeps.pyc | File | 3.14 KB | 0644 |
|
pdeps.pyo | File | 3.14 KB | 0644 |
|
pickle2db.py | File | 3.85 KB | 0755 |
|
pickle2db.pyc | File | 3.73 KB | 0644 |
|
pickle2db.pyo | File | 3.73 KB | 0644 |
|
pindent.py | File | 16.77 KB | 0755 |
|
pindent.pyc | File | 11.29 KB | 0644 |
|
pindent.pyo | File | 11.29 KB | 0644 |
|
ptags.py | File | 1.2 KB | 0755 |
|
ptags.pyc | File | 1.37 KB | 0644 |
|
ptags.pyo | File | 1.37 KB | 0644 |
|
pysource.py | File | 3.76 KB | 0755 |
|
pysource.pyc | File | 3.92 KB | 0644 |
|
pysource.pyo | File | 3.92 KB | 0644 |
|
redemo.py | File | 5.66 KB | 0755 |
|
redemo.pyc | File | 5.13 KB | 0644 |
|
redemo.pyo | File | 5.13 KB | 0644 |
|
reindent-rst.py | File | 278 B | 0755 |
|
reindent-rst.pyc | File | 481 B | 0644 |
|
reindent-rst.pyo | File | 481 B | 0644 |
|
reindent.py | File | 11.15 KB | 0755 |
|
reindent.pyc | File | 9.4 KB | 0644 |
|
reindent.pyo | File | 9.37 KB | 0644 |
|
rgrep.py | File | 1.46 KB | 0755 |
|
rgrep.pyc | File | 1.84 KB | 0644 |
|
rgrep.pyo | File | 1.84 KB | 0644 |
|
serve.py | File | 1.12 KB | 0755 |
|
serve.pyc | File | 1.56 KB | 0644 |
|
serve.pyo | File | 1.56 KB | 0644 |
|
setup.py | File | 421 B | 0644 |
|
setup.pyc | File | 548 B | 0644 |
|
setup.pyo | File | 548 B | 0644 |
|
suff.py | File | 621 B | 0755 |
|
suff.pyc | File | 904 B | 0644 |
|
suff.pyo | File | 904 B | 0644 |
|
svneol.py | File | 2.86 KB | 0755 |
|
svneol.pyc | File | 2.84 KB | 0644 |
|
svneol.pyo | File | 2.76 KB | 0644 |
|
texcheck.py | File | 9.04 KB | 0644 |
|
texcheck.pyc | File | 8.18 KB | 0644 |
|
texcheck.pyo | File | 8.18 KB | 0644 |
|
texi2html.py | File | 68.19 KB | 0755 |
|
texi2html.pyc | File | 81.37 KB | 0644 |
|
texi2html.pyo | File | 81.37 KB | 0644 |
|
treesync.py | File | 5.65 KB | 0755 |
|
treesync.pyc | File | 5.85 KB | 0644 |
|
treesync.pyo | File | 5.85 KB | 0644 |
|
untabify.py | File | 1.19 KB | 0755 |
|
untabify.pyc | File | 1.55 KB | 0644 |
|
untabify.pyo | File | 1.55 KB | 0644 |
|
which.py | File | 1.59 KB | 0755 |
|
which.pyc | File | 1.59 KB | 0644 |
|
which.pyo | File | 1.59 KB | 0644 |
|
win_add2path.py | File | 1.58 KB | 0644 |
|
win_add2path.pyc | File | 2.02 KB | 0644 |
|
win_add2path.pyo | File | 2.02 KB | 0644 |
|
xxci.py | File | 2.73 KB | 0755 |
|
xxci.pyc | File | 3.92 KB | 0644 |
|
xxci.pyo | File | 3.92 KB | 0644 |
|