<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import logging <EOL> import os <EOL> import re <EOL> import datetime <EOL> import salt . utils <EOL> import salt . utils . itertools <EOL> import salt . utils . decorators as decorators <EOL> import salt . utils . pkg . rpm <EOL> from salt . ext . six . moves import zip <EOL> try : <EOL> import rpm <EOL> HAS_RPM = True <EOL> except ImportError : <EOL> HAS_RPM = False <EOL> try : <EOL> import rpmUtils . miscutils <EOL> HAS_RPMUTILS = True <EOL> except ImportError : <EOL> HAS_RPMUTILS = False <EOL> from salt . exceptions import CommandExecutionError , SaltInvocationError <EOL> log = logging . getLogger ( __name__ ) <EOL> __virtualname__ = '<STR_LIT>' <EOL> def __virtual__ ( ) : <EOL> '''<STR_LIT>''' <EOL> if not salt . utils . which ( '<STR_LIT>' ) : <EOL> return ( False , '<STR_LIT>' ) <EOL> try : <EOL> os_grain = __grains__ [ '<STR_LIT>' ] . lower ( ) <EOL> os_family = __grains__ [ '<STR_LIT>' ] . lower ( ) <EOL> except Exception : <EOL> return ( False , '<STR_LIT>' ) <EOL> enabled = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if os_family in [ '<STR_LIT>' , '<STR_LIT>' ] or os_grain in enabled : <EOL> return __virtualname__ <EOL> return ( False , '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def bin_pkg_info ( path , saltenv = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> if __salt__ [ '<STR_LIT>' ] ( path ) : <EOL> newpath = __salt__ [ '<STR_LIT>' ] ( path , saltenv ) <EOL> if not newpath : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' <EOL> . format ( path , saltenv ) <EOL> ) <EOL> path = newpath <EOL> else : <EOL> if not os . path . exists ( path ) : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' . format ( path ) <EOL> ) <EOL> elif not os . path . isabs ( path ) : <EOL> raise SaltInvocationError ( <EOL> '<STR_LIT>' . format ( path ) <EOL> ) <EOL> queryformat = salt . utils . pkg . rpm . QUERYFORMAT . replace ( '<STR_LIT>' , '<STR_LIT:none>' ) <EOL> output = __salt__ [ '<STR_LIT>' ] ( <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , queryformat , path ] , <EOL> output_loglevel = '<STR_LIT>' , <EOL> ignore_retcode = True , <EOL> python_shell = False <EOL> ) <EOL> ret = { } <EOL> pkginfo = salt . utils . pkg . rpm . parse_pkginfo ( <EOL> output , <EOL> osarch = __grains__ [ '<STR_LIT>' ] <EOL> ) <EOL> for field in pkginfo . _fields : <EOL> ret [ field ] = getattr ( pkginfo , field ) <EOL> return ret <EOL> def list_pkgs ( * packages ) : <EOL> '''<STR_LIT>''' <EOL> pkgs = { } <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' if packages else '<STR_LIT>' , <EOL> '<STR_LIT>' , r'<STR_LIT>' ] <EOL> if packages : <EOL> cmd . extend ( packages ) <EOL> out = __salt__ [ '<STR_LIT>' ] ( cmd , output_loglevel = '<STR_LIT>' , python_shell = False ) <EOL> for line in salt . utils . itertools . split ( out , '<STR_LIT:\n>' ) : <EOL> if '<STR_LIT>' in line : <EOL> continue <EOL> comps = line . split ( ) <EOL> pkgs [ comps [ <NUM_LIT:0> ] ] = comps [ <NUM_LIT:1> ] <EOL> return pkgs <EOL> def verify ( * packages , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> ftypes = { '<STR_LIT:c>' : '<STR_LIT>' , <EOL> '<STR_LIT:d>' : '<STR_LIT>' , <EOL> '<STR_LIT:g>' : '<STR_LIT>' , <EOL> '<STR_LIT:l>' : '<STR_LIT>' , <EOL> '<STR_LIT:r>' : '<STR_LIT>' } <EOL> ret = { } <EOL> ignore_types = kwargs . get ( '<STR_LIT>' , [ ] ) <EOL> if packages : <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> cmd . extend ( packages ) <EOL> else : <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> out = __salt__ [ '<STR_LIT>' ] ( cmd , <EOL> output_loglevel = '<STR_LIT>' , <EOL> ignore_retcode = True , <EOL> python_shell = False ) <EOL> for line in salt . utils . itertools . split ( out , '<STR_LIT:\n>' ) : <EOL> fdict = { '<STR_LIT>' : [ ] } <EOL> if '<STR_LIT>' in line : <EOL> line = '<STR_LIT:U+0020>' + line <EOL> fdict [ '<STR_LIT>' ] = True <EOL> del fdict [ '<STR_LIT>' ] <EOL> fname = line [ <NUM_LIT> : ] <EOL> if line [ <NUM_LIT:11> : <NUM_LIT:12> ] in ftypes : <EOL> fdict [ '<STR_LIT:type>' ] = ftypes [ line [ <NUM_LIT:11> : <NUM_LIT:12> ] ] <EOL> if '<STR_LIT:type>' not in fdict or fdict [ '<STR_LIT:type>' ] not in ignore_types : <EOL> if line [ <NUM_LIT:0> : <NUM_LIT:1> ] == '<STR_LIT:S>' : <EOL> fdict [ '<STR_LIT>' ] . append ( '<STR_LIT:size>' ) <EOL> if line [ <NUM_LIT:1> : <NUM_LIT:2> ] == '<STR_LIT:M>' : <EOL> fdict [ '<STR_LIT>' ] . append ( '<STR_LIT>' ) <EOL> if line [ <NUM_LIT:2> : <NUM_LIT:3> ] == '<STR_LIT:5>' : <EOL> fdict [ '<STR_LIT>' ] . append ( '<STR_LIT>' ) <EOL> if line [ <NUM_LIT:3> : <NUM_LIT:4> ] == '<STR_LIT:D>' : <EOL> fdict [ '<STR_LIT>' ] . append ( '<STR_LIT>' ) <EOL> if line [ <NUM_LIT:4> : <NUM_LIT:5> ] == '<STR_LIT:L>' : <EOL> fdict [ '<STR_LIT>' ] . append ( '<STR_LIT>' ) <EOL> if line [ <NUM_LIT:5> : <NUM_LIT:6> ] == '<STR_LIT>' : <EOL> fdict [ '<STR_LIT>' ] . append ( '<STR_LIT:user>' ) <EOL> if line [ <NUM_LIT:6> : <NUM_LIT:7> ] == '<STR_LIT>' : <EOL> fdict [ '<STR_LIT>' ] . append ( '<STR_LIT>' ) <EOL> if line [ <NUM_LIT:7> : <NUM_LIT:8> ] == '<STR_LIT:T>' : <EOL> fdict [ '<STR_LIT>' ] . append ( '<STR_LIT>' ) <EOL> if line [ <NUM_LIT:8> : <NUM_LIT:9> ] == '<STR_LIT:P>' : <EOL> fdict [ '<STR_LIT>' ] . append ( '<STR_LIT>' ) <EOL> ret [ fname ] = fdict <EOL> return ret <EOL> def modified ( * packages , ** flags ) : <EOL> '''<STR_LIT>''' <EOL> ret = __salt__ [ '<STR_LIT>' ] ( <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] + list ( packages ) , <EOL> output_loglevel = '<STR_LIT>' , <EOL> python_shell = False ) <EOL> data = { } <EOL> if ret [ '<STR_LIT>' ] > <NUM_LIT:1> : <EOL> del ret [ '<STR_LIT>' ] <EOL> return ret <EOL> elif not ret [ '<STR_LIT>' ] : <EOL> return data <EOL> ptrn = re . compile ( r"<STR_LIT>" ) <EOL> changes = cfg = f_name = None <EOL> for f_info in salt . utils . itertools . split ( ret [ '<STR_LIT>' ] , '<STR_LIT:\n>' ) : <EOL> f_info = ptrn . split ( f_info ) <EOL> if len ( f_info ) == <NUM_LIT:3> : <EOL> changes , cfg , f_name = f_info <EOL> else : <EOL> changes , f_name = f_info <EOL> cfg = None <EOL> keys = [ '<STR_LIT:size>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:time>' , '<STR_LIT>' ] <EOL> changes = list ( changes ) <EOL> if len ( changes ) == <NUM_LIT:8> : <EOL> changes . append ( '<STR_LIT:.>' ) <EOL> stats = [ ] <EOL> for k , v in zip ( keys , changes ) : <EOL> if v != '<STR_LIT:.>' : <EOL> stats . append ( k ) <EOL> if cfg is not None : <EOL> stats . append ( '<STR_LIT>' ) <EOL> data [ f_name ] = stats <EOL> if not flags : <EOL> return data <EOL> filtered_data = { } <EOL> for f_name , stats in data . items ( ) : <EOL> include = True <EOL> for param , pval in flags . items ( ) : <EOL> if param . startswith ( "<STR_LIT:_>" ) : <EOL> continue <EOL> if ( not pval and param in stats ) or ( pval and param not in stats ) : <EOL> include = False <EOL> break <EOL> if include : <EOL> filtered_data [ f_name ] = stats <EOL> return filtered_data <EOL> def file_list ( * packages ) : <EOL> '''<STR_LIT>''' <EOL> if not packages : <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> else : <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> cmd . extend ( packages ) <EOL> ret = __salt__ [ '<STR_LIT>' ] ( <EOL> cmd , <EOL> output_loglevel = '<STR_LIT>' , <EOL> python_shell = False ) . splitlines ( ) <EOL> return { '<STR_LIT>' : [ ] , '<STR_LIT>' : ret } <EOL> def file_dict ( * packages ) : <EOL> '''<STR_LIT>''' <EOL> errors = [ ] <EOL> ret = { } <EOL> pkgs = { } <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' if packages else '<STR_LIT>' , <EOL> '<STR_LIT>' , r'<STR_LIT>' ] <EOL> if packages : <EOL> cmd . extend ( packages ) <EOL> out = __salt__ [ '<STR_LIT>' ] ( cmd , output_loglevel = '<STR_LIT>' , python_shell = False ) <EOL> for line in salt . utils . itertools . split ( out , '<STR_LIT:\n>' ) : <EOL> if '<STR_LIT>' in line : <EOL> errors . append ( line ) <EOL> continue <EOL> comps = line . split ( ) <EOL> pkgs [ comps [ <NUM_LIT:0> ] ] = { '<STR_LIT:version>' : comps [ <NUM_LIT:1> ] } <EOL> for pkg in pkgs : <EOL> files = [ ] <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' , pkg ] <EOL> out = __salt__ [ '<STR_LIT>' ] ( <EOL> [ '<STR_LIT>' , '<STR_LIT>' , pkg ] , <EOL> output_loglevel = '<STR_LIT>' , <EOL> python_shell = False ) <EOL> ret [ pkg ] = out . splitlines ( ) <EOL> return { '<STR_LIT>' : errors , '<STR_LIT>' : ret } <EOL> def owner ( * paths ) : <EOL> '''<STR_LIT>''' <EOL> if not paths : <EOL> return '<STR_LIT>' <EOL> ret = { } <EOL> for path in paths : <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , path ] <EOL> ret [ path ] = __salt__ [ '<STR_LIT>' ] ( cmd , <EOL> output_loglevel = '<STR_LIT>' , <EOL> python_shell = False ) <EOL> if '<STR_LIT>' in ret [ path ] . lower ( ) : <EOL> ret [ path ] = '<STR_LIT>' <EOL> if len ( ret ) == <NUM_LIT:1> : <EOL> return list ( ret . values ( ) ) [ <NUM_LIT:0> ] <EOL> return ret <EOL> @ decorators . which ( '<STR_LIT>' ) <EOL> @ decorators . which ( '<STR_LIT>' ) <EOL> @ decorators . which ( '<STR_LIT>' ) <EOL> def diff ( package , path ) : <EOL> '''<STR_LIT>''' <EOL> cmd = "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" <EOL> res = __salt__ [ '<STR_LIT>' ] ( cmd . format ( package , path ) , <EOL> output_loglevel = '<STR_LIT>' ) <EOL> if res and res . startswith ( '<STR_LIT>' ) : <EOL> return '<STR_LIT>' '<STR_LIT>' . format ( path ) <EOL> return res <EOL> def info ( * packages , ** attr ) : <EOL> '''<STR_LIT>''' <EOL> rpm_tags = __salt__ [ '<STR_LIT>' ] ( <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> python_shell = False ) . splitlines ( ) <EOL> if '<STR_LIT>' in rpm_tags : <EOL> size_tag = '<STR_LIT>' <EOL> else : <EOL> size_tag = '<STR_LIT>' <EOL> cmd = packages and "<STR_LIT>" . format ( '<STR_LIT:U+0020>' . join ( packages ) ) or "<STR_LIT>" <EOL> attr_map = { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:version>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:size>" : "<STR_LIT>" + size_tag + "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:url>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:description>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> attr = attr . get ( '<STR_LIT>' , None ) and attr [ '<STR_LIT>' ] . split ( "<STR_LIT:U+002C>" ) or None <EOL> query = list ( ) <EOL> if attr : <EOL> for attr_k in attr : <EOL> if attr_k in attr_map and attr_k != '<STR_LIT:description>' : <EOL> query . append ( attr_map [ attr_k ] ) <EOL> if not query : <EOL> raise CommandExecutionError ( '<STR_LIT>' ) <EOL> if '<STR_LIT:name>' not in attr : <EOL> attr . append ( '<STR_LIT:name>' ) <EOL> query . append ( attr_map [ '<STR_LIT:name>' ] ) <EOL> if '<STR_LIT>' not in attr : <EOL> attr . append ( '<STR_LIT>' ) <EOL> query . append ( attr_map [ '<STR_LIT>' ] ) <EOL> else : <EOL> for attr_k , attr_v in attr_map . iteritems ( ) : <EOL> if attr_k != '<STR_LIT:description>' : <EOL> query . append ( attr_v ) <EOL> if attr and '<STR_LIT:description>' in attr or not attr : <EOL> query . append ( attr_map [ '<STR_LIT:description>' ] ) <EOL> query . append ( "<STR_LIT>" ) <EOL> call = __salt__ [ '<STR_LIT>' ] ( cmd + ( "<STR_LIT>" . format ( '<STR_LIT>' . join ( query ) ) ) , <EOL> output_loglevel = '<STR_LIT>' , env = { '<STR_LIT>' : '<STR_LIT>' } , clean_env = True ) <EOL> if call [ '<STR_LIT>' ] != <NUM_LIT:0> : <EOL> comment = '<STR_LIT>' <EOL> if '<STR_LIT>' in call : <EOL> comment += ( call [ '<STR_LIT>' ] or call [ '<STR_LIT>' ] ) <EOL> raise CommandExecutionError ( '<STR_LIT>' . format ( comment ) ) <EOL> elif '<STR_LIT:error>' in call [ '<STR_LIT>' ] : <EOL> raise CommandExecutionError ( call [ '<STR_LIT>' ] ) <EOL> else : <EOL> out = call [ '<STR_LIT>' ] <EOL> _ret = list ( ) <EOL> for pkg_info in re . split ( r"<STR_LIT>" , out ) : <EOL> pkg_info = pkg_info . strip ( ) <EOL> if not pkg_info : <EOL> continue <EOL> pkg_info = pkg_info . split ( os . linesep ) <EOL> if pkg_info [ - <NUM_LIT:1> ] . lower ( ) . startswith ( '<STR_LIT>' ) : <EOL> pkg_info = pkg_info [ : - <NUM_LIT:1> ] <EOL> pkg_data = dict ( ) <EOL> pkg_name = None <EOL> descr_marker = False <EOL> descr = list ( ) <EOL> for line in pkg_info : <EOL> if descr_marker : <EOL> descr . append ( line ) <EOL> continue <EOL> line = [ item . strip ( ) for item in line . split ( '<STR_LIT::>' , <NUM_LIT:1> ) ] <EOL> if len ( line ) != <NUM_LIT:2> : <EOL> continue <EOL> key , value = line <EOL> if key == '<STR_LIT:description>' : <EOL> descr_marker = True <EOL> continue <EOL> if key == '<STR_LIT:name>' : <EOL> pkg_name = value <EOL> if key in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> try : <EOL> pkg_data [ key ] = datetime . datetime . fromtimestamp ( int ( value ) ) . isoformat ( ) + "<STR_LIT>" <EOL> except ValueError : <EOL> log . warning ( '<STR_LIT>' . format ( value ) ) <EOL> continue <EOL> if key in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> try : <EOL> pkg_data [ key ] = int ( value ) <EOL> except ValueError : <EOL> log . warning ( '<STR_LIT>' . format ( value ) ) <EOL> continue <EOL> if key not in [ '<STR_LIT:description>' , '<STR_LIT:name>' ] and value : <EOL> pkg_data [ key ] = value <EOL> if attr and '<STR_LIT:description>' in attr or not attr : <EOL> pkg_data [ '<STR_LIT:description>' ] = os . linesep . join ( descr ) <EOL> if pkg_name : <EOL> pkg_data [ '<STR_LIT:name>' ] = pkg_name <EOL> _ret . append ( pkg_data ) <EOL> ret = dict ( ) <EOL> for pkg_data in reversed ( sorted ( _ret , cmp = lambda a_vrs , b_vrs : version_cmp ( a_vrs [ '<STR_LIT>' ] , b_vrs [ '<STR_LIT>' ] ) ) ) : <EOL> pkg_name = pkg_data . pop ( '<STR_LIT:name>' ) <EOL> if pkg_name not in ret : <EOL> ret [ pkg_name ] = pkg_data . copy ( ) <EOL> del ret [ pkg_name ] [ '<STR_LIT>' ] <EOL> return ret <EOL> def version_cmp ( ver1 , ver2 ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> if HAS_RPM : <EOL> cmp_func = rpm . labelCompare <EOL> elif HAS_RPMUTILS : <EOL> cmp_func = rpmUtils . miscutils . compareEVR <EOL> else : <EOL> cmp_func = None <EOL> cmp_result = cmp_func is None and <NUM_LIT:2> or cmp_func ( salt . utils . str_version_to_evr ( ver1 ) , <EOL> salt . utils . str_version_to_evr ( ver2 ) ) <EOL> if cmp_result not in ( - <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ) : <EOL> raise Exception ( "<STR_LIT>" . format ( cmp_result ) ) <EOL> return cmp_result <EOL> except Exception as exc : <EOL> log . warning ( "<STR_LIT>" . format ( ver1 , ver2 , exc ) ) <EOL> return salt . utils . version_cmp ( ver1 , ver2 ) </s>
