<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import logging <EOL> import os <EOL> import shutil <EOL> import salt . utils <EOL> from salt . states . git import _fail , _neutral_test <EOL> log = logging . getLogger ( __name__ ) <EOL> if salt . utils . is_windows ( ) : <EOL> HG_BINARY = "<STR_LIT>" <EOL> else : <EOL> HG_BINARY = "<STR_LIT>" <EOL> def __virtual__ ( ) : <EOL> '''<STR_LIT>''' <EOL> return __salt__ [ '<STR_LIT>' ] ( HG_BINARY ) <EOL> def latest ( name , <EOL> rev = None , <EOL> target = None , <EOL> clean = False , <EOL> user = None , <EOL> identity = None , <EOL> force = False , <EOL> opts = False ) : <EOL> '''<STR_LIT>''' <EOL> ret = { '<STR_LIT:name>' : name , '<STR_LIT:result>' : True , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : { } } <EOL> if not target : <EOL> return _fail ( ret , '<STR_LIT>' ) <EOL> is_repository = ( <EOL> os . path . isdir ( target ) and <EOL> os . path . isdir ( '<STR_LIT>' . format ( target ) ) ) <EOL> if is_repository : <EOL> ret = _update_repo ( ret , name , target , clean , user , identity , rev , opts ) <EOL> else : <EOL> if os . path . isdir ( target ) : <EOL> fail = _handle_existing ( ret , target , force ) <EOL> if fail is not None : <EOL> return fail <EOL> else : <EOL> log . debug ( <EOL> '<STR_LIT>' . format ( <EOL> target ) ) <EOL> if __opts__ [ '<STR_LIT:test>' ] : <EOL> return _neutral_test ( <EOL> ret , <EOL> '<STR_LIT>' . format ( <EOL> name , target ) ) <EOL> _clone_repo ( ret , target , name , user , identity , rev , opts ) <EOL> return ret <EOL> def _update_repo ( ret , name , target , clean , user , identity , rev , opts ) : <EOL> '''<STR_LIT>''' <EOL> log . debug ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( target ) <EOL> ) <EOL> current_rev = __salt__ [ '<STR_LIT>' ] ( target , user = user , rev = '<STR_LIT:.>' ) <EOL> if not current_rev : <EOL> return _fail ( <EOL> ret , <EOL> '<STR_LIT>' . format ( target ) ) <EOL> if __opts__ [ '<STR_LIT:test>' ] : <EOL> test_result = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) . format ( target , current_rev ) <EOL> return _neutral_test ( <EOL> ret , <EOL> test_result ) <EOL> pull_out = __salt__ [ '<STR_LIT>' ] ( target , user = user , identity = identity , opts = opts , repository = name ) <EOL> if rev : <EOL> __salt__ [ '<STR_LIT>' ] ( target , rev , force = clean , user = user ) <EOL> else : <EOL> __salt__ [ '<STR_LIT>' ] ( target , '<STR_LIT>' , force = clean , user = user ) <EOL> new_rev = __salt__ [ '<STR_LIT>' ] ( cwd = target , user = user , rev = '<STR_LIT:.>' ) <EOL> if current_rev != new_rev : <EOL> revision_text = '<STR_LIT>' . format ( current_rev , new_rev ) <EOL> log . info ( <EOL> '<STR_LIT>' . format ( <EOL> target , revision_text ) <EOL> ) <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( target ) <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] = revision_text <EOL> elif '<STR_LIT>' in pull_out : <EOL> return _fail ( <EOL> ret , <EOL> '<STR_LIT>' . format ( pull_out ) <EOL> ) <EOL> return ret <EOL> def _handle_existing ( ret , target , force ) : <EOL> not_empty = os . listdir ( target ) <EOL> if not not_empty : <EOL> log . debug ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( target ) ) <EOL> shutil . rmtree ( target ) <EOL> elif force : <EOL> log . debug ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( target ) ) <EOL> shutil . rmtree ( target ) <EOL> else : <EOL> return _fail ( ret , '<STR_LIT>' ) <EOL> def _clone_repo ( ret , target , name , user , identity , rev , opts ) : <EOL> result = __salt__ [ '<STR_LIT>' ] ( target , name , user = user , identity = identity , opts = opts ) <EOL> if not os . path . isdir ( target ) : <EOL> return _fail ( ret , result ) <EOL> if rev : <EOL> __salt__ [ '<STR_LIT>' ] ( target , rev , user = user ) <EOL> new_rev = __salt__ [ '<STR_LIT>' ] ( cwd = target , user = user ) <EOL> message = '<STR_LIT>' . format ( name , target ) <EOL> log . info ( message ) <EOL> ret [ '<STR_LIT>' ] = message <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] = name <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] = new_rev <EOL> return ret </s>
