99
1010import ConfigParser
1111import copy
12+ import os
1213import subprocess
1314import 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+
87114def 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
111141def 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
123156def main ():
0 commit comments