Skip to content

Commit

Permalink
Run python checkstyle only on invalidated targets.
Browse files Browse the repository at this point in the history
Removes that annoying 3-4 second pause every time you
iterate on a unit test.

Testing Done:
CI passes: https://travis-ci.org/pantsbuild/pants/builds/86285193

Reviewed at https://rbcommons.com/s/twitter/r/2995/
  • Loading branch information
benjyw authored and Benjy committed Oct 19, 2015
1 parent 570690f commit 4d1d2ba
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/python/pants/backend/python/tasks/checkstyle/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ def _is_checked(self, target):

@classmethod
def clear_plugins(cls):
"""Clear all current plugins registered"""
"""Clear all current plugins registered."""
cls._plugins = []

@classmethod
def register_plugin(cls, name, checker):
"""Register plugin to be used run as part of Python Style checks
"""Register plugin to be used run as part of Python Style checks.
:param name: (string) Name of the method plugin
:param checker: (CheckstylePlugin) Plugin subclass
Expand All @@ -82,7 +82,7 @@ def register_plugin(cls, name, checker):
cls._subsystems += (plugin.checker.subsystem, )

def get_nits(self, python_file):
"""Iterate over the instances style checker and yield Nits
"""Iterate over the instances style checker and yield Nits.
:param python_file: PythonFile Object
"""
Expand Down Expand Up @@ -112,6 +112,7 @@ def get_nits(self, python_file):

def check_file(self, filename):
"""Process python file looking for indications of problems.
:param filename: (str) Python source filename
:return: (bool) flag indicating failure
"""
Expand All @@ -121,25 +122,26 @@ def check_file(self, filename):
print('{filename}:SyntaxError: {error}'.format(filename=filename, error=e))
return True

# If the user specifies an invalid severity use comment
# If the user specifies an invalid severity use comment.
severity = Nit.SEVERITY.get(self.options.severity, Nit.COMMENT)

should_fail = False
fail_threshold = Nit.WARNING if self.options.strict else Nit.ERROR

for i, nit in enumerate(self.get_nits(python_file)):
if i == 0:
print() # add an extra newline to clean up the output only if we have nits
print() # Add an extra newline to clean up the output only if we have nits.
if nit.severity >= severity:
print('{nit}\n'.format(nit=nit))
should_fail |= (nit.severity >= fail_threshold)
return should_fail

def checkstyle(self, sources):
""" Iterate over sources and run checker on each file
"""Iterate over sources and run checker on each file.
Files can be suppressed with a --suppress option which takes an xml file containing
file paths that have exceptions and the plugins they need to ignore.
:param sources: iterable containing source file names.
:return: Boolean indicating problems found
"""
Expand All @@ -151,18 +153,17 @@ def checkstyle(self, sources):
raise TaskError('Python Style issues found', exit_code=should_fail)

def execute(self):
"""Run Checkstyle on all found source files"""
"""Run Checkstyle on all found source files."""
if self.options.skip:
return

targets = self.context.targets(self._is_checked)
sources = self.calculate_sources(targets)

if sources:
return self.checkstyle(sources)
with self.invalidated(self.context.targets(self._is_checked)) as invalidation_check:
sources = self.calculate_sources([vt.target for vt in invalidation_check.invalid_vts])
if sources:
return self.checkstyle(sources)

def calculate_sources(self, targets):
"""Generate a set of source files from the given targets"""
"""Generate a set of source files from the given targets."""
sources = set()
for target in targets:
sources.update(
Expand Down

0 comments on commit 4d1d2ba

Please sign in to comment.