<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import copy <EOL> import salt . utils <EOL> from salt . exceptions import CommandExecutionError , MinionError <EOL> import salt . ext . six as six <EOL> __virtualname__ = '<STR_LIT>' <EOL> def __virtual__ ( ) : <EOL> '''<STR_LIT>''' <EOL> if __grains__ [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> return __virtualname__ <EOL> return ( False , '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def refresh_db ( ) : <EOL> '''<STR_LIT>''' <EOL> return __salt__ [ '<STR_LIT>' ] ( '<STR_LIT>' ) == <NUM_LIT:0> <EOL> def upgrade_available ( name ) : <EOL> '''<STR_LIT>''' <EOL> version_num = None <EOL> cmd = '<STR_LIT>' . format ( <EOL> name ) <EOL> out = __salt__ [ '<STR_LIT>' ] ( cmd ) <EOL> if out : <EOL> version_num = out . split ( ) [ <NUM_LIT:2> ] . strip ( ) <EOL> if version_num : <EOL> if version_num == "<STR_LIT>" : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return version_num <EOL> return '<STR_LIT>' <EOL> def list_upgrades ( refresh = True ) : <EOL> '''<STR_LIT>''' <EOL> if salt . utils . is_true ( refresh ) : <EOL> refresh_db ( ) <EOL> upgrades = { } <EOL> lines = __salt__ [ '<STR_LIT>' ] ( <EOL> '<STR_LIT>' ) . splitlines ( ) <EOL> for line in lines : <EOL> comps = line . split ( '<STR_LIT:\t>' ) <EOL> if comps [ <NUM_LIT:2> ] == "<STR_LIT>" : <EOL> continue <EOL> if comps [ <NUM_LIT:2> ] == "<STR_LIT>" : <EOL> continue <EOL> upgrades [ comps [ <NUM_LIT:0> ] ] = comps [ <NUM_LIT:1> ] <EOL> return upgrades <EOL> def upgrade ( refresh = True ) : <EOL> '''<STR_LIT>''' <EOL> if salt . utils . is_true ( refresh ) : <EOL> refresh_db ( ) <EOL> old = list_pkgs ( ) <EOL> cmd = '<STR_LIT>' <EOL> __salt__ [ '<STR_LIT>' ] ( cmd ) <EOL> __context__ . pop ( '<STR_LIT>' , None ) <EOL> new = list_pkgs ( ) <EOL> return salt . utils . compare_dicts ( old , new ) <EOL> def list_pkgs ( versions_as_list = False , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> versions_as_list = salt . utils . is_true ( versions_as_list ) <EOL> if salt . utils . is_true ( kwargs . get ( '<STR_LIT>' ) ) : <EOL> return { } <EOL> if '<STR_LIT>' in __context__ : <EOL> if versions_as_list : <EOL> return __context__ [ '<STR_LIT>' ] <EOL> else : <EOL> ret = copy . deepcopy ( __context__ [ '<STR_LIT>' ] ) <EOL> __salt__ [ '<STR_LIT>' ] ( ret ) <EOL> return ret <EOL> ret = { } <EOL> cmd = '<STR_LIT>' <EOL> lines = __salt__ [ '<STR_LIT>' ] ( cmd ) . splitlines ( ) <EOL> for index , line in enumerate ( lines ) : <EOL> if index % <NUM_LIT:2> == <NUM_LIT:0> : <EOL> name = line . split ( ) [ <NUM_LIT:0> ] . strip ( ) <EOL> if index % <NUM_LIT:2> == <NUM_LIT:1> : <EOL> version_num = line . split ( ) [ <NUM_LIT:1> ] . strip ( ) <EOL> __salt__ [ '<STR_LIT>' ] ( ret , name , version_num ) <EOL> __salt__ [ '<STR_LIT>' ] ( ret ) <EOL> __context__ [ '<STR_LIT>' ] = copy . deepcopy ( ret ) <EOL> if not versions_as_list : <EOL> __salt__ [ '<STR_LIT>' ] ( ret ) <EOL> return ret <EOL> def version ( * names , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> return __salt__ [ '<STR_LIT>' ] ( * names , ** kwargs ) <EOL> def latest_version ( * names , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> refresh = salt . utils . is_true ( kwargs . pop ( '<STR_LIT>' , True ) ) <EOL> if not names : <EOL> return '<STR_LIT>' <EOL> ret = { } <EOL> for name in names : <EOL> ret [ name ] = '<STR_LIT>' <EOL> if refresh : <EOL> refresh_db ( ) <EOL> pkgs = list_pkgs ( ) <EOL> cmd = '<STR_LIT>' . format ( '<STR_LIT:U+0020>' . join ( names ) ) <EOL> output = __salt__ [ '<STR_LIT>' ] ( cmd ) . get ( '<STR_LIT>' , '<STR_LIT>' ) . splitlines ( ) <EOL> for line in output : <EOL> try : <EOL> name , version_rev = line . split ( ) [ <NUM_LIT:1> : <NUM_LIT:3> ] <EOL> except ValueError : <EOL> continue <EOL> if name in names : <EOL> cver = pkgs . get ( name , '<STR_LIT>' ) <EOL> nver = version_rev . split ( '<STR_LIT:U+002C>' ) [ <NUM_LIT:0> ] <EOL> if not cver or salt . utils . compare_versions ( ver1 = cver , <EOL> oper = '<STR_LIT:<>' , <EOL> ver2 = nver ) : <EOL> ret [ name ] = version_rev <EOL> if len ( names ) == <NUM_LIT:1> : <EOL> return ret [ names [ <NUM_LIT:0> ] ] <EOL> return ret <EOL> available_version = salt . utils . alias_function ( latest_version , '<STR_LIT>' ) <EOL> def install ( name = None , refresh = False , version = None , pkgs = None , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> if refresh : <EOL> refresh_db ( ) <EOL> try : <EOL> pkg_params = __salt__ [ '<STR_LIT>' ] ( name , <EOL> pkgs , <EOL> ** kwargs ) [ <NUM_LIT:0> ] <EOL> except MinionError as exc : <EOL> raise CommandExecutionError ( exc ) <EOL> if pkg_params is None or len ( pkg_params ) == <NUM_LIT:0> : <EOL> return { } <EOL> if pkgs is None and version and len ( pkg_params ) == <NUM_LIT:1> : <EOL> pkg_params = { name : version } <EOL> targets = [ ] <EOL> for param , pkgver in six . iteritems ( pkg_params ) : <EOL> if pkgver is None : <EOL> targets . append ( param ) <EOL> else : <EOL> targets . append ( '<STR_LIT>' . format ( param , pkgver ) ) <EOL> cmd = '<STR_LIT>' . format ( '<STR_LIT:U+0020>' . join ( targets ) ) <EOL> old = list_pkgs ( ) <EOL> __salt__ [ '<STR_LIT>' ] ( cmd ) <EOL> __context__ . pop ( '<STR_LIT>' , None ) <EOL> new = list_pkgs ( ) <EOL> return salt . utils . compare_dicts ( old , new ) <EOL> def remove ( name = None , pkgs = None , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> pkg_params = __salt__ [ '<STR_LIT>' ] ( name , pkgs ) [ <NUM_LIT:0> ] <EOL> except MinionError as exc : <EOL> raise CommandExecutionError ( exc ) <EOL> old = list_pkgs ( ) <EOL> targets = [ x for x in pkg_params if x in old ] <EOL> if not targets : <EOL> return { } <EOL> cmd = '<STR_LIT>' . format ( '<STR_LIT:U+0020>' . join ( targets ) ) <EOL> __salt__ [ '<STR_LIT>' ] ( cmd ) <EOL> __context__ . pop ( '<STR_LIT>' , None ) <EOL> new = list_pkgs ( ) <EOL> return salt . utils . compare_dicts ( old , new ) <EOL> def purge ( name = None , pkgs = None , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> return remove ( name = name , pkgs = pkgs ) </s>
