Skip to content

Commit da28005

Browse files
committed
Reducing repeated pylint configs by defining test configs as a delta.
Uses ConfigParser to append modifications onto base file and writes a new config file (which is gitignore-ed) before running pylint.
1 parent 94d9646 commit da28005

5 files changed

Lines changed: 48 additions & 42 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,6 @@ coverage.xml
4545

4646
# Regression test environment variables.
4747
regression/local_test_setup
48+
49+
# Make sure a generated file isn't accidentally committed.
50+
pylintrc_reduced

pylintrc_default

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@ ignore = datastore_v1_pb2.py
2424
[MESSAGES CONTROL]
2525
disable = I, protected-access, maybe-no-member, no-member,
2626
redefined-builtin, star-args, missing-format-attribute,
27-
similarities, arguments-differ,
28-
29-
30-
31-
27+
similarities, arguments-differ
3228

3329
[REPORTS]
3430
reports = no

pylintrc_reduced

Lines changed: 0 additions & 37 deletions
This file was deleted.

pylintrc_test_modifications

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# This config is intended to be used for test code
2+
# and other non-production code, like demos.
3+
4+
[MESSAGES CONTROL]
5+
disable = invalid-name, missing-docstring, too-many-public-methods,
6+
too-few-public-methods, attribute-defined-outside-init,
7+
unbalanced-tuple-unpacking, too-many-locals, exec-used,
8+
no-init, no-self-use

run_pylint.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
violations (hence it has a reduced number of style checks).
88
"""
99

10+
import ConfigParser
11+
import copy
1012
import subprocess
1113
import sys
1214

@@ -18,6 +20,39 @@
1820
]
1921
PRODUCTION_RC = 'pylintrc_default'
2022
TEST_RC = 'pylintrc_reduced'
23+
TEST_RC_MODS = 'pylintrc_test_modifications'
24+
25+
26+
def read_config(filename):
27+
"""Reads pylintrc config onto native ConfigParser object."""
28+
config = ConfigParser.ConfigParser()
29+
with open(filename, 'r') as file_obj:
30+
config.readfp(file_obj)
31+
return config
32+
33+
34+
def make_test_rc(base_rc_filename, modifications_rc_filename,
35+
target_filename):
36+
"""Combines a base rc and modifications into single file."""
37+
main_cfg = read_config(base_rc_filename)
38+
modification_cfg = read_config(modifications_rc_filename)
39+
40+
# Create fresh config for test, which must extend production.
41+
test_cfg = ConfigParser.ConfigParser()
42+
43+
test_cfg._sections = copy.deepcopy(main_cfg._sections)
44+
for section, opts in modification_cfg._sections.items():
45+
curr_section = test_cfg._sections.setdefault(
46+
section, modification_cfg._dict())
47+
for opt, opt_val in opts.items():
48+
if opt in curr_section:
49+
if curr_section[opt] != opt_val:
50+
curr_section[opt] = ', '.join([curr_section[opt], opt_val])
51+
else:
52+
curr_section[opt] = opt_val
53+
54+
with open(target_filename, 'w') as file_obj:
55+
test_cfg.write(file_obj)
2156

2257

2358
def valid_filename(filename):
@@ -74,6 +109,7 @@ def lint_fileset(filenames, rcfile, description):
74109

75110
def main():
76111
"""Script entry point. Lints both sets of files."""
112+
make_test_rc(PRODUCTION_RC, TEST_RC_MODS, TEST_RC)
77113
library_files, non_library_files = get_python_files()
78114
lint_fileset(library_files, PRODUCTION_RC, 'library code')
79115
lint_fileset(non_library_files, TEST_RC, 'test and demo code')

0 commit comments

Comments
 (0)