Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Tools/scripts/patchcheck.py

    r2 r391  
     1#!/usr/bin/env python
     2import re
     3import sys
     4import shutil
    15import os.path
    26import subprocess
    3 import sys
     7import sysconfig
    48
    59import reindent
     10import untabify
     11
     12
     13SRCDIR = sysconfig.get_config_var('srcdir')
     14
     15
     16def 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 "")
    619
    720
     
    1831                print info(result)
    1932            else:
    20                 if result:
    21                     print "yes"
    22                 else:
    23                     print "NO"
     33                print "yes" if result else "NO"
    2434            return result
    2535        return call_fxn
    2636    return decorated_fxn
    2737
     38
     39def mq_patches_applied():
     40    """Check if there are any applied MQ patches."""
     41    cmd = 'hg qapplied'
     42    st = subprocess.Popen(cmd.split(),
     43                          stdout=subprocess.PIPE,
     44                          stderr=subprocess.PIPE)
     45    try:
     46        bstdout, _ = st.communicate()
     47        return st.returncode == 0 and bstdout
     48    finally:
     49        st.stdout.close()
     50        st.stderr.close()
     51
     52
    2853@status("Getting the list of files that have been added/changed",
    29             info=lambda x: "%s files" % len(x))
     54        info=lambda x: n_files_str(len(x)))
    3055def changed_files():
    31     """Run ``svn status`` and return a set of files that have been
    32     changed/added."""
    33     cmd = 'svn status --quiet --non-interactive --ignore-externals'
    34     svn_st = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
    35     svn_st.wait()
    36     output = [line.strip() for line in svn_st.stdout.readlines()]
    37     files = set()
    38     for line in output:
    39         if not line[0] in ('A', 'M'):
    40             continue
    41         line_parts = line.split()
    42         path = line_parts[-1]
    43         if os.path.isfile(path):
    44             files.add(path)
    45     return files
     56    """Get the list of changed or added files from the VCS."""
     57    if os.path.isdir(os.path.join(SRCDIR, '.hg')):
     58        vcs = 'hg'
     59        cmd = 'hg status --added --modified --no-status'
     60        if mq_patches_applied():
     61            cmd += ' --rev qparent'
     62    elif os.path.isdir('.svn'):
     63        vcs = 'svn'
     64        cmd = 'svn status --quiet --non-interactive --ignore-externals'
     65    else:
     66        sys.exit('need a checkout to get modified files')
    4667
    47 @status("Fixing whitespace", info=lambda x: "%s files" % x)
     68    st = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
     69    try:
     70        st.wait()
     71        if vcs == 'hg':
     72            return [x.decode().rstrip() for x in st.stdout]
     73        else:
     74            output = (x.decode().rstrip().rsplit(None, 1)[-1]
     75                      for x in st.stdout if x[0] in 'AM')
     76        return set(path for path in output if os.path.isfile(path))
     77    finally:
     78        st.stdout.close()
     79
     80
     81def report_modified_files(file_paths):
     82    count = len(file_paths)
     83    if count == 0:
     84        return n_files_str(count)
     85    else:
     86        lines = ["{}:".format(n_files_str(count))]
     87        for path in file_paths:
     88            lines.append("  {}".format(path))
     89        return "\n".join(lines)
     90
     91
     92@status("Fixing whitespace", info=report_modified_files)
    4893def normalize_whitespace(file_paths):
    4994    """Make sure that the whitespace for .py files have been normalized."""
    5095    reindent.makebackup = False  # No need to create backups.
    51     result = map(reindent.check, (x for x in file_paths if x.endswith('.py')))
    52     return sum(result)
     96    fixed = []
     97    for path in (x for x in file_paths if x.endswith('.py')):
     98        if reindent.check(os.path.join(SRCDIR, path)):
     99            fixed.append(path)
     100    return fixed
     101
     102
     103@status("Fixing C file whitespace", info=report_modified_files)
     104def normalize_c_whitespace(file_paths):
     105    """Report if any C files """
     106    fixed = []
     107    for path in file_paths:
     108        abspath = os.path.join(SRCDIR, path)
     109        with open(abspath, 'r') as f:
     110            if '\t' not in f.read():
     111                continue
     112        untabify.process(abspath, 8, verbose=False)
     113        fixed.append(path)
     114    return fixed
     115
     116
     117ws_re = re.compile(br'\s+(\r?\n)$')
     118
     119@status("Fixing docs whitespace", info=report_modified_files)
     120def normalize_docs_whitespace(file_paths):
     121    fixed = []
     122    for path in file_paths:
     123        abspath = os.path.join(SRCDIR, path)
     124        try:
     125            with open(abspath, 'rb') as f:
     126                lines = f.readlines()
     127            new_lines = [ws_re.sub(br'\1', line) for line in lines]
     128            if new_lines != lines:
     129                shutil.copyfile(abspath, abspath + '.bak')
     130                with open(abspath, 'wb') as f:
     131                    f.writelines(new_lines)
     132                fixed.append(path)
     133        except Exception as err:
     134            print 'Cannot fix %s: %s' % (path, err)
     135    return fixed
     136
    53137
    54138@status("Docs modified", modal=True)
    55139def docs_modified(file_paths):
    56     """Report if any files in the Docs directory."""
    57     for path in file_paths:
    58         if path.startswith("Doc"):
    59             return True
    60     return False
     140    """Report if any file in the Doc directory has been changed."""
     141    return bool(file_paths)
     142
    61143
    62144@status("Misc/ACKS updated", modal=True)
    63145def credit_given(file_paths):
    64146    """Check if Misc/ACKS has been changed."""
    65     return 'Misc/ACKS' in file_paths
     147    return os.path.join('Misc', 'ACKS') in file_paths
     148
    66149
    67150@status("Misc/NEWS updated", modal=True)
    68151def reported_news(file_paths):
    69152    """Check if Misc/NEWS has been changed."""
    70     return 'Misc/NEWS' in file_paths
     153    return os.path.join('Misc', 'NEWS') in file_paths
    71154
    72155
    73156def main():
    74157    file_paths = changed_files()
    75     # PEP 7/8 verification.
    76     normalize_whitespace(file_paths)
     158    python_files = [fn for fn in file_paths if fn.endswith('.py')]
     159    c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))]
     160    doc_files = [fn for fn in file_paths if fn.startswith('Doc')]
     161    misc_files = {os.path.join('Misc', 'ACKS'), os.path.join('Misc', 'NEWS')}\
     162            & set(file_paths)
     163    # PEP 8 whitespace rules enforcement.
     164    normalize_whitespace(python_files)
     165    # C rules enforcement.
     166    normalize_c_whitespace(c_files)
     167    # Doc whitespace enforcement.
     168    normalize_docs_whitespace(doc_files)
    77169    # Docs updated.
    78     docs_modified(file_paths)
     170    docs_modified(doc_files)
    79171    # Misc/ACKS changed.
    80     credit_given(file_paths)
     172    credit_given(misc_files)
    81173    # Misc/NEWS changed.
    82     reported_news(file_paths)
     174    reported_news(misc_files)
    83175
    84176    # Test suite run and passed.
    85     print
    86     print "Did you run the test suite?"
     177    if python_files or c_files:
     178        end = " and check for refleaks?" if c_files else "?"
     179        print
     180        print "Did you run the test suite" + end
    87181
    88182
Note: See TracChangeset for help on using the changeset viewer.