<s> from __future__ import ( absolute_import , division , generators , nested_scopes , print_function , <EOL> unicode_literals , with_statement ) <EOL> import os <EOL> from contextlib import contextmanager <EOL> from pants . backend . jvm . targets . jvm_binary import JvmBinary <EOL> from pants . backend . jvm . tasks . jar_task import JarBuilderTask <EOL> from pants . base . exceptions import TaskError <EOL> from pants . base . workunit import WorkUnitLabel <EOL> def is_jvm_binary ( target ) : <EOL> return isinstance ( target , JvmBinary ) <EOL> def is_java_library ( target ) : <EOL> return target . has_sources ( '<STR_LIT>' ) <EOL> def is_scala_library ( target ) : <EOL> return target . has_sources ( '<STR_LIT>' ) <EOL> def is_jvm_library ( target ) : <EOL> return ( is_java_library ( target ) <EOL> or is_scala_library ( target ) <EOL> or ( is_jvm_binary ( target ) and target . has_resources ) ) <EOL> class JarCreate ( JarBuilderTask ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def register_options ( cls , register ) : <EOL> super ( JarCreate , cls ) . register_options ( register ) <EOL> register ( '<STR_LIT>' , default = True , type = bool , <EOL> fingerprint = True , <EOL> help = '<STR_LIT>' ) <EOL> @ classmethod <EOL> def product_types ( cls ) : <EOL> return [ '<STR_LIT>' ] <EOL> @ classmethod <EOL> def prepare ( cls , options , round_manager ) : <EOL> super ( JarCreate , cls ) . prepare ( options , round_manager ) <EOL> cls . JarBuilder . prepare ( round_manager ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( JarCreate , self ) . __init__ ( * args , ** kwargs ) <EOL> self . compressed = self . get_options ( ) . compressed <EOL> self . _jars = { } <EOL> @ property <EOL> def cache_target_dirs ( self ) : <EOL> return True <EOL> def execute ( self ) : <EOL> with self . invalidated ( self . context . targets ( is_jvm_library ) ) as invalidation_check : <EOL> with self . context . new_workunit ( name = '<STR_LIT>' , labels = [ WorkUnitLabel . MULTITOOL ] ) : <EOL> jar_mapping = self . context . products . get ( '<STR_LIT>' ) <EOL> for vt in invalidation_check . all_vts : <EOL> jar_name = vt . target . name + '<STR_LIT>' <EOL> jar_path = os . path . join ( vt . results_dir , jar_name ) <EOL> def add_jar_to_products ( ) : <EOL> jar_mapping . add ( vt . target , vt . results_dir ) . append ( jar_name ) <EOL> if vt . valid : <EOL> if os . path . exists ( jar_path ) : <EOL> add_jar_to_products ( ) <EOL> else : <EOL> with self . create_jar ( vt . target , jar_path ) as jarfile : <EOL> with self . create_jar_builder ( jarfile ) as jar_builder : <EOL> if jar_builder . add_target ( vt . target ) : <EOL> add_jar_to_products ( ) <EOL> @ contextmanager <EOL> def create_jar ( self , target , path ) : <EOL> existing = self . _jars . setdefault ( path , target ) <EOL> if target != existing : <EOL> raise TaskError ( <EOL> '<STR_LIT>' <EOL> . format ( target , path , existing ) ) <EOL> self . _jars [ path ] = target <EOL> with self . open_jar ( path , overwrite = True , compressed = self . compressed ) as jar : <EOL> yield jar </s>
