Commit 0cbf03e5 authored by Matt Wala's avatar Matt Wala
Browse files

WIP: Make Pylint more flexible with a YAML-based config

See also: pytools#4

This adds support for a YAML-based config file format for Pylint with
a thin wrapper script that translates the YAML file into command line
arguments.

This change also modifies prepare-and-run-pylint.sh to look for YAML
configs. It looks for a .pylintrc.yml, and if it does not find it, it
downloads the global config file from ci-support. It also supports
local configurations via the file .pylintrc-local.yml.
parent ee199e32
Loading
Loading
Loading
Loading

.pylintrc-default

deleted100644 → 0
+0 −814

File deleted.

Preview size limit exceeded, changes collapsed.

.pylintrc-default.yml

0 → 100644
+4 −0
Original line number Diff line number Diff line
- arg: disable
  val: all
- arg: enable
  val: E
+13 −4
Original line number Diff line number Diff line
@@ -13,10 +13,19 @@ fi
curl -L -O -k https://gitlab.tiker.net/inducer/ci-support/raw/master/build-py-project.sh
source build-py-project.sh

$PY_EXE -m pip install pylint
curl -L -O -k https://gitlab.tiker.net/inducer/ci-support/raw/master/run-pylint.py

if ! test -f .pylintrc; then
  curl -o .pylintrc https://gitlab.tiker.net/inducer/ci-support/raw/master/.pylintrc-default
$PY_EXE -m pip install pylint PyYAML

PYLINT_RUNNER_ARGS=""

if ! test -f .pylintrc.yml; then
  curl -o .pylintrc.yml https://gitlab.tiker.net/inducer/ci-support/raw/master/.pylintrc-default.yml
  PYLINT_RUNNER_ARGS="$PYLINT_RUNNER_ARGS --yaml-rcfile=.pylintrc.yml"
fi

if ! test -f .pylintrc-local.yml; then
  PYLINT_RUNNER_ARGS="$PYLINT_RUNNER_ARGS --yaml-rcfile=.pylintrc-local.yml"
fi

python -m pylint "$@"
python run-pylint.py $PYLINT_RUNNER_ARGS "$@"

run-pylint.py

0 → 100755
+79 −0
Original line number Diff line number Diff line
#!/usr/bin/env python
"""This script allows using Pylint with YAML-based config files.

The usage of this script is identical to Pylint, except that this script accepts
an additional argument "--yaml-rcfile=", which specifies a path to a YAML file
from which command line options are derived. The "--yaml-rcfile=" argument may
be given multiple times.

The YAML config file format is a list of "arg"/"val" entries. Multiple or
omitted values are allowed. Repeated arguments are allowed. An example is as
follows:

    ---
    - arg: errors-only
    - arg: ignore
      val:
        - dir1
        - dir2
    - arg: ignore
        val: dir3

This example is equivalent to invoking pylint with the options

    pylint --errors-only --ignore=dir1,dir2 --ignore=dir3

"""

import sys

import pylint.lint
import yaml


def generate_args_from_yaml(input_yaml):
    """Return a list of strings suitable for use as Pylint args, from YAML.

    Arguments:
        input_yaml: YAML data, as an input file or bytes

    """

    parsed_data = yaml.safe_load(input_yaml)
    args = []

    for entry in parsed_data:
        arg = entry["arg"]
        val = entry.get("val", None)

        if val is not None:
            if isinstance(val, list):
                val = ",".join(str(item) for item in val)

            args.append("--%s=%s" % (arg, val))
        else:
            args.append("--%s" % arg)

    return args


YAML_RCFILE_PREFIX = "--yaml-rcfile="


def main():
    """Process command line args and run Pylint."""
    args = []

    for arg in sys.argv[1:]:
        if arg.startswith(YAML_RCFILE_PREFIX):
            config_path = arg[len(YAML_RCFILE_PREFIX):]
            with open(config_path, "r") as config_file:
                args.extend(generate_args_from_yaml(config_file))
        else:
            args.append(arg)

    pylint.lint.Run(args)


if __name__ == "__main__":
    main()