|
1 | | -"""Needs module docstring.""" |
| 1 | +"""Custom script to run PyLint on gcloud codebase. |
2 | 2 |
|
| 3 | +This runs pylint as a script via subprocess in two different |
| 4 | +subprocesses. The first lints the production/library code |
| 5 | +using the default rc file (PRODUCTION_RC). The second lints the |
| 6 | +demo/test code using an rc file (TEST_RC) which allows more style |
| 7 | +violations (hence it has a reduced number of style checks). |
| 8 | +""" |
3 | 9 |
|
4 | 10 | import subprocess |
5 | 11 | import sys |
6 | 12 |
|
7 | 13 |
|
8 | | -IGNORED = [ |
| 14 | +IGNORED_FILES = [ |
9 | 15 | 'gcloud/datastore/datastore_v1_pb2.py', |
10 | 16 | 'docs/conf.py', |
11 | 17 | 'setup.py', |
12 | 18 | ] |
| 19 | +PRODUCTION_RC = 'pylintrc_default' |
| 20 | +TEST_RC = 'pylintrc_reduced' |
13 | 21 |
|
14 | 22 |
|
15 | 23 | def valid_filename(filename): |
16 | | - """Needs method docstring.""" |
| 24 | + """Checks if a file is a Python file and is not ignored.""" |
17 | 25 | return (filename.endswith('.py') and |
18 | | - filename not in IGNORED) |
| 26 | + filename not in IGNORED_FILES) |
| 27 | + |
| 28 | + |
| 29 | +def is_production_filename(filename): |
| 30 | + """Checks if the file contains production code. |
| 31 | +
|
| 32 | + :rtype: `bool` |
| 33 | + :returns: Boolean indicating production status. |
| 34 | + """ |
| 35 | + return not ('demo' in filename or 'test' in filename) |
19 | 36 |
|
20 | 37 |
|
21 | 38 | def get_python_files(): |
22 | | - """Needs method docstring.""" |
| 39 | + """Gets a list of all Python files in the repository. |
| 40 | +
|
| 41 | + NOTE: This requires `git` to be installed and requires that this |
| 42 | + is run within the `git` repository. |
| 43 | +
|
| 44 | + :rtype: `tuple` |
| 45 | + :returns: A tuple containing two lists. The first list contains |
| 46 | + all production files and the next all test/demo files. |
| 47 | + """ |
23 | 48 | all_files = subprocess.check_output(['git', 'ls-files']) |
24 | 49 |
|
25 | 50 | library_files = [] |
26 | 51 | non_library_files = [] |
27 | 52 | for filename in all_files.split('\n'): |
28 | 53 | if valid_filename(filename): |
29 | | - if 'demo' in filename or 'test' in filename: |
30 | | - non_library_files.append(filename) |
31 | | - else: |
| 54 | + if is_production_filename(filename): |
32 | 55 | library_files.append(filename) |
| 56 | + else: |
| 57 | + non_library_files.append(filename) |
33 | 58 |
|
34 | 59 | return library_files, non_library_files |
35 | 60 |
|
36 | 61 |
|
37 | | -def main(): |
38 | | - """Needs method docstring.""" |
39 | | - library_files, non_library_files = get_python_files() |
40 | | - # Use default RC file with the code from the library. |
41 | | - pylint_library_code = (['pylint', '--rcfile=pylintrc_default'] + |
42 | | - library_files) |
43 | | - status_code = subprocess.call(pylint_library_code) |
| 62 | +def lint_fileset(filenames, rcfile, description): |
| 63 | + """Lints a group of files using a given rcfile.""" |
| 64 | + rc_flag = '--rcfile=%s' % (rcfile,) |
| 65 | + pylint_shell_command = ['pylint', rc_flag] + filenames |
| 66 | + status_code = subprocess.call(pylint_shell_command) |
44 | 67 | if status_code != 0: |
45 | | - error_message = ('Pylint failed on library code with ' |
46 | | - 'status %d.' % (status_code,)) |
| 68 | + error_message = ('Pylint failed on %s with ' |
| 69 | + 'status %d.' % (description, status_code)) |
47 | 70 | print >> sys.stderr, error_message |
48 | 71 | sys.exit(status_code) |
49 | 72 |
|
50 | | - pylint_non_library_code = (['pylint', '--rcfile=pylintrc_reduced'] |
51 | | - + non_library_files) |
52 | | - status_code = subprocess.call(pylint_non_library_code) |
53 | | - if status_code != 0: |
54 | | - error_message = ('Pylint failed on test and demo code with ' |
55 | | - 'status %d.' % (status_code,)) |
56 | | - print >> sys.stderr, error_message |
57 | | - sys.exit(status_code) |
| 73 | + |
| 74 | +def main(): |
| 75 | + """Script entry point. Lints both sets of files.""" |
| 76 | + library_files, non_library_files = get_python_files() |
| 77 | + lint_fileset(library_files, PRODUCTION_RC, 'library code') |
| 78 | + lint_fileset(non_library_files, TEST_RC, 'test and demo code') |
58 | 79 |
|
59 | 80 |
|
60 | 81 | if __name__ == '__main__': |
|
0 commit comments