<s> from __future__ import ( absolute_import , division , generators , nested_scopes , print_function , <EOL> unicode_literals , with_statement ) <EOL> import os <EOL> import re <EOL> from pants . backend . jvm . subsystems . scala_platform import ScalaPlatform <EOL> from pants . backend . jvm . tasks . nailgun_task import NailgunTask <EOL> from pants . base . exceptions import TaskError <EOL> from pants . build_graph . target import Target <EOL> from pants . option . custom_types import file_option <EOL> from pants . process . xargs import Xargs <EOL> from pants . util . dirutil import touch <EOL> class FileExcluder ( object ) : <EOL> def __init__ ( self , excludes_path , log ) : <EOL> self . excludes = set ( ) <EOL> if excludes_path : <EOL> if not os . path . exists ( excludes_path ) : <EOL> raise TaskError ( '<STR_LIT>' . format ( excludes_path ) ) <EOL> with open ( excludes_path ) as fh : <EOL> for line in fh . readlines ( ) : <EOL> pattern = line . strip ( ) <EOL> if pattern and not pattern . startswith ( '<STR_LIT:#>' ) : <EOL> self . excludes . add ( re . compile ( pattern ) ) <EOL> log . debug ( '<STR_LIT>' . format ( pattern = pattern ) ) <EOL> else : <EOL> log . debug ( '<STR_LIT>' ) <EOL> def should_include ( self , source_filename ) : <EOL> for exclude in self . excludes : <EOL> if exclude . match ( source_filename ) : <EOL> return False <EOL> return True <EOL> class Scalastyle ( NailgunTask ) : <EOL> """<STR_LIT>""" <EOL> class UnspecifiedConfig ( TaskError ) : <EOL> def __init__ ( self ) : <EOL> super ( Scalastyle . UnspecifiedConfig , self ) . __init__ ( <EOL> '<STR_LIT>' ) <EOL> class MissingConfig ( TaskError ) : <EOL> def __init__ ( self , path ) : <EOL> super ( Scalastyle . MissingConfig , self ) . __init__ ( <EOL> '<STR_LIT>' . format ( path ) ) <EOL> _SCALA_SOURCE_EXTENSION = '<STR_LIT>' <EOL> _MAIN = '<STR_LIT>' <EOL> @ classmethod <EOL> def subsystem_dependencies ( cls ) : <EOL> return super ( Scalastyle , cls ) . subsystem_dependencies ( ) + ( ScalaPlatform , ) <EOL> @ classmethod <EOL> def register_options ( cls , register ) : <EOL> super ( Scalastyle , cls ) . register_options ( register ) <EOL> register ( '<STR_LIT>' , type = bool , fingerprint = True , help = '<STR_LIT>' ) <EOL> register ( '<STR_LIT>' , type = file_option , advanced = True , fingerprint = True , <EOL> help = '<STR_LIT>' ) <EOL> register ( '<STR_LIT>' , type = file_option , advanced = True , fingerprint = True , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> register ( '<STR_LIT>' , type = bool , <EOL> help = '<STR_LIT>' ) <EOL> @ classmethod <EOL> def get_non_synthetic_scala_targets ( cls , targets ) : <EOL> return filter ( <EOL> lambda target : isinstance ( target , Target ) <EOL> and target . has_sources ( cls . _SCALA_SOURCE_EXTENSION ) <EOL> and ( not target . is_synthetic ) , <EOL> targets ) <EOL> @ classmethod <EOL> def get_non_excluded_scala_sources ( cls , scalastyle_excluder , scala_targets ) : <EOL> scala_sources = list ( ) <EOL> for target in scala_targets : <EOL> scala_sources . extend ( target . sources_relative_to_buildroot ( ) ) <EOL> scala_sources = filter ( <EOL> lambda filename : filename . endswith ( cls . _SCALA_SOURCE_EXTENSION ) , <EOL> scala_sources ) <EOL> scala_sources = filter ( scalastyle_excluder . should_include , scala_sources ) <EOL> return scala_sources <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( Scalastyle , self ) . __init__ ( * args , ** kwargs ) <EOL> self . _results_dir = os . path . join ( self . workdir , '<STR_LIT>' ) <EOL> def _create_result_file ( self , target ) : <EOL> result_file = os . path . join ( self . _results_dir , target . id ) <EOL> touch ( result_file ) <EOL> return result_file <EOL> @ property <EOL> def cache_target_dirs ( self ) : <EOL> return True <EOL> def execute ( self ) : <EOL> if self . get_options ( ) . skip : <EOL> self . context . log . info ( '<STR_LIT>' ) <EOL> return <EOL> targets = self . get_non_synthetic_scala_targets ( self . context . targets ( ) ) <EOL> if not targets : <EOL> return <EOL> with self . invalidated ( targets ) as invalidation_check : <EOL> invalid_targets = [ vt . target for vt in invalidation_check . invalid_vts ] <EOL> scalastyle_config = self . validate_scalastyle_config ( ) <EOL> scalastyle_verbose = self . get_options ( ) . verbose <EOL> scalastyle_quiet = self . get_options ( ) . quiet or False <EOL> scalastyle_excluder = self . create_file_excluder ( ) <EOL> self . context . log . debug ( '<STR_LIT>' ) <EOL> for target in invalid_targets : <EOL> self . context . log . debug ( '<STR_LIT>' . format ( address_spec = target . address . spec ) ) <EOL> scala_sources = self . get_non_excluded_scala_sources ( scalastyle_excluder , invalid_targets ) <EOL> self . context . log . debug ( '<STR_LIT>' ) <EOL> for source in scala_sources : <EOL> self . context . log . debug ( '<STR_LIT>' . format ( source = source ) ) <EOL> if scala_sources : <EOL> def call ( srcs ) : <EOL> def to_java_boolean ( x ) : <EOL> return str ( x ) . lower ( ) <EOL> cp = ScalaPlatform . global_instance ( ) . style_classpath ( self . context . products ) <EOL> scalastyle_args = [ <EOL> '<STR_LIT:-c>' , scalastyle_config , <EOL> '<STR_LIT>' , to_java_boolean ( scalastyle_verbose ) , <EOL> '<STR_LIT>' , to_java_boolean ( scalastyle_quiet ) , <EOL> ] <EOL> return self . runjava ( classpath = cp , <EOL> main = self . _MAIN , <EOL> jvm_options = self . get_options ( ) . jvm_options , <EOL> args = scalastyle_args + srcs ) <EOL> result = Xargs ( call ) . execute ( scala_sources ) <EOL> if result != <NUM_LIT:0> : <EOL> raise TaskError ( '<STR_LIT>' . format ( <EOL> entry = Scalastyle . _MAIN , exit_code = result ) ) <EOL> def validate_scalastyle_config ( self ) : <EOL> scalastyle_config = self . get_options ( ) . config <EOL> if not scalastyle_config : <EOL> raise Scalastyle . UnspecifiedConfig ( ) <EOL> if not os . path . exists ( scalastyle_config ) : <EOL> raise Scalastyle . MissingConfig ( scalastyle_config ) <EOL> return scalastyle_config <EOL> def create_file_excluder ( self ) : <EOL> return FileExcluder ( self . get_options ( ) . excludes , self . context . log ) </s>
