-
Notifications
You must be signed in to change notification settings - Fork 3
/
.run-pylint.py
82 lines (57 loc) · 1.98 KB
/
.run-pylint.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
"""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 logging
import shlex
import pylint.lint
import yaml
logger = logging.getLogger(__name__)
def generate_args_from_yaml(input_yaml):
"""Generate 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)
for entry in parsed_data:
arg = entry["arg"]
val = entry.get("val")
if val is not None:
if isinstance(val, list):
val = ",".join(str(item) for item in val)
yield "--%s=%s" % (arg, val)
else:
yield "--%s" % arg
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)
logger.info(" ".join(shlex.quote(arg) for arg in ["pylint"] + args))
pylint.lint.Run(args)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
main()