Modified skip flag logic to skip instead of filter.

Defaulted skipPresent to false, since it causes legitimate patches to be missed.
Fixed output of install_updates.
Fixed misc. code problems.
This commit is contained in:
Cott Lang 2014-05-23 08:47:53 -06:00
parent 16596d7d2f
commit 270d693d5b

View File

@ -57,8 +57,8 @@ def _gather_update_categories(updateCollection):
class PyWinUpdater(object):
def __init__(self, categories=None, skipUI=True, skipDownloaded=True,
skipInstalled=True, skipReboot=False, skipPresent=True,
def __init__(self, categories=None, skipUI=True, skipDownloaded=False,
skipInstalled=True, skipReboot=False, skipPresent=False,
softwareUpdates=True, driverUpdates=False, skipHidden=True):
log.debug('CoInitializing the pycom system')
pythoncom.CoInitialize()
@ -106,6 +106,9 @@ class PyWinUpdater(object):
#the results of the installation process
self.install_results = None
#search results from CreateUpdateSearcher()
self.search_results = None
def Search(self, searchString):
try:
log.debug('beginning search of the passed string: {0}'.format(searchString))
@ -124,17 +127,18 @@ class PyWinUpdater(object):
for update in self.search_results.Updates:
#this skipps an update if UI updates are not desired.
if update.InstallationBehavior.CanRequestUserInput:
log.debug('Skipped update {0}'.format(str(update)))
log.debug('Skipped update {0} - requests user input'.format(str(update)))
continue
#if this update is already downloaded, it doesn't need to be in
# the download_collection. so skipping it unless the user mandates redownload.
# the download_collection. so skipping it unless the user mandates re-download.
if self.skipDownloaded and update.IsDownloaded:
log.debug('Skipped update {0} - already downloaded'.format(str(update)))
continue
#check this update's categories aginst the ones desired.
#check this update's categories against the ones desired.
for category in update.Categories:
#this is a zero gaurd. these tests have to be in this order
#this is a zero guard. these tests have to be in this order
# or it will error out when the user tries to search for
# updates with out specifying categories.
if self.categories is None or category.Name in self.categories:
@ -164,29 +168,18 @@ class PyWinUpdater(object):
if self.skipInstalled:
searchParams.append('IsInstalled=0')
else:
searchParams.append('IsInstalled=1')
if self.skipHidden:
searchParams.append('IsHidden=0')
else:
searchParams.append('IsHidden=1')
if self.skipReboot:
searchParams.append('RebootRequired=1')
else:
searchParams.append('RebootRequired=0')
if self.skipPresent:
searchParams.append('IsPresent=0')
else:
searchParams.append('IsPresent=1')
if len(searchParams) > 1:
for i in searchParams:
search_string += '{0} and '.format(i)
else:
search_string += '{0} and '.format(searchParams[1])
for i in searchParams:
search_string += '{0} and '.format(i)
if self.softwareUpdates and self.driverUpdates:
search_string += 'Type=\'Software\' or Type=\'Driver\''
@ -230,7 +223,6 @@ class PyWinUpdater(object):
#if the blugger is empty. no point it starting the install process.
if self.install_collection.Count != 0:
log.debug('Install list created, about to install')
updates = []
try:
#the call to install.
self.install_results = self.win_installer.Install()
@ -248,12 +240,12 @@ class PyWinUpdater(object):
this gets results of installation process.
'''
#if the blugger is empty, the results are nil.
log.debug('bluger has {0} updates in it'.format(str(self.install_collection.Count)))
log.debug('blugger has {0} updates in it'.format(str(self.install_collection.Count)))
if self.install_collection.Count == 0:
return {}
updates = []
log.debug('reparing update list')
log.debug('repairing update list')
for i in range(self.install_collection.Count):
#this gets the result from install_results, but the title comes from the update
#collection install_collection.
@ -278,7 +270,7 @@ class PyWinUpdater(object):
updates = self.GetInstallationResults()
ret = 'The following are the updates and their return codes.\n'
for i in updates.keys():
ret += '\t{0} : {1}\n'.format(str(updates[i].ResultCode), str(updates[i].Title))
ret += '\t{0}\n'.format(updates[i])
return ret
def GetDownloadResults(self):
@ -349,7 +341,6 @@ class PyWinUpdater(object):
self.skipPresent, self.softwareUpdates, self.driverUpdates))
def __str__(self):
updates = []
results = 'There are {0} updates, by category there are:\n'.format(
str(self.download_collection.count))
for category in self.foundCategories:
@ -381,7 +372,6 @@ def _search(quidditch, retries=5):
retries -= 1
if retries:
comment += '{0} tries to go. retrying\n'.format(str(retries))
passed = False
else:
comment += 'out of retries. this update round failed.\n'
return (comment, True, retries)
@ -460,7 +450,7 @@ def list_updates(verbose=False, includes=None, retries=5, categories=None):
adding: retries=n
various aspects of the updates can be included or excluded. this feature is
still indevelopment.
still in development.
You can also specify by category of update similarly to how you do includes:
categories=['Windows 7', 'Security Updates']
@ -527,7 +517,7 @@ def download_updates(includes=None, retries=5, categories=None):
'''
log.debug('categories to search for are: '.format(str(categories)))
quidditch = PyWinUpdater()
quidditch = PyWinUpdater(skipDownloaded=True)
quidditch.SetCategories(categories)
quidditch.SetIncludes(includes)
@ -544,7 +534,7 @@ def download_updates(includes=None, retries=5, categories=None):
try:
comment = quidditch.GetDownloadResults()
except Exception as e:
comment = 'could not get results, but updates were installed.'
comment = 'could not get results, but updates were installed. {0}'.format(str(e))
return 'Windows is up to date. \n{0}'.format(comment)
@ -558,7 +548,7 @@ def install_updates(cached=None, includes=None, retries=5, categories=None):
adding: retries=n
various aspects of the updates can be included or excluded. this feature is
still indevelopment.
still in development.
You can also specify by category of update similarly to how you do includes:
categories=['Windows 7', 'Security Updates']
@ -602,5 +592,5 @@ def install_updates(cached=None, includes=None, retries=5, categories=None):
try:
comment = quidditch.GetInstallationResultsPretty()
except Exception as e:
comment = 'could not get results, but updates were installed.'
comment = 'Could not get results, but updates were installed. {0}'.format(str(e))
return 'Windows is up to date. \n{0}'.format(comment)