<s> from __future__ import ( absolute_import , division , generators , nested_scopes , print_function , <EOL> unicode_literals , with_statement ) <EOL> import hashlib <EOL> import logging <EOL> import os <EOL> import shutil <EOL> from pants . ivy . ivy import Ivy <EOL> from pants . ivy . ivy_subsystem import IvySubsystem <EOL> from pants . net . http . fetcher import Fetcher <EOL> from pants . util . contextutil import temporary_file <EOL> from pants . util . dirutil import safe_delete , touch <EOL> logger = logging . getLogger ( __name__ ) <EOL> class Bootstrapper ( object ) : <EOL> """<STR_LIT>""" <EOL> class Error ( Exception ) : <EOL> """<STR_LIT>""" <EOL> _INSTANCE = None <EOL> @ classmethod <EOL> def default_ivy ( cls , bootstrap_workunit_factory = None ) : <EOL> """<STR_LIT>""" <EOL> return cls . instance ( ) . ivy ( bootstrap_workunit_factory = bootstrap_workunit_factory ) <EOL> def __init__ ( self , ivy_subsystem = None ) : <EOL> """<STR_LIT>""" <EOL> self . _ivy_subsystem = ivy_subsystem or IvySubsystem . global_instance ( ) <EOL> self . _version_or_ivyxml = self . _ivy_subsystem . get_options ( ) . ivy_profile <EOL> self . _classpath = None <EOL> @ classmethod <EOL> def instance ( cls ) : <EOL> """<STR_LIT>""" <EOL> if cls . _INSTANCE is None : <EOL> cls . _INSTANCE = Bootstrapper ( ) <EOL> return cls . _INSTANCE <EOL> @ classmethod <EOL> def reset_instance ( cls ) : <EOL> cls . _INSTANCE = None <EOL> def ivy ( self , bootstrap_workunit_factory = None ) : <EOL> """<STR_LIT>""" <EOL> return Ivy ( self . _get_classpath ( bootstrap_workunit_factory ) , <EOL> ivy_settings = self . _ivy_subsystem . get_options ( ) . ivy_settings , <EOL> ivy_cache_dir = self . _ivy_subsystem . get_options ( ) . cache_dir , <EOL> extra_jvm_options = self . _ivy_subsystem . extra_jvm_options ( ) ) <EOL> def _get_classpath ( self , workunit_factory ) : <EOL> """<STR_LIT>""" <EOL> if not self . _classpath : <EOL> self . _classpath = self . _bootstrap_ivy_classpath ( workunit_factory ) <EOL> return self . _classpath <EOL> def _bootstrap_ivy_classpath ( self , workunit_factory , retry = True ) : <EOL> ivy_bootstrap_dir = os . path . join ( self . _ivy_subsystem . get_options ( ) . pants_bootstrapdir , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> digest = hashlib . sha1 ( ) <EOL> if os . path . isfile ( self . _version_or_ivyxml ) : <EOL> with open ( self . _version_or_ivyxml ) as fp : <EOL> digest . update ( fp . read ( ) ) <EOL> else : <EOL> digest . update ( self . _version_or_ivyxml ) <EOL> classpath = os . path . join ( ivy_bootstrap_dir , '<STR_LIT>' . format ( digest . hexdigest ( ) ) ) <EOL> if not os . path . exists ( classpath ) : <EOL> ivy = self . _bootstrap_ivy ( os . path . join ( ivy_bootstrap_dir , '<STR_LIT>' ) ) <EOL> args = [ '<STR_LIT>' , '<STR_LIT:default>' , '<STR_LIT>' , classpath ] <EOL> if os . path . isfile ( self . _version_or_ivyxml ) : <EOL> args . extend ( [ '<STR_LIT>' , self . _version_or_ivyxml ] ) <EOL> else : <EOL> args . extend ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , self . _version_or_ivyxml ] ) <EOL> try : <EOL> ivy . execute ( args = args , workunit_factory = workunit_factory , workunit_name = '<STR_LIT>' ) <EOL> except ivy . Error as e : <EOL> safe_delete ( classpath ) <EOL> raise self . Error ( '<STR_LIT>' . format ( e ) ) <EOL> with open ( classpath ) as fp : <EOL> cp = fp . read ( ) . strip ( ) . split ( os . pathsep ) <EOL> if not all ( map ( os . path . exists , cp ) ) : <EOL> safe_delete ( classpath ) <EOL> if retry : <EOL> return self . _bootstrap_ivy_classpath ( workunit_factory , retry = False ) <EOL> raise self . Error ( '<STR_LIT>' . format ( '<STR_LIT::>' . join ( cp ) ) ) <EOL> return cp <EOL> def _bootstrap_ivy ( self , bootstrap_jar_path ) : <EOL> options = self . _ivy_subsystem . get_options ( ) <EOL> if not os . path . exists ( bootstrap_jar_path ) : <EOL> with temporary_file ( ) as bootstrap_jar : <EOL> fetcher = Fetcher ( ) <EOL> checksummer = fetcher . ChecksumListener ( digest = hashlib . sha1 ( ) ) <EOL> try : <EOL> logger . info ( '<STR_LIT>' . format ( options . bootstrap_jar_url ) ) <EOL> fetcher . download ( options . bootstrap_jar_url , <EOL> listener = fetcher . ProgressListener ( ) . wrap ( checksummer ) , <EOL> path_or_fd = bootstrap_jar , <EOL> timeout_secs = options . bootstrap_fetch_timeout_secs ) <EOL> logger . info ( '<STR_LIT>' . format ( checksummer . checksum ) ) <EOL> bootstrap_jar . close ( ) <EOL> touch ( bootstrap_jar_path ) <EOL> shutil . move ( bootstrap_jar . name , bootstrap_jar_path ) <EOL> except fetcher . Error as e : <EOL> raise self . Error ( '<STR_LIT>' . format ( e ) ) <EOL> return Ivy ( bootstrap_jar_path , <EOL> ivy_settings = options . bootstrap_ivy_settings or options . ivy_settings , <EOL> ivy_cache_dir = options . cache_dir , <EOL> extra_jvm_options = self . _ivy_subsystem . extra_jvm_options ( ) ) </s>
