Skip to content

Commit cdf9d55

Browse files
committed
Speeding up lint by only considering files which are different.
H/T to @sk- and https://github.com/sk-/git-lint for the inspiration.
1 parent ce7f890 commit cdf9d55

File tree

3 files changed

+57
-11
lines changed

3 files changed

+57
-11
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ before_install:
44
- git clone https://github.com/GoogleCloudPlatform/gcloud-python-wheels
55
gcloud-python-wheels
66
- export WHEELHOUSE="$(pwd)/gcloud-python-wheels/wheelhouse/"
7+
- export GCLOUD_REMOTE_FOR_LINT="origin"
8+
- export GCLOUD_BRANCH_FOR_LINT="master"
79

810
install:
911
- scripts/custom_pip_install.sh tox

CONTRIBUTING.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ Coding Style
104104

105105
$ tox -e lint
106106

107+
- In order to make ``tox -e lint`` run faster, you can set some environment
108+
variables::
109+
110+
export GCLOUD_REMOTE_FOR_LINT="upstream"
111+
export GCLOUD_BRANCH_FOR_LINT="master"
112+
113+
By doing this, you are specifying the location of the most up-to-date
114+
version of ``gcloud-python``. The the suggested remote name ``upstream``
115+
should point to the official ``GoogleCloudPlatform`` checkout and the
116+
the branch should be the main branch on that remote (``master``).
117+
107118
Exceptions to PEP8:
108119

109120
- Many unit tests use a helper method, ``_callFUT`` ("FUT" is short for

run_pylint.py

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import ConfigParser
1111
import copy
12+
import os
1213
import subprocess
1314
import sys
1415

@@ -84,8 +85,37 @@ def is_production_filename(filename):
8485
or filename.startswith('regression'))
8586

8687

88+
def get_files_for_linting():
89+
"""Gets a list of files in the repository.
90+
91+
By default returns all files via `git ls-files`. However, if the
92+
environment variables `GCLOUD_REMOTE_FOR_LINT` and `GCLOUD_BRANCH_FOR_LINT`
93+
are set, this will return only those files which have changed since
94+
the last commit in `REMOTE_FOR_LINT/BRANCH_FOR_LINT`
95+
"""
96+
remote = os.getenv('GCLOUD_REMOTE_FOR_LINT')
97+
branch = os.getenv('GCLOUD_BRANCH_FOR_LINT')
98+
99+
if remote is None or branch is None:
100+
print 'Remote branch not specified, listing all files in repository.'
101+
result = subprocess.check_output(['git', 'ls-files'])
102+
else:
103+
remote_branch = '%s/%s' % (remote, branch)
104+
result = subprocess.check_output(['git', 'diff', '--name-only',
105+
remote_branch])
106+
print 'Using files changed relative to %s:' % (remote_branch,)
107+
print '-' * 60
108+
print result.rstrip('\n') # Don't print trailing newlines.
109+
print '-' * 60
110+
111+
return result.rstrip('\n').split('\n')
112+
113+
87114
def get_python_files():
88-
"""Gets a list of all Python files in the repository.
115+
"""Gets a list of all Python files in the repository that need linting.
116+
117+
Relies on get_files_for_linting() to determine which files should be
118+
considered.
89119
90120
NOTE: This requires `git` to be installed and requires that this
91121
is run within the `git` repository.
@@ -94,11 +124,11 @@ def get_python_files():
94124
:returns: A tuple containing two lists. The first list contains
95125
all production files and the next all test/demo files.
96126
"""
97-
all_files = subprocess.check_output(['git', 'ls-files'])
127+
all_files = get_files_for_linting()
98128

99129
library_files = []
100130
non_library_files = []
101-
for filename in all_files.split('\n'):
131+
for filename in all_files:
102132
if valid_filename(filename):
103133
if is_production_filename(filename):
104134
library_files.append(filename)
@@ -110,14 +140,17 @@ def get_python_files():
110140

111141
def lint_fileset(filenames, rcfile, description):
112142
"""Lints a group of files using a given rcfile."""
113-
rc_flag = '--rcfile=%s' % (rcfile,)
114-
pylint_shell_command = ['pylint', rc_flag] + filenames
115-
status_code = subprocess.call(pylint_shell_command)
116-
if status_code != 0:
117-
error_message = ('Pylint failed on %s with '
118-
'status %d.' % (description, status_code))
119-
print >> sys.stderr, error_message
120-
sys.exit(status_code)
143+
if filenames:
144+
rc_flag = '--rcfile=%s' % (rcfile,)
145+
pylint_shell_command = ['pylint', rc_flag] + filenames
146+
status_code = subprocess.call(pylint_shell_command)
147+
if status_code != 0:
148+
error_message = ('Pylint failed on %s with '
149+
'status %d.' % (description, status_code))
150+
print >> sys.stderr, error_message
151+
sys.exit(status_code)
152+
else:
153+
print 'Skipping %s, no files to lint.' % (description,)
121154

122155

123156
def main():

0 commit comments

Comments
 (0)