diff options
Diffstat (limited to 'Tools/scripts/patchcheck.py')
-rwxr-xr-x[-rw-r--r--] | Tools/scripts/patchcheck.py | 38 |
1 files changed, 29 insertions, 9 deletions
diff --git a/Tools/scripts/patchcheck.py b/Tools/scripts/patchcheck.py index e767eda..0e18dd9 100644..100755 --- a/Tools/scripts/patchcheck.py +++ b/Tools/scripts/patchcheck.py @@ -1,13 +1,18 @@ +#!/usr/bin/env python3 import re import sys import shutil import os.path import subprocess +import sysconfig import reindent import untabify +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 "") @@ -31,13 +36,25 @@ def status(message, modal=False, info=None): return decorated_fxn +def mq_patches_applied(): + """Check if there are any applied MQ patches.""" + cmd = 'hg qapplied' + with subprocess.Popen(cmd.split(), + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) as st: + bstdout, _ = st.communicate() + return st.returncode == 0 and bstdout + + @status("Getting the list of files that have been added/changed", info=lambda x: n_files_str(len(x))) def changed_files(): """Get the list of changed or added files from the VCS.""" - if os.path.isdir('.hg'): + if os.path.isdir(os.path.join(SRCDIR, '.hg')): vcs = 'hg' cmd = 'hg status --added --modified --no-status' + if mq_patches_applied(): + cmd += ' --rev qparent' elif os.path.isdir('.svn'): vcs = 'svn' cmd = 'svn status --quiet --non-interactive --ignore-externals' @@ -74,7 +91,7 @@ def normalize_whitespace(file_paths): 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(path): + if reindent.check(os.path.join(SRCDIR, path)): fixed.append(path) return fixed @@ -84,10 +101,11 @@ def normalize_c_whitespace(file_paths): """Report if any C files """ fixed = [] for path in file_paths: - with open(path, 'r') as f: + abspath = os.path.join(SRCDIR, path) + with open(abspath, 'r') as f: if '\t' not in f.read(): continue - untabify.process(path, 8, verbose=False) + untabify.process(abspath, 8, verbose=False) fixed.append(path) return fixed @@ -98,13 +116,14 @@ ws_re = re.compile(br'\s+(\r?\n)$') def normalize_docs_whitespace(file_paths): fixed = [] for path in file_paths: + abspath = os.path.join(SRCDIR, path) try: - with open(path, 'rb') as f: + 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(path, path + '.bak') - with open(path, 'wb') as f: + shutil.copyfile(abspath, abspath + '.bak') + with open(abspath, 'wb') as f: f.writelines(new_lines) fixed.append(path) except Exception as err: @@ -150,8 +169,9 @@ def main(): reported_news(special_files) # Test suite run and passed. - print() - print("Did you run the test suite?") + if python_files or c_files: + print() + print("Did you run the test suite?") if __name__ == '__main__': |