<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import copy <EOL> import json <EOL> import logging <EOL> import salt . utils <EOL> from salt . exceptions import CommandExecutionError , MinionError <EOL> import salt . ext . six as six <EOL> from salt . ext . six . moves import zip <EOL> log = logging . getLogger ( __name__ ) <EOL> __virtualname__ = '<STR_LIT>' <EOL> def __virtual__ ( ) : <EOL> '''<STR_LIT>''' <EOL> if salt . utils . which ( '<STR_LIT>' ) and __grains__ [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> return __virtualname__ <EOL> return ( False , '<STR_LIT>' ) <EOL> def _list_taps ( ) : <EOL> '''<STR_LIT>''' <EOL> cmd = '<STR_LIT>' <EOL> return _call_brew ( cmd ) [ '<STR_LIT>' ] . splitlines ( ) <EOL> def _tap ( tap , runas = None ) : <EOL> '''<STR_LIT>''' <EOL> if tap in _list_taps ( ) : <EOL> return True <EOL> cmd = '<STR_LIT>' . format ( tap ) <EOL> if _call_brew ( cmd ) [ '<STR_LIT>' ] : <EOL> log . error ( '<STR_LIT>' . format ( tap ) ) <EOL> return False <EOL> return True <EOL> def _homebrew_bin ( ) : <EOL> '''<STR_LIT>''' <EOL> ret = __salt__ [ '<STR_LIT>' ] ( '<STR_LIT>' , output_loglevel = '<STR_LIT>' ) <EOL> ret += '<STR_LIT>' <EOL> return ret <EOL> def _call_brew ( cmd , redirect_stderr = False ) : <EOL> '''<STR_LIT>''' <EOL> user = __salt__ [ '<STR_LIT>' ] ( _homebrew_bin ( ) ) <EOL> runas = user if user != __opts__ [ '<STR_LIT:user>' ] else None <EOL> return __salt__ [ '<STR_LIT>' ] ( cmd , <EOL> runas = runas , <EOL> output_loglevel = '<STR_LIT>' , <EOL> python_shell = False , <EOL> redirect_stderr = redirect_stderr ) <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 any ( [ salt . utils . is_true ( kwargs . get ( x ) ) <EOL> for x in ( '<STR_LIT>' , '<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> cmd = '<STR_LIT>' <EOL> ret = { } <EOL> out = _call_brew ( cmd ) [ '<STR_LIT>' ] <EOL> for line in out . splitlines ( ) : <EOL> try : <EOL> name_and_versions = line . split ( '<STR_LIT:U+0020>' ) <EOL> name = name_and_versions [ <NUM_LIT:0> ] <EOL> installed_versions = name_and_versions [ <NUM_LIT:1> : ] <EOL> newest_version = sorted ( installed_versions , cmp = salt . utils . version_cmp ) . pop ( ) <EOL> except ValueError : <EOL> continue <EOL> __salt__ [ '<STR_LIT>' ] ( ret , name , newest_version ) <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 refresh : <EOL> refresh_db ( ) <EOL> def get_version ( pkg_info ) : <EOL> return pkg_info [ '<STR_LIT>' ] [ '<STR_LIT>' ] or pkg_info [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> versions_dict = dict ( ( key , get_version ( val ) ) for key , val in six . iteritems ( _info ( * names ) ) ) <EOL> if len ( names ) == <NUM_LIT:1> : <EOL> return next ( six . itervalues ( versions_dict ) ) <EOL> else : <EOL> return versions_dict <EOL> available_version = salt . utils . alias_function ( latest_version , '<STR_LIT>' ) <EOL> def remove ( name = None , pkgs = None , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> pkg_params = __salt__ [ '<STR_LIT>' ] ( <EOL> name , pkgs , ** kwargs <EOL> ) [ <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> out = _call_brew ( cmd ) <EOL> if out [ '<STR_LIT>' ] != <NUM_LIT:0> and out [ '<STR_LIT>' ] : <EOL> errors = [ out [ '<STR_LIT>' ] ] <EOL> else : <EOL> errors = [ ] <EOL> __context__ . pop ( '<STR_LIT>' , None ) <EOL> new = list_pkgs ( ) <EOL> ret = salt . utils . compare_dicts ( old , new ) <EOL> if errors : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' , <EOL> info = { '<STR_LIT>' : errors , '<STR_LIT>' : ret } <EOL> ) <EOL> return ret <EOL> def refresh_db ( ) : <EOL> '''<STR_LIT>''' <EOL> cmd = '<STR_LIT>' <EOL> if _call_brew ( cmd ) [ '<STR_LIT>' ] : <EOL> log . error ( '<STR_LIT>' ) <EOL> return False <EOL> return True <EOL> def _info ( * pkgs ) : <EOL> '''<STR_LIT>''' <EOL> cmd = '<STR_LIT>' . format ( '<STR_LIT:U+0020>' . join ( pkgs ) ) <EOL> brew_result = _call_brew ( cmd ) <EOL> if brew_result [ '<STR_LIT>' ] : <EOL> log . error ( '<STR_LIT>' . format ( '<STR_LIT:U+0020>' . join ( pkgs ) ) ) <EOL> return { } <EOL> output = json . loads ( brew_result [ '<STR_LIT>' ] ) <EOL> return dict ( zip ( pkgs , output ) ) <EOL> def install ( name = None , pkgs = None , taps = None , options = None , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> pkg_params , pkg_type = __salt__ [ '<STR_LIT>' ] ( <EOL> name , pkgs , kwargs . get ( '<STR_LIT>' , { } ) <EOL> ) <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> formulas = '<STR_LIT:U+0020>' . join ( pkg_params ) <EOL> old = list_pkgs ( ) <EOL> if taps : <EOL> if not isinstance ( taps , list ) : <EOL> taps = [ taps ] <EOL> for tap in taps : <EOL> _tap ( tap ) <EOL> if options : <EOL> cmd = '<STR_LIT>' . format ( formulas , '<STR_LIT:U+0020>' . join ( options ) ) <EOL> else : <EOL> cmd = '<STR_LIT>' . format ( formulas ) <EOL> out = _call_brew ( cmd ) <EOL> if out [ '<STR_LIT>' ] != <NUM_LIT:0> and out [ '<STR_LIT>' ] : <EOL> errors = [ out [ '<STR_LIT>' ] ] <EOL> else : <EOL> errors = [ ] <EOL> __context__ . pop ( '<STR_LIT>' , None ) <EOL> new = list_pkgs ( ) <EOL> ret = salt . utils . compare_dicts ( old , new ) <EOL> if errors : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' , <EOL> info = { '<STR_LIT>' : errors , '<STR_LIT>' : ret } <EOL> ) <EOL> return ret <EOL> def list_upgrades ( refresh = True ) : <EOL> '''<STR_LIT>''' <EOL> if refresh : <EOL> refresh_db ( ) <EOL> cmd = '<STR_LIT>' <EOL> call = _call_brew ( cmd ) <EOL> if call [ '<STR_LIT>' ] != <NUM_LIT:0> : <EOL> comment = '<STR_LIT>' <EOL> if '<STR_LIT>' in call : <EOL> comment += call [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in call : <EOL> comment += call [ '<STR_LIT>' ] <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' . format ( comment ) <EOL> ) <EOL> else : <EOL> out = call [ '<STR_LIT>' ] <EOL> return out . splitlines ( ) <EOL> def upgrade_available ( pkg ) : <EOL> '''<STR_LIT>''' <EOL> return pkg in list_upgrades ( ) <EOL> def upgrade ( refresh = True ) : <EOL> '''<STR_LIT>''' <EOL> ret = { '<STR_LIT>' : { } , <EOL> '<STR_LIT:result>' : True , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> old = list_pkgs ( ) <EOL> if salt . utils . is_true ( refresh ) : <EOL> refresh_db ( ) <EOL> cmd = '<STR_LIT>' <EOL> call = _call_brew ( cmd , redirect_stderr = True ) <EOL> if call [ '<STR_LIT>' ] != <NUM_LIT:0> : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> if call [ '<STR_LIT>' ] : <EOL> ret [ '<STR_LIT>' ] = call [ '<STR_LIT>' ] <EOL> __context__ . pop ( '<STR_LIT>' , None ) <EOL> new = list_pkgs ( ) <EOL> ret [ '<STR_LIT>' ] = salt . utils . compare_dicts ( old , new ) <EOL> return ret </s>
