Skip to content

Commit 32cce91

Browse files
authored
Merge pull request googleapis#2486 from tseaver/pycodestyle-use_diff_base
Use 'diff_base' for pycodestyle check, if set.
2 parents 8426afa + eb6c2e5 commit 32cce91

File tree

3 files changed

+62
-61
lines changed

3 files changed

+62
-61
lines changed

scripts/pycodestyle_on_repo.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@
2323
import subprocess
2424
import sys
2525

26+
from script_utils import get_affected_files
27+
2628

2729
def main():
2830
"""Run pycodestyle on all Python files in the repository."""
2931
git_root = subprocess.check_output(
3032
['git', 'rev-parse', '--show-toplevel']).strip()
3133
os.chdir(git_root)
32-
python_files = subprocess.check_output(['git', 'ls-files', '*py'])
33-
python_files = python_files.strip().split()
34+
candidates, _ = get_affected_files()
35+
python_files = [
36+
candidate for candidate in candidates if candidate.endswith('.py')]
3437

3538
pycodestyle_command = ['pycodestyle'] + python_files
3639
status_code = subprocess.call(pycodestyle_command)

scripts/run_pylint.py

Lines changed: 3 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@
2929
import subprocess
3030
import sys
3131

32-
from script_utils import LOCAL_BRANCH_ENV
33-
from script_utils import LOCAL_REMOTE_ENV
34-
from script_utils import in_travis
35-
from script_utils import in_travis_pr
36-
from script_utils import travis_branch
32+
from script_utils import get_affected_files
3733

3834

3935
IGNORED_DIRECTORIES = [
@@ -141,62 +137,10 @@ def is_production_filename(filename):
141137
return 'test' not in filename and 'docs' not in filename
142138

143139

144-
def get_files_for_linting(allow_limited=True):
145-
"""Gets a list of files in the repository.
146-
147-
By default, returns all files via ``git ls-files``. However, in some cases
148-
uses a specific commit or branch (a so-called diff base) to compare
149-
against for changed files. (This requires ``allow_limited=True``.)
150-
151-
To speed up linting on Travis pull requests against master, we manually
152-
set the diff base to the branch the pull request is against. We don't do
153-
this on "push" builds since "master" will be the currently checked out
154-
code. One could potentially use ${TRAVIS_COMMIT_RANGE} to find a diff base
155-
but this value is not dependable.
156-
157-
To allow faster local ``tox`` runs, the local remote and local branch
158-
environment variables can be set to specify a remote branch to diff
159-
against.
160-
161-
:type allow_limited: bool
162-
:param allow_limited: Boolean indicating if a reduced set of files can
163-
be used.
164-
165-
:rtype: pair
166-
:returns: Tuple of the diff base using the the list of filenames to be
167-
linted.
168-
"""
169-
diff_base = None
170-
if in_travis():
171-
# In the case of a pull request into a branch, we want to
172-
# diff against HEAD in that branch.
173-
if in_travis_pr():
174-
diff_base = travis_branch()
175-
else:
176-
# Only allow specified remote and branch in local dev.
177-
remote = os.getenv(LOCAL_REMOTE_ENV)
178-
branch = os.getenv(LOCAL_BRANCH_ENV)
179-
if remote is not None and branch is not None:
180-
diff_base = '%s/%s' % (remote, branch)
181-
182-
if diff_base is not None and allow_limited:
183-
result = subprocess.check_output(['git', 'diff', '--name-only',
184-
diff_base])
185-
print('Using files changed relative to %s:' % (diff_base,))
186-
print('-' * 60)
187-
print(result.rstrip('\n')) # Don't print trailing newlines.
188-
print('-' * 60)
189-
else:
190-
print('Diff base not specified, listing all files in repository.')
191-
result = subprocess.check_output(['git', 'ls-files'])
192-
193-
return result.rstrip('\n').split('\n'), diff_base
194-
195-
196140
def get_python_files(all_files=None):
197141
"""Gets a list of all Python files in the repository that need linting.
198142
199-
Relies on :func:`get_files_for_linting()` to determine which files should
143+
Relies on :func:`get_affected_files()` to determine which files should
200144
be considered.
201145
202146
NOTE: This requires ``git`` to be installed and requires that this
@@ -210,7 +154,7 @@ def get_python_files(all_files=None):
210154
contains all production files, the next all test files.
211155
"""
212156
if all_files is None:
213-
all_files, diff_base = get_files_for_linting()
157+
all_files, diff_base = get_affected_files()
214158

215159
library_files = []
216160
non_library_files = []

scripts/script_utils.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
"""Common helpers for testing scripts."""
1616

17+
from __future__ import print_function
18+
1719
import os
1820
import subprocess
1921

@@ -159,3 +161,55 @@ def get_changed_packages(blob_name1, blob_name2, package_list):
159161
result.add(file_root)
160162

161163
return sorted(result)
164+
165+
166+
def get_affected_files(allow_limited=True):
167+
"""Gets a list of files in the repository.
168+
169+
By default, returns all files via ``git ls-files``. However, in some cases
170+
uses a specific commit or branch (a so-called diff base) to compare
171+
against for changed files. (This requires ``allow_limited=True``.)
172+
173+
To speed up linting on Travis pull requests against master, we manually
174+
set the diff base to the branch the pull request is against. We don't do
175+
this on "push" builds since "master" will be the currently checked out
176+
code. One could potentially use ${TRAVIS_COMMIT_RANGE} to find a diff base
177+
but this value is not dependable.
178+
179+
To allow faster local ``tox`` runs, the local remote and local branch
180+
environment variables can be set to specify a remote branch to diff
181+
against.
182+
183+
:type allow_limited: bool
184+
:param allow_limited: Boolean indicating if a reduced set of files can
185+
be used.
186+
187+
:rtype: pair
188+
:returns: Tuple of the diff base using the list of filenames to be
189+
linted.
190+
"""
191+
diff_base = None
192+
if in_travis():
193+
# In the case of a pull request into a branch, we want to
194+
# diff against HEAD in that branch.
195+
if in_travis_pr():
196+
diff_base = travis_branch()
197+
else:
198+
# Only allow specified remote and branch in local dev.
199+
remote = os.getenv(LOCAL_REMOTE_ENV)
200+
branch = os.getenv(LOCAL_BRANCH_ENV)
201+
if remote is not None and branch is not None:
202+
diff_base = '%s/%s' % (remote, branch)
203+
204+
if diff_base is not None and allow_limited:
205+
result = subprocess.check_output(['git', 'diff', '--name-only',
206+
diff_base])
207+
print('Using files changed relative to %s:' % (diff_base,))
208+
print('-' * 60)
209+
print(result.rstrip('\n')) # Don't print trailing newlines.
210+
print('-' * 60)
211+
else:
212+
print('Diff base not specified, listing all files in repository.')
213+
result = subprocess.check_output(['git', 'ls-files'])
214+
215+
return result.rstrip('\n').split('\n'), diff_base

0 commit comments

Comments
 (0)