run-pylint.py 2.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#!/usr/bin/env python
"""
Determines the number of pylint violations (note that this includes both warnings and errors).
If larger than the supplied threshold, emits a status of 1 indicating failure.
"""

import re
import sys


USAGE = u"{} VIOLATIONS_FILE VIOLATIONS_THRESHOLD"


def count_pylint_violations(report_file):
    """
    Parses a pylint report line-by-line, and determins the number of pylint violations.

    Taken from https://github.com/edx/edx-platform/blob/master/pavelib/quality.py.
    """
    num_violations = 0
    # An example string:
    # common/lib/xmodule/xmodule/tests/test_conditional.py:21: [C0111(missing-docstring), DummySystem] Missing docstring
    # More examples can be found in the unit tests for this method
    pylint_pattern = re.compile(r".(\d+):\ \[(\D\d+.+\]).")

    for line in report_file:
        violation_list_for_line = pylint_pattern.split(line)
        # If the string is parsed into four parts, then we've found a violation. Example of split parts:
        # test file, line number, violation name, violation details
        if len(violation_list_for_line) == 4:
            num_violations += 1

    return num_violations


def main():
    """
    Main entry point for the script.
    """
    if len(sys.argv) < 3:
        print USAGE.format(sys.argv[0])
        sys.exit(1)

    try:
        with open(sys.argv[1]) as violations:
            num_violations = count_pylint_violations(violations)
            max_violations = sys.argv[2]
            print "Found {} pylint violations, threshold is {}".format(num_violations, max_violations)
            if num_violations > int(max_violations):
                violations.seek(0)
                for line in violations:
                    print line
                print "NUMBER OF PYLINT VIOLATIONS ({}) EXCEEDED THRESHOLD {}".format(num_violations, max_violations)
                sys.exit(1)
    except IOError as ex:
        print u"Could not open pylint violations file: {}".format(sys.argv[1])
        print(ex)
        sys.exit(1)


if __name__ == '__main__':
    main()