blob: b06a98c4281986d7279d25463d3493daa3c2f53b [file] [log] [blame]
Danny Hermes97791812016-11-01 12:43:01 -07001# Copyright 2016 Google Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""This script runs Pylint on the specified source.
16
17Before running Pylint, it generates a Pylint configuration on
18the fly based on programmatic defaults.
19"""
20
21from __future__ import print_function
22
23import collections
24import copy
25import io
26import os
27import subprocess
28import sys
29
30import six
31
32
33_SCRIPTS_DIR = os.path.abspath(os.path.dirname(__file__))
34PRODUCTION_RC = os.path.join(_SCRIPTS_DIR, 'pylintrc')
35TEST_RC = os.path.join(_SCRIPTS_DIR, 'pylintrc.test')
36
37_PRODUCTION_RC_ADDITIONS = {
38 'MESSAGES CONTROL': {
39 'disable': [
40 'I',
41 'import-error',
42 'no-member',
43 'protected-access',
44 'redefined-variable-type',
45 'similarities',
46 ],
47 },
48}
49_PRODUCTION_RC_REPLACEMENTS = {
50 'MASTER': {
51 'ignore': ['CVS', '.git', '.cache', '.tox', '.nox'],
52 'load-plugins': 'pylint.extensions.check_docs',
53 },
54 'REPORTS': {
55 'reports': 'no',
56 },
57 'BASIC': {
58 'method-rgx': '[a-z_][a-z0-9_]{2,40}$',
59 'function-rgx': '[a-z_][a-z0-9_]{2,40}$',
60 },
61 'TYPECHECK': {
62 'ignored-modules': ['six', 'google.protobuf'],
63 },
64 'DESIGN': {
65 'min-public-methods': '0',
66 'max-args': '10',
67 'max-attributes': '15',
68 },
69}
70_TEST_RC_ADDITIONS = copy.deepcopy(_PRODUCTION_RC_ADDITIONS)
71_TEST_RC_ADDITIONS['MESSAGES CONTROL']['disable'].extend([
72 'missing-docstring',
73 'no-self-use',
74 'redefined-outer-name',
75 'unused-argument',
Jon Wayne Parrottb9897dc2016-11-02 20:31:14 -070076 'no-name-in-module',
Danny Hermes97791812016-11-01 12:43:01 -070077])
78_TEST_RC_REPLACEMENTS = copy.deepcopy(_PRODUCTION_RC_REPLACEMENTS)
79_TEST_RC_REPLACEMENTS.setdefault('BASIC', {})
80_TEST_RC_REPLACEMENTS['BASIC'].update({
Jon Wayne Parrottb9897dc2016-11-02 20:31:14 -070081 'good-names': ['i', 'j', 'k', 'ex', 'Run', '_', 'fh', 'pytestmark'],
Danny Hermes97791812016-11-01 12:43:01 -070082 'method-rgx': '[a-z_][a-z0-9_]{2,80}$',
83 'function-rgx': '[a-z_][a-z0-9_]{2,80}$',
84})
85IGNORED_FILES = ()
86
87_ERROR_TEMPLATE = 'Pylint failed on {} with status {:d}.'
88_LINT_FILESET_MSG = (
89 'Keyword arguments rc_filename and description are both '
90 'required. No other keyword arguments are allowed.')
91
92
93def get_default_config():
94 """Get the default Pylint configuration.
95
96 .. note::
97
98 The output of this function varies based on the current version of
99 Pylint installed.
100
101 Returns:
102 str: The default Pylint configuration.
103 """
104 # Swallow STDERR if it says
105 # "No config file found, using default configuration"
106 result = subprocess.check_output(['pylint', '--generate-rcfile'],
107 stderr=subprocess.PIPE)
108 # On Python 3, this returns bytes (from STDOUT), so we
109 # convert to a string.
110 return result.decode('utf-8')
111
112
113def read_config(contents):
114 """Reads pylintrc config into native ConfigParser object.
115
116 Args:
117 contents (str): The contents of the file containing the INI config.
118
119 Returns
120 ConfigParser.ConfigParser: The parsed configuration.
121 """
122 file_obj = io.StringIO(contents)
123 config = six.moves.configparser.ConfigParser()
124 config.readfp(file_obj)
125 return config
126
127
128def _transform_opt(opt_val):
129 """Transform a config option value to a string.
130
131 If already a string, do nothing. If an iterable, then
132 combine into a string by joining on ",".
133
134 Args:
135 opt_val (Union[str, list]): A config option's value.
136
137 Returns:
138 str: The option value converted to a string.
139 """
140 if isinstance(opt_val, (list, tuple)):
141 return ','.join(opt_val)
142 else:
143 return opt_val
144
145
146def lint_fileset(*dirnames, **kwargs):
147 """Lints a group of files using a given rcfile.
148
149 Keyword arguments are
150
151 * ``rc_filename`` (``str``): The name of the Pylint config RC file.
152 * ``description`` (``str``): A description of the files and configuration
153 currently being run.
154
155 Args:
156 dirnames (tuple): Directories to run Pylint in.
157 kwargs: The keyword arguments. The only keyword arguments
158 are ``rc_filename`` and ``description`` and both
159 are required.
160
161 Raises:
162 KeyError: If the wrong keyword arguments are used.
163 """
164 try:
165 rc_filename = kwargs['rc_filename']
166 description = kwargs['description']
167 if len(kwargs) != 2:
168 raise KeyError
169 except KeyError:
170 raise KeyError(_LINT_FILESET_MSG)
171
172 pylint_shell_command = ['pylint', '--rcfile', rc_filename]
173 pylint_shell_command.extend(dirnames)
174 status_code = subprocess.call(pylint_shell_command)
175 if status_code != 0:
176 error_message = _ERROR_TEMPLATE.format(description, status_code)
177 print(error_message, file=sys.stderr)
178 sys.exit(status_code)
179
180
181def make_rc(base_cfg, target_filename,
182 additions=None, replacements=None):
183 """Combines a base rc and additions into single file.
184
185 Args:
186 base_cfg (ConfigParser.ConfigParser): The configuration we are
187 merging into.
188 target_filename (str): The filename where the new configuration
189 will be saved.
190 additions (dict): (Optional) The values added to the configuration.
191 replacements (dict): (Optional) The wholesale replacements for
192 the new configuration.
193
194 Raises:
195 KeyError: if one of the additions or replacements does not
196 already exist in the current config.
197 """
198 # Set-up the mutable default values.
199 if additions is None:
200 additions = {}
201 if replacements is None:
202 replacements = {}
203
204 # Create fresh config, which must extend the base one.
205 new_cfg = six.moves.configparser.ConfigParser()
206 # pylint: disable=protected-access
207 new_cfg._sections = copy.deepcopy(base_cfg._sections)
208 new_sections = new_cfg._sections
209 # pylint: enable=protected-access
210
211 for section, opts in additions.items():
212 curr_section = new_sections.setdefault(
213 section, collections.OrderedDict())
214 for opt, opt_val in opts.items():
215 curr_val = curr_section.get(opt)
216 if curr_val is None:
217 raise KeyError('Expected to be adding to existing option.')
218 curr_val = curr_val.rstrip(',')
219 opt_val = _transform_opt(opt_val)
220 curr_section[opt] = '%s, %s' % (curr_val, opt_val)
221
222 for section, opts in replacements.items():
223 curr_section = new_sections.setdefault(
224 section, collections.OrderedDict())
225 for opt, opt_val in opts.items():
226 curr_val = curr_section.get(opt)
227 if curr_val is None:
228 raise KeyError('Expected to be replacing existing option.')
229 opt_val = _transform_opt(opt_val)
230 curr_section[opt] = '%s' % (opt_val,)
231
232 with open(target_filename, 'w') as file_obj:
233 new_cfg.write(file_obj)
234
235
236def main():
237 """Script entry point. Lints both sets of files."""
238 default_config = read_config(get_default_config())
239 make_rc(default_config, PRODUCTION_RC,
240 additions=_PRODUCTION_RC_ADDITIONS,
241 replacements=_PRODUCTION_RC_REPLACEMENTS)
242 make_rc(default_config, TEST_RC,
243 additions=_TEST_RC_ADDITIONS,
244 replacements=_TEST_RC_REPLACEMENTS)
245 lint_fileset('google', rc_filename=PRODUCTION_RC,
246 description='Library')
247 lint_fileset('tests', 'system_tests', rc_filename=TEST_RC,
248 description='Test')
249
250
251if __name__ == '__main__':
252 main()