def main(): """ Main function for the script """ elevateAdminRights() print "Reading all OEM drivers...", drivers = getAllDrivers() print "done" # Let's find possible duplicates. The tuple of driver class (e.g. Keyboard, Display, etc.), # driver provider (Microsoft, nVidia, etc.) and signed information (MS Compatibility, etc.) # is considered to be the key defining a driver for the device. All drivers that have this # key being the same are considered to be the instances of the same driver, thus we sort # them by version and date and mark all older ones as duplicates of the most recent driver. duplicates = collections.defaultdict(list) for driver in drivers.itervalues(): duplicates[(driver.driverClass, driver.provider, driver.signedBy)].append(driver) oemDups = {} for key, driversList in duplicates.items(): if len(driversList) <= 1: del duplicates[key] else: driversList.sort( cmp=lambda d1, d2: cmp(d1.driverVersion, d2.driverVersion) or cmp(d1.driverDate, d2.driverDate), reverse=True, ) for dupDriver in driversList[1:]: oemDups[dupDriver.name] = driversList[0].name # Now we read all %SystemRoot%\inf\oem*.inf files to make a map that will allow us by # estimating the size of drivers stored in DriverStore to find out which oem drivers are # the largest and what we should remove. print "Reading oem*.inf files...", infFiles = os.path.join(os.getenv("SystemRoot"), "inf", "oem*.inf") oemFiles = {} for infName in glob.glob(infFiles): try: with open(infName, "rb") as f: content = f.read() except IOError, err: print 'Warning! Cannot read "%s" file: %s' % (infName, err) continue infName = os.path.basename(infName) try: oemFiles[content] except KeyError: oemFiles[content] = infName else: # There're two or more exact copies of .inf file with different names, that's really # strange. My guess here was that something is wrong with Windows installation, # so I used to stop script execution, but for now I've decided to ignore such # drivers completely continue
def main(): ''' Main function for the script ''' elevateAdminRights() print 'Reading all OEM drivers...', drivers = getAllDrivers() print 'done' # Let's find possible duplicates. The tuple of driver class (e.g. Keyboard, Display, etc.), # driver provider (Microsoft, nVidia, etc.) and signed information (MS Compatibility, etc.) # is considered to be the key defining a driver for the device. All drivers that have this # key being the same are considered to be the instances of the same driver, thus we sort # them by version and date and mark all older ones as duplicates of the most recent driver. duplicates = collections.defaultdict(list) for driver in drivers.itervalues(): duplicates[(driver.driverClass, driver.provider, driver.signedBy)].append(driver) oemDups = {} for key, driversList in duplicates.items(): if len(driversList) <= 1: del duplicates[key] else: driversList.sort(cmp=lambda d1, d2: cmp(d1.driverVersion, d2.driverVersion) or \ cmp(d1.driverDate, d2.driverDate), reverse=True) for dupDriver in driversList[1:]: oemDups[dupDriver.name] = driversList[0].name # Now we read all %SystemRoot%\inf\oem*.inf files to make a map that will allow us by # estimating the size of drivers stored in DriverStore to find out which oem drivers are # the largest and what we should remove. print 'Reading oem*.inf files...', infFiles = os.path.join(os.getenv('SystemRoot'), 'inf', 'oem*.inf') oemFiles = {} for infName in glob.glob(infFiles): try: with open(infName, 'rb') as f: content = f.read() except IOError, err: print 'Warning! Cannot read "%s" file: %s' % (infName, err) continue infName = os.path.basename(infName) try: oemFiles[content] except KeyError: oemFiles[content] = infName else: # There're two or more exact copies of .inf file with different names, that's really # strange. My guess here was that something is wrong with Windows installation, # so I used to stop script execution, but for now I've decided to ignore such # drivers completely continue
def main(): elevateAdminRights() orphanCleanup('patches', 'msp', getAllPatches) orphanCleanup('installs', 'msi', getAllProducts)
# -*- coding:utf-8 -*- # Copyright (C) 2020 Timothy Lin. [email protected] # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """ git-repo's wrapper for Win32 for UAC elevation.""" import os import sys import subprocess import win32elevate if __name__ == '__main__': win32elevate.elevateAdminRights(reattachConsole=True) new_argv = subprocess.list2cmdline([sys.executable] + sys.argv[1:]) rc = os.system(new_argv) #print("********************"*50, file=sys.stderr) #input() #print("rc:%d" % rc) #sys.exit(rc)
def main(): ''' Main function for the script ''' elevateAdminRights() print 'Reading all OEM drivers...', drivers = getAllDrivers() print 'done' # Let's find possible duplicates. The tuple of driver class (e.g. Keyboard, Display, etc.), # driver provider (Microsoft, nVidia, etc.) and signed information (MS Compatibility, etc.) # is considered to be the key defining a driver for the device. All drivers that have this # key being the same are considered to be the instances of the same driver, thus we sort # them by version and date and mark all older ones as duplicates of the most recent driver. duplicates = collections.defaultdict(list) for driver in drivers.itervalues(): duplicates[(driver.driverClass, driver.provider, driver.signedBy)].append(driver) oemDups = {} for key, driversList in duplicates.items(): if len(driversList) <= 1: del duplicates[key] else: driversList.sort(cmp=lambda d1, d2: cmp(d1.driverVersion, d2.driverVersion) or \ cmp(d1.driverDate, d2.driverDate), reverse=True) for dupDriver in driversList[1:]: oemDups[dupDriver.name] = driversList[0].name # Now we read all %SystemRoot%\inf\oem*.inf files to make a map that will allow us by # estimating the size of drivers stored in DriverStore to find out which oem drivers are # the largest and what we should remove. print 'Reading oem*.inf files...', infFiles = os.path.join(os.getenv('SystemRoot'), 'inf', 'oem*.inf') oemFiles = {} for infName in glob.glob(infFiles): with open(infName, 'rb') as f: content = f.read() infName = os.path.basename(infName) try: dupFile = oemFiles[content] except KeyError: oemFiles[content] = infName else: # There're two exact copies of .inf file with different names, that's really # strange. Our guess here is that something is wrong with Windows installation, # so we stop our execution raise Exception('%s is duplicate of %s' % (infName, dupFile)) print 'done' # now parse %SystemRoot%\system32\DriverStore\FileRepository print 'Parsing DriverStore...', driverRepo = os.path.join(os.getenv('SystemRoot'), 'system32', 'DriverStore', 'FileRepository') driverSize = [] for driverDir in os.walk(driverRepo).next()[1]: # All folders should in here should have the same pattern - abc.inf_something where # abc.inf lies within and should match to some oem###.inf file read above if this driver # is OEM (not built in current Windows setup). try: infName = re.match(r'^(.*?\.inf)_.*$', driverDir).group(1) except ValueError: # this folder does not match desired pattern, ignore it continue with open(os.path.join(driverRepo, driverDir, infName), 'rb') as f: content = f.read() try: oemName = oemFiles[content] except KeyError: # this infName is not OEM, skipping continue driverSize.append((oemName, getFolderSize(os.path.join(driverRepo, driverDir)))) print 'done' print 'Drivers (sorted by size):' driverSize.sort(reverse=True, key=lambda (oemName, size): size) dups, dupSize = [], 0 for oemName, size in driverSize: if oemName in oemDups: dups.append((oemName, size)) dupSize += size print '%s: %s%s' % (drivers[oemName], MB(size), ' (probably superseded by %s)' % oemDups[oemName] if oemName in oemDups else '') if dups: answer = raw_input(('Possible obsolete drivers found (taking %s). Try to delete? ' + \ '[y(es)/n(o)] ') % (MB(dupSize))).lower() cleanedSize = 0 if answer in ('y', 'yes'): for dup, size in dups: if deleteDriver(dup): cleanedSize += size print 'Was able to clean up %s out of %s expected' % (MB(cleanedSize), MB(dupSize)) else: print 'Cancelled by user'
def main(): elevateAdminRights() orphanCleanup("patches", "msp", getAllPatches) orphanCleanup("installs", "msi", getAllProducts)