<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import logging <EOL> from salt . ext . six . moves import range <EOL> try : <EOL> import win32com . client <EOL> import pythoncom <EOL> HAS_DEPENDENCIES = True <EOL> except ImportError : <EOL> HAS_DEPENDENCIES = False <EOL> import salt . utils <EOL> log = logging . getLogger ( __name__ ) <EOL> def __virtual__ ( ) : <EOL> '''<STR_LIT>''' <EOL> if salt . utils . is_windows ( ) and HAS_DEPENDENCIES : <EOL> return True <EOL> return False <EOL> def _gather_update_categories ( updateCollection ) : <EOL> '''<STR_LIT>''' <EOL> categories = [ ] <EOL> for i in range ( updateCollection . Count ) : <EOL> update = updateCollection . Item ( i ) <EOL> for j in range ( update . Categories . Count ) : <EOL> name = update . Categories . Item ( j ) . Name <EOL> if name not in categories : <EOL> log . debug ( '<STR_LIT>' . format ( name ) ) <EOL> categories . append ( name ) <EOL> return categories <EOL> class PyWinUpdater ( object ) : <EOL> def __init__ ( self , categories = None , skipUI = True , skipDownloaded = False , <EOL> skipInstalled = True , skipReboot = False , skipPresent = False , <EOL> skipSoftwareUpdates = False , skipDriverUpdates = False , skipHidden = True ) : <EOL> log . debug ( '<STR_LIT>' ) <EOL> pythoncom . CoInitialize ( ) <EOL> self . skipUI = skipUI <EOL> self . skipDownloaded = skipDownloaded <EOL> self . skipInstalled = skipInstalled <EOL> self . skipReboot = skipReboot <EOL> self . skipPresent = skipPresent <EOL> self . skipHidden = skipHidden <EOL> self . skipSoftwareUpdates = skipSoftwareUpdates <EOL> self . skipDriverUpdates = skipDriverUpdates <EOL> self . categories = categories <EOL> self . foundCategories = None <EOL> log . debug ( '<STR_LIT>' ) <EOL> self . update_session = win32com . client . Dispatch ( '<STR_LIT>' ) <EOL> log . debug ( '<STR_LIT>' ) <EOL> self . win_searcher = self . update_session . CreateUpdateSearcher ( ) <EOL> self . download_collection = win32com . client . Dispatch ( '<STR_LIT>' ) <EOL> self . install_collection = win32com . client . Dispatch ( '<STR_LIT>' ) <EOL> self . win_downloader = self . update_session . CreateUpdateDownloader ( ) <EOL> self . win_downloader . Updates = self . download_collection <EOL> self . win_installer = self . update_session . CreateUpdateInstaller ( ) <EOL> self . win_installer . Updates = self . install_collection <EOL> self . download_results = None <EOL> self . install_results = None <EOL> def Search ( self , searchString ) : <EOL> try : <EOL> log . debug ( '<STR_LIT>' . format ( searchString ) ) <EOL> self . search_results = self . win_searcher . Search ( searchString ) <EOL> log . debug ( '<STR_LIT>' ) <EOL> except Exception as exc : <EOL> log . info ( '<STR_LIT>' . format ( exc ) ) <EOL> return exc <EOL> log . debug ( '<STR_LIT>' . format ( <EOL> self . search_results . Updates . Count ) ) <EOL> try : <EOL> for update in self . search_results . Updates : <EOL> if update . InstallationBehavior . CanRequestUserInput : <EOL> log . debug ( u'<STR_LIT>' . format ( update . title ) ) <EOL> continue <EOL> for category in update . Categories : <EOL> if self . skipDownloaded and update . IsDownloaded : <EOL> continue <EOL> if self . categories is None or category . Name in self . categories : <EOL> self . download_collection . Add ( update ) <EOL> log . debug ( u'<STR_LIT>' . format ( update . title ) ) <EOL> self . foundCategories = _gather_update_categories ( self . download_collection ) <EOL> return True <EOL> except Exception as exc : <EOL> log . info ( '<STR_LIT>' . format ( exc ) ) <EOL> return exc <EOL> def AutoSearch ( self ) : <EOL> search_string = '<STR_LIT>' <EOL> searchParams = [ ] <EOL> if self . skipInstalled : <EOL> searchParams . append ( '<STR_LIT>' ) <EOL> else : <EOL> searchParams . append ( '<STR_LIT>' ) <EOL> if self . skipHidden : <EOL> searchParams . append ( '<STR_LIT>' ) <EOL> else : <EOL> searchParams . append ( '<STR_LIT>' ) <EOL> if self . skipReboot : <EOL> searchParams . append ( '<STR_LIT>' ) <EOL> else : <EOL> searchParams . append ( '<STR_LIT>' ) <EOL> if self . skipPresent : <EOL> searchParams . append ( '<STR_LIT>' ) <EOL> else : <EOL> searchParams . append ( '<STR_LIT>' ) <EOL> if len ( searchParams ) > <NUM_LIT:1> : <EOL> for i in searchParams : <EOL> search_string += '<STR_LIT>' . format ( i ) <EOL> else : <EOL> search_string += '<STR_LIT>' . format ( searchParams [ <NUM_LIT:1> ] ) <EOL> if not self . skipSoftwareUpdates and not self . skipDriverUpdates : <EOL> search_string += '<STR_LIT>' <EOL> elif not self . skipSoftwareUpdates : <EOL> search_string += '<STR_LIT>' <EOL> elif not self . skipDriverUpdates : <EOL> search_string += '<STR_LIT>' <EOL> else : <EOL> return False <EOL> log . debug ( '<STR_LIT>' . format ( search_string ) ) <EOL> return self . Search ( search_string ) <EOL> def Download ( self ) : <EOL> try : <EOL> if self . download_collection . Count != <NUM_LIT:0> : <EOL> self . download_results = self . win_downloader . Download ( ) <EOL> else : <EOL> log . debug ( '<STR_LIT>' ) <EOL> return True <EOL> except Exception as exc : <EOL> log . debug ( '<STR_LIT>' . format ( exc ) ) <EOL> return exc <EOL> def Install ( self ) : <EOL> try : <EOL> for update in self . search_results . Updates : <EOL> if update . IsDownloaded : <EOL> self . install_collection . Add ( update ) <EOL> log . debug ( '<STR_LIT>' ) <EOL> except Exception as exc : <EOL> log . info ( '<STR_LIT>' . format ( exc ) ) <EOL> return exc <EOL> try : <EOL> for update in self . search_results . Updates : <EOL> if not update . EulaAccepted : <EOL> log . debug ( u'<STR_LIT>' . format ( update . Title ) ) <EOL> update . AcceptEula ( ) <EOL> except Exception as exc : <EOL> log . info ( '<STR_LIT>' . format ( exc ) ) <EOL> return exc <EOL> if self . install_collection . Count != <NUM_LIT:0> : <EOL> log . debug ( '<STR_LIT>' ) <EOL> updates = [ ] <EOL> try : <EOL> self . install_results = self . win_installer . Install ( ) <EOL> log . info ( '<STR_LIT>' ) <EOL> return True <EOL> except Exception as exc : <EOL> log . info ( '<STR_LIT>' . format ( exc ) ) <EOL> return exc <EOL> else : <EOL> log . info ( '<STR_LIT>' ) <EOL> return True <EOL> def GetInstallationResults ( self ) : <EOL> log . debug ( '<STR_LIT>' . format ( self . install_collection . Count ) ) <EOL> updates = [ ] <EOL> if self . install_collection . Count == <NUM_LIT:0> : <EOL> return { } <EOL> for i in range ( self . install_collection . Count ) : <EOL> updates . append ( '<STR_LIT>' . format ( <EOL> self . install_results . GetUpdateResult ( i ) . ResultCode , <EOL> self . install_collection . Item ( i ) . Title ) ) <EOL> log . debug ( '<STR_LIT>' ) <EOL> results = { } <EOL> for i , update in enumerate ( updates ) : <EOL> results [ '<STR_LIT>' . format ( i ) ] = update <EOL> log . debug ( '<STR_LIT>' ) <EOL> return results <EOL> def GetDownloadResults ( self ) : <EOL> updates = [ ] <EOL> for i in range ( self . download_collection . Count ) : <EOL> updates . append ( '<STR_LIT>' . format ( <EOL> self . download_results . GetUpdateResult ( i ) . ResultCode , <EOL> self . download_collection . Item ( i ) . Title ) ) <EOL> results = { } <EOL> for i , update in enumerate ( updates ) : <EOL> results [ '<STR_LIT>' . format ( i ) ] = update <EOL> return results <EOL> def SetCategories ( self , categories ) : <EOL> self . categories = categories <EOL> def GetCategories ( self ) : <EOL> return self . categories <EOL> def GetAvailableCategories ( self ) : <EOL> return self . foundCategories <EOL> def SetSkips ( self , skips ) : <EOL> if skips : <EOL> for i in skips : <EOL> value = i [ next ( i . iterkeys ( ) ) ] <EOL> skip = next ( i . iterkeys ( ) ) <EOL> self . SetSkip ( skip , value ) <EOL> log . debug ( '<STR_LIT>' . format ( skip , value ) ) <EOL> def SetSkip ( self , skip , state ) : <EOL> if skip == '<STR_LIT>' : <EOL> self . skipUI = state <EOL> elif skip == '<STR_LIT>' : <EOL> self . skipDownloaded = state <EOL> elif skip == '<STR_LIT>' : <EOL> self . skipInstalled = state <EOL> elif skip == '<STR_LIT>' : <EOL> self . skipReboot = state <EOL> elif skip == '<STR_LIT>' : <EOL> self . skipPresent = state <EOL> elif skip == '<STR_LIT>' : <EOL> self . skipHidden = state <EOL> elif skip == '<STR_LIT>' : <EOL> self . skipSoftwareUpdates = state <EOL> elif skip == '<STR_LIT>' : <EOL> self . skipDriverUpdates = state <EOL> log . debug ( '<STR_LIT>' . format ( <EOL> self . skipUI , self . skipDownloaded , self . skipInstalled , self . skipReboot , <EOL> self . skipPresent , self . skipHidden , self . skipSoftwareUpdates , self . skipDriverUpdates ) ) <EOL> def _search ( win_updater , retries = <NUM_LIT:5> ) : <EOL> passed = False <EOL> clean = True <EOL> comment = '<STR_LIT>' <EOL> while not passed : <EOL> log . debug ( '<STR_LIT>' . format ( retries ) ) <EOL> passed = win_updater . AutoSearch ( ) <EOL> log . debug ( '<STR_LIT>' . format ( passed ) ) <EOL> if isinstance ( passed , Exception ) : <EOL> clean = False <EOL> comment += '<STR_LIT>' . format ( passed ) <EOL> retries -= <NUM_LIT:1> <EOL> if retries : <EOL> comment += '<STR_LIT>' . format ( retries ) <EOL> passed = False <EOL> else : <EOL> comment += '<STR_LIT>' <EOL> return ( comment , True , retries ) <EOL> passed = False <EOL> if clean : <EOL> comment += '<STR_LIT>' <EOL> return ( comment , True , retries ) <EOL> def _download ( win_updater , retries = <NUM_LIT:5> ) : <EOL> passed = False <EOL> clean = True <EOL> comment = '<STR_LIT>' <EOL> while not passed : <EOL> log . debug ( '<STR_LIT>' . format ( retries ) ) <EOL> passed = win_updater . Download ( ) <EOL> log . debug ( '<STR_LIT>' . format ( passed ) ) <EOL> if isinstance ( passed , Exception ) : <EOL> clean = False <EOL> comment += '<STR_LIT>' . format ( passed ) <EOL> retries -= <NUM_LIT:1> <EOL> if retries : <EOL> comment += '<STR_LIT>' . format ( retries ) <EOL> passed = False <EOL> else : <EOL> comment += '<STR_LIT>' <EOL> return ( comment , False , retries ) <EOL> if clean : <EOL> comment += '<STR_LIT>' <EOL> return ( comment , True , retries ) <EOL> def _install ( win_updater , retries = <NUM_LIT:5> ) : <EOL> passed = False <EOL> clean = True <EOL> comment = '<STR_LIT>' <EOL> while not passed : <EOL> log . debug ( '<STR_LIT>' . format ( win_updater . install_collection . Count ) ) <EOL> log . debug ( '<STR_LIT>' . format ( retries ) ) <EOL> passed = win_updater . Install ( ) <EOL> log . info ( '<STR_LIT>' . format ( passed ) ) <EOL> if isinstance ( passed , Exception ) : <EOL> clean = False <EOL> comment += '<STR_LIT>' . format ( passed ) <EOL> retries -= <NUM_LIT:1> <EOL> if retries : <EOL> comment += '<STR_LIT>' . format ( retries ) <EOL> passed = False <EOL> else : <EOL> comment += '<STR_LIT>' <EOL> return ( comment , False , retries ) <EOL> if clean : <EOL> comment += '<STR_LIT>' <EOL> return ( comment , True , retries ) <EOL> def installed ( name , categories = None , skips = None , retries = <NUM_LIT:10> ) : <EOL> '''<STR_LIT>''' <EOL> ret = { '<STR_LIT:name>' : name , <EOL> '<STR_LIT:result>' : True , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> if not categories : <EOL> categories = [ name ] <EOL> log . debug ( '<STR_LIT>' . format ( categories ) ) <EOL> win_updater = PyWinUpdater ( ) <EOL> win_updater . SetCategories ( categories ) <EOL> win_updater . SetSkips ( skips ) <EOL> comment , passed , retries = _search ( win_updater , retries ) <EOL> ret [ '<STR_LIT>' ] += comment <EOL> if not passed : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> return ret <EOL> comment , passed , retries = _download ( win_updater , retries ) <EOL> ret [ '<STR_LIT>' ] += comment <EOL> if not passed : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> return ret <EOL> comment , passed , retries = _install ( win_updater , retries ) <EOL> ret [ '<STR_LIT>' ] += comment <EOL> if not passed : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> return ret <EOL> try : <EOL> ret [ '<STR_LIT>' ] = win_updater . GetInstallationResults ( ) <EOL> except Exception : <EOL> ret [ '<STR_LIT>' ] += '<STR_LIT>' <EOL> return ret <EOL> def downloaded ( name , categories = None , skips = None , retries = <NUM_LIT:10> ) : <EOL> '''<STR_LIT>''' <EOL> ret = { '<STR_LIT:name>' : name , <EOL> '<STR_LIT:result>' : True , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> if not categories : <EOL> categories = [ name ] <EOL> log . debug ( '<STR_LIT>' . format ( categories ) ) <EOL> win_updater = PyWinUpdater ( ) <EOL> win_updater . SetCategories ( categories ) <EOL> win_updater . SetSkips ( skips ) <EOL> comment , passed , retries = _search ( win_updater , retries ) <EOL> ret [ '<STR_LIT>' ] += comment <EOL> if not passed : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> return ret <EOL> comment , passed , retries = _download ( win_updater , retries ) <EOL> ret [ '<STR_LIT>' ] += comment <EOL> if not passed : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> return ret <EOL> try : <EOL> ret [ '<STR_LIT>' ] = win_updater . GetDownloadResults ( ) <EOL> except Exception : <EOL> ret [ '<STR_LIT>' ] += '<STR_LIT>' <EOL> return ret </s>
