<s> from __future__ import ( absolute_import , division , generators , nested_scopes , print_function , <EOL> unicode_literals , with_statement ) <EOL> import os <EOL> import shutil <EOL> from pants . util . dirutil import safe_mkdir <EOL> class AndroidDistribution ( object ) : <EOL> """<STR_LIT>""" <EOL> class DistributionError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> _CACHED_SDK = { } <EOL> @ classmethod <EOL> def cached ( cls , path = None ) : <EOL> """<STR_LIT>""" <EOL> dist = cls . _CACHED_SDK . get ( path ) <EOL> if not dist : <EOL> dist = cls . locate_sdk_path ( path ) <EOL> cls . _CACHED_SDK [ path ] = dist <EOL> return dist <EOL> @ classmethod <EOL> def locate_sdk_path ( cls , path = None ) : <EOL> """<STR_LIT>""" <EOL> def sdk_path ( sdk_env_var ) : <EOL> """<STR_LIT>""" <EOL> sdk = os . environ . get ( sdk_env_var ) <EOL> return os . path . abspath ( sdk ) if sdk else None <EOL> def search_path ( path ) : <EOL> """<STR_LIT>""" <EOL> if path : <EOL> yield os . path . abspath ( path ) <EOL> yield sdk_path ( '<STR_LIT>' ) <EOL> yield sdk_path ( '<STR_LIT>' ) <EOL> yield sdk_path ( '<STR_LIT>' ) <EOL> for path in filter ( None , search_path ( path ) ) : <EOL> dist = cls ( sdk_path = path ) <EOL> return dist <EOL> raise cls . DistributionError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def __init__ ( self , sdk_path ) : <EOL> """<STR_LIT>""" <EOL> self . _sdk_path = sdk_path <EOL> self . _validated_tools = { } <EOL> def register_android_tool ( self , tool_path , workdir = None ) : <EOL> """<STR_LIT>""" <EOL> if tool_path not in self . _validated_tools : <EOL> android_tool = self . _get_tool_path ( tool_path ) <EOL> if workdir : <EOL> copy_path = os . path . join ( workdir , tool_path ) <EOL> if not os . path . isfile ( copy_path ) : <EOL> try : <EOL> safe_mkdir ( os . path . dirname ( copy_path ) ) <EOL> shutil . copy ( android_tool , copy_path ) <EOL> except OSError as e : <EOL> raise self . DistributionError ( '<STR_LIT>' . format ( e ) ) <EOL> self . _validated_tools [ tool_path ] = copy_path <EOL> else : <EOL> self . _validated_tools [ tool_path ] = android_tool <EOL> return self . _validated_tools [ tool_path ] <EOL> def _get_tool_path ( self , tool_path ) : <EOL> """<STR_LIT>""" <EOL> android_tool = os . path . join ( self . _sdk_path , tool_path ) <EOL> if os . path . isfile ( android_tool ) : <EOL> return android_tool <EOL> else : <EOL> raise self . DistributionError ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( android_tool ) ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' . format ( self . _sdk_path ) </s>
