quality.py 4.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
"""
Check code quality using pep8, pylint, and diff_quality.
"""
from paver.easy import sh, task, cmdopts, needs
import os
import errno
from .utils.envs import Env

@task
@needs('pavelib.prereqs.install_python_prereqs')
@cmdopts([
    ("system=", "s", "System to act on"),
    ("errors", "e", "Check for errors only"),
])
def run_pylint(options):
    """
    Run pylint on system code
    """
    errors = getattr(options, 'errors', False)
20
    systems = getattr(options, 'system', 'lms,cms,common').split(',')
21

22 23 24
    for system in systems:
        # Directory to put the pylint report in.
        # This makes the folder if it doesn't already exist.
25
        report_dir = (Env.REPORT_DIR / system).makedirs_p()
26

27
        flags = '-E' if errors else ''
28

29
        apps = [system]
30

31 32 33
        for directory in ['djangoapps', 'lib']:
            dirs = os.listdir(os.path.join(system, directory))
            apps.extend([d for d in dirs if os.path.isdir(os.path.join(system, directory, d))])
34

35
        apps_list = ' '.join(apps)
36

37 38 39 40 41
        pythonpath_prefix = (
            "PYTHONPATH={system}:{system}/djangoapps:{system}/"
            "lib:common/djangoapps:common/lib".format(
                system=system
            )
42
        )
43 44 45 46 47 48 49 50 51

        sh(
            "{pythonpath_prefix} pylint {flags} -f parseable {apps} | "
            "tee {report_dir}/pylint.report".format(
                pythonpath_prefix=pythonpath_prefix,
                flags=flags,
                apps=apps_list,
                report_dir=report_dir
            )
52 53 54 55 56 57 58 59 60 61 62 63
        )


@task
@needs('pavelib.prereqs.install_python_prereqs')
@cmdopts([
    ("system=", "s", "System to act on"),
])
def run_pep8(options):
    """
    Run pep8 on system code
    """
64
    systems = getattr(options, 'system', 'lms,cms,common').split(',')
65

66 67 68
    for system in systems:
        # Directory to put the pep8 report in.
        # This makes the folder if it doesn't already exist.
69
        report_dir = (Env.REPORT_DIR / system).makedirs_p()
70

71
        sh('pep8 {system} | tee {report_dir}/pep8.report'.format(system=system, report_dir=report_dir))
72 73 74 75 76 77 78 79 80 81 82


@task
@needs('pavelib.prereqs.install_python_prereqs')
def run_quality():
    """
    Build the html diff quality reports, and print the reports to the console.
    """

    # Directory to put the diff reports in.
    # This makes the folder if it doesn't already exist.
83
    dquality_dir = (Env.REPORT_DIR / "diff_quality").makedirs_p()
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

    # Generage diff-quality html report for pep8, and print to console
    # If pep8 reports exist, use those
    # Otherwise, `diff-quality` will call pep8 itself

    pep8_files = []
    for subdir, _dirs, files in os.walk(os.path.join(Env.REPORT_DIR)):
        for f in files:
            if f == "pep8.report":
                pep8_files.append(os.path.join(subdir, f))

    pep8_reports = u' '.join(pep8_files)

    sh(
        "diff-quality --violations=pep8 --html-report {dquality_dir}/"
        "diff_quality_pep8.html {pep8_reports}".format(
            dquality_dir=dquality_dir, pep8_reports=pep8_reports)
    )

    sh(
        "diff-quality --violations=pep8 {pep8_reports}".format(
            pep8_reports=pep8_reports)
    )

    # Generage diff-quality html report for pylint, and print to console
    # If pylint reports exist, use those
    # Otherwise, `diff-quality` will call pylint itself

    pylint_files = []
    for subdir, _dirs, files in os.walk(os.path.join(Env.REPORT_DIR)):
        for f in files:
            if f == "pylint.report":
                pylint_files.append(os.path.join(subdir, f))

    pylint_reports = u' '.join(pylint_files)

    pythonpath_prefix = (
        "PYTHONPATH=$PYTHONPATH:lms:lms/djangoapps:lms/lib:cms:cms/djangoapps:cms/lib:"
        "common:common/djangoapps:common/lib"
    )

    sh(
        "{pythonpath_prefix} diff-quality --violations=pylint --html-report "
        "{dquality_dir}/diff_quality_pylint.html {pylint_reports}".format(
            pythonpath_prefix=pythonpath_prefix,
            dquality_dir=dquality_dir,
            pylint_reports=pylint_reports
        )
    )

    sh(
        "{pythonpath_prefix} diff-quality --violations=pylint {pylint_reports}".format(
            pythonpath_prefix=pythonpath_prefix,
            pylint_reports=pylint_reports
        )
    )