<s> from __future__ import ( absolute_import , division , generators , nested_scopes , print_function , <EOL> unicode_literals , with_statement ) <EOL> import logging <EOL> import os <EOL> import subprocess <EOL> from pants . base . exceptions import TaskError <EOL> from pants . base . workunit import WorkUnitLabel <EOL> from pants . java . distribution . distribution import DistributionLocator <EOL> from pants . task . task import Task <EOL> from pants . util . dirutil import safe_mkdir <EOL> from pants . contrib . android . android_config_util import AndroidConfigUtil <EOL> from pants . contrib . android . keystore . keystore_resolver import KeystoreResolver <EOL> from pants . contrib . android . targets . android_binary import AndroidBinary <EOL> logger = logging . getLogger ( __name__ ) <EOL> class SignApkTask ( Task ) : <EOL> """<STR_LIT>""" <EOL> _DEFAULT_KEYSTORE_CONFIG = '<STR_LIT>' <EOL> @ classmethod <EOL> def register_options ( cls , register ) : <EOL> super ( SignApkTask , cls ) . register_options ( register ) <EOL> register ( '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> @ classmethod <EOL> def prepare ( cls , options , round_manager ) : <EOL> super ( SignApkTask , cls ) . prepare ( options , round_manager ) <EOL> round_manager . require_data ( '<STR_LIT>' ) <EOL> @ classmethod <EOL> def is_signtarget ( cls , target ) : <EOL> return isinstance ( target , AndroidBinary ) <EOL> @ classmethod <EOL> def product_types ( cls ) : <EOL> return [ '<STR_LIT>' ] <EOL> @ classmethod <EOL> def setup_default_config ( cls , path ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> AndroidConfigUtil . setup_keystore_config ( path ) <EOL> except AndroidConfigUtil . AndroidConfigError as e : <EOL> raise TaskError ( '<STR_LIT>' . format ( e ) ) <EOL> @ classmethod <EOL> def signed_package_name ( cls , target , build_type ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' . format ( target . manifest . package_name , build_type ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( SignApkTask , self ) . __init__ ( * args , ** kwargs ) <EOL> self . _config_file = self . get_options ( ) . keystore_config_location <EOL> self . _distdir = self . get_options ( ) . pants_distdir <EOL> self . _configdir = self . get_options ( ) . pants_configdir <EOL> self . _dist = None <EOL> @ property <EOL> def config_file ( self ) : <EOL> if not self . _config_file : <EOL> raise TaskError ( '<STR_LIT>' ) <EOL> return os . path . expanduser ( self . _config_file ) <EOL> @ property <EOL> def default_config_location ( self ) : <EOL> """<STR_LIT>""" <EOL> return os . path . join ( self . _configdir , self . _DEFAULT_KEYSTORE_CONFIG ) <EOL> @ property <EOL> def distribution ( self ) : <EOL> if self . _dist is None : <EOL> self . _dist = DistributionLocator . cached ( minimum_version = '<STR_LIT>' , <EOL> maximum_version = '<STR_LIT>' , <EOL> jdk = True ) <EOL> return self . _dist <EOL> def _render_args ( self , target , key , unsigned_apk , outdir ) : <EOL> """<STR_LIT>""" <EOL> args = [ self . distribution . binary ( '<STR_LIT>' ) ] <EOL> args . extend ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> args . extend ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> args . extend ( [ '<STR_LIT>' , key . keystore_location ] ) <EOL> args . extend ( [ '<STR_LIT>' , key . keystore_password ] ) <EOL> args . extend ( [ '<STR_LIT>' , key . key_password ] ) <EOL> args . extend ( [ '<STR_LIT>' , <EOL> os . path . join ( outdir , self . signed_package_name ( target , key . build_type ) ) ] ) <EOL> args . append ( unsigned_apk ) <EOL> args . append ( key . keystore_alias ) <EOL> logger . debug ( '<STR_LIT>' . format ( '<STR_LIT:U+0020>' . join ( args ) ) ) <EOL> return args <EOL> def execute ( self ) : <EOL> if not os . path . isfile ( self . default_config_location ) : <EOL> self . setup_default_config ( self . default_config_location ) <EOL> targets = self . context . targets ( self . is_signtarget ) <EOL> with self . invalidated ( targets ) as invalidation_check : <EOL> invalid_targets = [ ] <EOL> for vt in invalidation_check . invalid_vts : <EOL> invalid_targets . extend ( vt . targets ) <EOL> for target in invalid_targets : <EOL> def get_products_path ( target ) : <EOL> """<STR_LIT>""" <EOL> unsigned_apks = self . context . products . get ( '<STR_LIT>' ) <EOL> packages = unsigned_apks . get ( target ) <EOL> if packages : <EOL> for tgts , products in packages . items ( ) : <EOL> for prod in products : <EOL> yield os . path . join ( tgts , prod ) <EOL> packages = list ( get_products_path ( target ) ) <EOL> for unsigned_apk in packages : <EOL> keystores = KeystoreResolver . resolve ( self . config_file ) <EOL> for key in keystores : <EOL> outdir = self . sign_apk_out ( target , keystores [ key ] . build_type ) <EOL> safe_mkdir ( outdir ) <EOL> args = self . _render_args ( target , keystores [ key ] , unsigned_apk , outdir ) <EOL> with self . context . new_workunit ( name = '<STR_LIT>' , <EOL> labels = [ WorkUnitLabel . MULTITOOL ] ) as workunit : <EOL> returncode = subprocess . call ( args , stdout = workunit . output ( '<STR_LIT>' ) , <EOL> stderr = workunit . output ( '<STR_LIT>' ) ) <EOL> if returncode : <EOL> raise TaskError ( '<STR_LIT>' <EOL> . format ( returncode ) ) <EOL> for target in targets : <EOL> release_path = self . sign_apk_out ( target , '<STR_LIT>' ) <EOL> release_apk = self . signed_package_name ( target , '<STR_LIT>' ) <EOL> if os . path . isfile ( os . path . join ( release_path , release_apk ) ) : <EOL> self . context . products . get ( '<STR_LIT>' ) . add ( target , release_path ) . append ( release_apk ) <EOL> def sign_apk_out ( self , target , build_type ) : <EOL> """<STR_LIT>""" <EOL> if build_type == '<STR_LIT>' : <EOL> return os . path . join ( self . workdir , target . name , build_type ) <EOL> elif build_type == '<STR_LIT>' : <EOL> return os . path . join ( self . _distdir , target . name ) </s>
