<s> from __future__ import ( absolute_import , division , generators , nested_scopes , print_function , <EOL> unicode_literals , with_statement ) <EOL> import re <EOL> from hashlib import sha1 <EOL> from six import string_types <EOL> from pants . backend . jvm . targets . exclude import Exclude <EOL> from pants . backend . jvm . targets . jvm_target import JvmTarget <EOL> from pants . base . exceptions import TargetDefinitionException <EOL> from pants . base . payload import Payload <EOL> from pants . base . payload_field import ( ExcludesField , FingerprintedField , FingerprintedMixin , <EOL> PrimitiveField ) <EOL> from pants . base . validation import assert_list <EOL> from pants . util . meta import AbstractClass <EOL> class JarRule ( FingerprintedMixin , AbstractClass ) : <EOL> def __init__ ( self , apply_pattern , payload = None ) : <EOL> self . payload = payload or Payload ( ) <EOL> if not isinstance ( apply_pattern , string_types ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> . format ( apply_pattern ) ) <EOL> try : <EOL> self . _apply_pattern = re . compile ( apply_pattern ) <EOL> except re . error as e : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> . format ( pattern = apply_pattern , msg = e ) ) <EOL> self . payload . add_fields ( { <EOL> '<STR_LIT>' : PrimitiveField ( apply_pattern ) , <EOL> } ) <EOL> def fingerprint ( self ) : <EOL> return self . payload . fingerprint ( ) <EOL> @ property <EOL> def apply_pattern ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _apply_pattern <EOL> class Skip ( JarRule ) : <EOL> """<STR_LIT>""" <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . payload . apply_pattern ) <EOL> class Duplicate ( JarRule ) : <EOL> """<STR_LIT>""" <EOL> class Error ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , path ) : <EOL> """<STR_LIT>""" <EOL> assert path and isinstance ( path , string_types ) , '<STR_LIT>' <EOL> super ( Duplicate . Error , self ) . __init__ ( '<STR_LIT>' . format ( path ) ) <EOL> self . _path = path <EOL> @ property <EOL> def path ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _path <EOL> SKIP = '<STR_LIT>' <EOL> """<STR_LIT>""" <EOL> REPLACE = '<STR_LIT>' <EOL> """<STR_LIT>""" <EOL> CONCAT = '<STR_LIT>' <EOL> """<STR_LIT>""" <EOL> CONCAT_TEXT = '<STR_LIT>' <EOL> """<STR_LIT>""" <EOL> FAIL = '<STR_LIT>' <EOL> """<STR_LIT>""" <EOL> _VALID_ACTIONS = frozenset ( ( SKIP , REPLACE , CONCAT , CONCAT_TEXT , FAIL ) ) <EOL> @ classmethod <EOL> def validate_action ( cls , action ) : <EOL> """<STR_LIT>""" <EOL> if action not in cls . _VALID_ACTIONS : <EOL> raise ValueError ( '<STR_LIT>' <EOL> . format ( valid = cls . _VALID_ACTIONS , given = action ) ) <EOL> return action <EOL> def __init__ ( self , apply_pattern , action ) : <EOL> """<STR_LIT>""" <EOL> payload = Payload ( ) <EOL> payload . add_fields ( { <EOL> '<STR_LIT:action>' : PrimitiveField ( self . validate_action ( action ) ) , <EOL> } ) <EOL> super ( Duplicate , self ) . __init__ ( apply_pattern , payload = payload ) <EOL> @ property <EOL> def action ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . payload . action <EOL> def fingerprint ( self ) : <EOL> return self . payload . fingerprint ( ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . payload . apply_pattern , <EOL> self . payload . action ) <EOL> class JarRules ( FingerprintedMixin ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def skip_signatures_and_duplicates_concat_well_known_metadata ( cls , default_dup_action = None , <EOL> additional_rules = None ) : <EOL> """<STR_LIT>""" <EOL> default_dup_action = Duplicate . validate_action ( default_dup_action or Duplicate . SKIP ) <EOL> additional_rules = assert_list ( additional_rules , <EOL> expected_type = ( Duplicate , Skip ) ) <EOL> rules = [ Skip ( r'<STR_LIT>' ) , <EOL> Skip ( r'<STR_LIT>' ) , <EOL> Skip ( r'<STR_LIT>' ) , <EOL> Skip ( r'<STR_LIT>' ) , <EOL> Duplicate ( r'<STR_LIT>' , Duplicate . CONCAT_TEXT ) ] <EOL> return JarRules ( rules = rules + additional_rules , default_dup_action = default_dup_action ) <EOL> _DEFAULT = None <EOL> @ classmethod <EOL> def default ( cls ) : <EOL> """<STR_LIT>""" <EOL> if cls . _DEFAULT is None : <EOL> cls . _DEFAULT = cls . skip_signatures_and_duplicates_concat_well_known_metadata ( ) <EOL> return cls . _DEFAULT <EOL> @ classmethod <EOL> def set_default ( cls , rules ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( rules , JarRules ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> cls . _DEFAULT = rules <EOL> def __init__ ( self , rules = None , default_dup_action = Duplicate . SKIP ) : <EOL> """<STR_LIT>""" <EOL> self . payload = Payload ( ) <EOL> self . payload . add_fields ( { <EOL> '<STR_LIT>' : PrimitiveField ( Duplicate . validate_action ( default_dup_action ) ) <EOL> } ) <EOL> self . _rules = assert_list ( rules , expected_type = JarRule , key_arg = "<STR_LIT>" ) <EOL> @ property <EOL> def default_dup_action ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . payload . default_dup_action <EOL> @ property <EOL> def rules ( self ) : <EOL> """<STR_LIT>""" <EOL> return list ( self . _rules ) <EOL> def fingerprint ( self ) : <EOL> hasher = sha1 ( ) <EOL> hasher . update ( self . payload . fingerprint ( ) ) <EOL> for rule in self . rules : <EOL> hasher . update ( rule . fingerprint ( ) ) <EOL> return hasher . hexdigest ( ) <EOL> @ property <EOL> def value ( self ) : <EOL> return self . _jar_rules <EOL> class ManifestEntries ( FingerprintedMixin ) : <EOL> """<STR_LIT>""" <EOL> class ExpectedDictionaryError ( Exception ) : <EOL> pass <EOL> def __init__ ( self , entries = None ) : <EOL> """<STR_LIT>""" <EOL> self . payload = Payload ( ) <EOL> if entries : <EOL> if not isinstance ( entries , dict ) : <EOL> raise self . ExpectedDictionaryError ( "<STR_LIT>" ) <EOL> for key in entries . keys ( ) : <EOL> if not isinstance ( key , string_types ) : <EOL> raise self . ExpectedDictionaryError ( <EOL> "<STR_LIT>" <EOL> . format ( key , type ( key ) . __name__ ) ) <EOL> self . payload . add_fields ( { <EOL> '<STR_LIT>' : PrimitiveField ( entries or { } ) , <EOL> } ) <EOL> def fingerprint ( self ) : <EOL> return self . payload . fingerprint ( ) <EOL> @ property <EOL> def entries ( self ) : <EOL> return self . payload . entries <EOL> class JvmBinary ( JvmTarget ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> name = None , <EOL> address = None , <EOL> payload = None , <EOL> main = None , <EOL> basename = None , <EOL> source = None , <EOL> deploy_excludes = None , <EOL> deploy_jar_rules = None , <EOL> manifest_entries = None , <EOL> shading_rules = None , <EOL> ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . address = address <EOL> if main and not isinstance ( main , string_types ) : <EOL> raise TargetDefinitionException ( self , '<STR_LIT>' ) <EOL> if source and not isinstance ( source , string_types ) : <EOL> raise TargetDefinitionException ( self , '<STR_LIT>' ) <EOL> if deploy_jar_rules and not isinstance ( deploy_jar_rules , JarRules ) : <EOL> raise TargetDefinitionException ( self , <EOL> '<STR_LIT>' <EOL> . format ( type ( deploy_jar_rules ) . __name__ ) ) <EOL> if manifest_entries and not isinstance ( manifest_entries , dict ) : <EOL> raise TargetDefinitionException ( self , <EOL> '<STR_LIT>' <EOL> . format ( type ( manifest_entries ) . __name__ ) ) <EOL> sources = [ source ] if source else None <EOL> if '<STR_LIT>' in kwargs : <EOL> raise self . IllegalArgument ( address . spec , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> payload = payload or Payload ( ) <EOL> payload . add_fields ( { <EOL> '<STR_LIT>' : PrimitiveField ( basename or name ) , <EOL> '<STR_LIT>' : ExcludesField ( self . assert_list ( deploy_excludes , <EOL> expected_type = Exclude , <EOL> key_arg = '<STR_LIT>' ) ) , <EOL> '<STR_LIT>' : FingerprintedField ( deploy_jar_rules or JarRules . default ( ) ) , <EOL> '<STR_LIT>' : FingerprintedField ( ManifestEntries ( manifest_entries ) ) , <EOL> '<STR_LIT>' : PrimitiveField ( main ) , <EOL> '<STR_LIT>' : PrimitiveField ( shading_rules or ( ) ) , <EOL> } ) <EOL> super ( JvmBinary , self ) . __init__ ( name = name , <EOL> address = address , <EOL> payload = payload , <EOL> sources = self . assert_list ( sources , key_arg = '<STR_LIT>' ) , <EOL> ** kwargs ) <EOL> @ property <EOL> def basename ( self ) : <EOL> return self . payload . basename <EOL> @ property <EOL> def deploy_excludes ( self ) : <EOL> return self . payload . deploy_excludes <EOL> @ property <EOL> def deploy_jar_rules ( self ) : <EOL> return self . payload . deploy_jar_rules <EOL> @ property <EOL> def shading_rules ( self ) : <EOL> return self . payload . shading_rules <EOL> @ property <EOL> def main ( self ) : <EOL> return self . payload . main <EOL> @ property <EOL> def manifest_entries ( self ) : <EOL> return self . payload . manifest_entries </s>
