-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
run_pylint.py
executable file
·159 lines (120 loc) · 5.05 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
83
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python
# Copyright (c) 2017 Dependable Systems Lab, EPFL
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
"""
Runs Pylint with some additional checkers.
"""
from __future__ import print_function
import os
import sys
import tokenize
import astroid
from pylint.checkers import BaseChecker, BaseTokenChecker
from pylint.interfaces import IAstroidChecker, ITokenChecker
from pylint.lint import PyLinter
import six
################
# Additional checkers
################
SINGLE_QUOTES = ('\'', '"')
TRIPLE_QUOTES = ('\'\'\'', '"""')
DEFAULT_SINGLE_QUOTE = '\''
DEFAULT_TRIPLE_QUOTE = '"""'
class LiteralQuoteChecker(BaseTokenChecker):
"""
Check that string literals use a consistent quote character, be it a single
quote or a double quote.
"""
__implements__ = ITokenChecker
name = 'string_literal_quotes'
msgs = {'C8001': ('Use quote character `%s` for string literals, not '
'`%s`',
'incorrect-string-literal-quote',
'Used when the string literal quote character does not '
'match the one specified in the '
'`expected-string-literal-quote` option.'),
'C8002': ('Use `%s` triple-quotes, not `%s`',
'incorrect-triple-quotes',
'Used when the triple-quotes character does not match '
'the one specified in the `expected-triple-quote` '
'option.'),
}
options = (('expected-string-literal-quote',
{'type': 'choice', 'metavar': '<\' or ">',
'default': DEFAULT_SINGLE_QUOTE, 'choices': SINGLE_QUOTES,
'help': 'The default string literal quote character. Must be '
'either \' or "'}),
('expected-string-triple-quote',
{'type': 'choice', 'metavar': '<\'\'\' or """>',
'default': DEFAULT_TRIPLE_QUOTE, 'choices': TRIPLE_QUOTES,
'help': 'The default string triple quote. Must be either '
'\'\'\' or """'}),
)
def process_tokens(self, tokens):
for tok_type, token, (start_row, _), _, _ in tokens:
if tok_type == tokenize.STRING:
self._process_string_token(token, start_row)
def _process_string_token(self, token, start_row):
expected_quote_char = self.config.expected_string_literal_quote
expected_triple_quotes = self.config.expected_string_triple_quote
# Adapted from pylint/checkers/strings.py
for i, c in enumerate(token):
if c in SINGLE_QUOTES:
break
# pylint: disable=undefined-loop-variable
# We ignore prefix markers like u, b, r
after_prefix = token[i:]
# Check triple-quote strings
if len(after_prefix) >= 3 and after_prefix[:3] in TRIPLE_QUOTES:
if after_prefix[:3] != expected_triple_quotes:
self.add_message('incorrect-triple-quotes', line=start_row,
args=(expected_triple_quotes,
after_prefix[:3]))
# Check single quote strings
elif after_prefix[0] != expected_quote_char:
self.add_message('incorrect-string-literal-quote', line=start_row,
args=(expected_quote_char, after_prefix[0]))
class StringConcatChecker(BaseChecker):
"""
Look for string concatenation operations.
We use a very naive approach and only look for string concatenations that
contain at least one string literal.
"""
__implements__ = IAstroidChecker
name = 'string_concatenation'
msgs = {'C8003': ('Prefer string substitution to string concatenation',
'string-concat',
'Used when a string concatenation operation is found.'),
}
def visit_binop(self, node):
if node.op != '+':
return
left = node.left
if (isinstance(left, astroid.Const) and
isinstance(left.value, six.string_types)):
self.add_message('string-concat', node=node)
def register(linter):
linter.register_checker(LiteralQuoteChecker(linter))
linter.register_checker(StringConcatChecker(linter))
################
# Run pylint
################
FILE_DIR = os.path.dirname(__file__)
THIS_MODULE = os.path.splitext(os.path.basename(__file__))[0]
PYLINT_RC_PATH = os.path.join(os.path.realpath(FILE_DIR), 'pylint_rc')
def main(args):
if len(args) != 2:
print('Usage: %s /path/to/python/code' % args[0])
sys.exit(1)
# Need to add tis script's directory to the Python path so that this module
# can be made available to Pylint
sys.path.append(FILE_DIR)
linter = PyLinter()
linter.load_default_plugins()
linter.load_plugin_modules([THIS_MODULE])
linter.read_config_file(PYLINT_RC_PATH)
linter.load_config_file()
linter.check(args[1])
linter.generate_reports()
if __name__ == '__main__':
main(sys.argv)