Advertisement
veblush

WinstView

Nov 3rd, 2012
1,140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.45 KB | None | 0 0
  1. #/usr/bin/python2
  2.  
  3. import win32api
  4. import win32con
  5. import os
  6. import glob
  7.  
  8. def GetProductNameByProductGUID(gstr):
  9.     rpath = r"SOFTWARE\Classes\Installer\Products\%s" % gstr
  10.     try:
  11.         hkey = win32api.RegOpenKeyEx(win32con.HKEY_LOCAL_MACHINE, rpath, 0, win32con.KEY_READ)
  12.         name = win32api.RegQueryValueEx(hkey, "ProductName")
  13.         win32api.RegCloseKey(hkey)
  14.         if name is not None:
  15.             return name[0]
  16.     except:
  17.         print "!"
  18.         pass
  19.  
  20. def GetProductNameByPatchGUID(gstr):
  21.     # 0x100 은 Win64 에서 Win32 프로세스가 Win64 Registry 에 접근하기 위한 것
  22.     rhkey = win32api.RegOpenKeyEx(win32con.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products", 0, win32con.KEY_READ | 0x100)
  23.     subkeys = win32api.RegEnumKeyEx(rhkey)
  24.     for key in subkeys:
  25.         try:
  26.             hkey = win32api.RegOpenKeyEx(rhkey, "%s\\Patches" % key[0], 0, win32con.KEY_READ | 0x100)
  27.             subkeys2 = win32api.RegEnumKeyEx(hkey)
  28.             for subkey in subkeys2:
  29.                 if (subkey[0] == gstr):
  30.                     hkey2 = win32api.RegOpenKeyEx(hkey, subkey[0], 0, win32con.KEY_READ | 0x100)
  31.                     name = win32api.RegQueryValueEx(hkey2, "DisplayName")
  32.                     hkey3 = win32api.RegOpenKeyEx(rhkey, "%s\\InstallProperties" % key[0], 0, win32con.KEY_READ | 0x100)
  33.                     pname = win32api.RegQueryValueEx(hkey3, "DisplayName")
  34.                     return "[%s]%s" % (pname[0], name[0])
  35.             win32api.RegCloseKey(hkey)
  36.         except:
  37.             continue
  38.  
  39. def GetProductNameByMsiFile(file):
  40.     rhkey = win32api.RegOpenKeyEx(win32con.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products", 0, win32con.KEY_READ | 0x100)
  41.     subkeys = win32api.RegEnumKeyEx(rhkey)
  42.     for key in subkeys:
  43.         try:
  44.             hkey = win32api.RegOpenKeyEx(rhkey, "%s\\InstallProperties" % key[0], 0, win32con.KEY_READ | 0x100)
  45.             package = win32api.RegQueryValueEx(hkey, "LocalPackage")
  46.             if package is not None and package[0].lower() == file.lower():
  47.                 name = win32api.RegQueryValueEx(hkey, "DisplayName")
  48.                 if name is not None:
  49.                     win32api.RegCloseKey(hkey)
  50.                     return name[0]
  51.             win32api.RegCloseKey(hkey)
  52.         except:
  53.             continue
  54.  
  55. def GetProductNameByMspFile(file):
  56.     rhkey = win32api.RegOpenKeyEx(win32con.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Patches", 0, win32con.KEY_READ | 0x100)
  57.     subkeys = win32api.RegEnumKeyEx(rhkey)
  58.     for key in subkeys:
  59.         try:
  60.             hkey = win32api.RegOpenKeyEx(rhkey, "%s" % key[0], 0, win32con.KEY_READ | 0x100)
  61.             package = win32api.RegQueryValueEx(hkey, "LocalPackage")
  62.             if package is not None and package[0].lower() == file.lower():
  63.                 return GetProductNameByPatchGUID(key[0])
  64.             win32api.RegCloseKey(hkey)
  65.         except:
  66.             continue
  67.  
  68. def GetDirectorySize(path):
  69.     total = 0
  70.     for root, dirs, files in os.walk(path):
  71.         for file in files:
  72.             try:
  73.                 total += os.path.getsize(os.path.join(root, file))
  74.             except:
  75.                 pass
  76.     return total
  77.  
  78. def ViewInstallerFile():
  79.     print "file\tname\tsize"
  80.     rootDir = r"C:\Windows\Installer"
  81.     for path in glob.glob(os.path.join(rootDir, "*")):
  82.         if os.path.isfile(path):
  83.             file = os.path.split(path)[1]
  84.             ext = os.path.splitext(file)[1].lower()
  85.             if ext == ".msi":
  86.                 name = GetProductNameByMsiFile(path)
  87.                 size = os.path.getsize(path)
  88.                 print "%s\t%s\t%d" % (file, name, size)
  89.             if ext == ".msp":
  90.                 name = GetProductNameByMspFile(path)
  91.                 size = os.path.getsize(path)
  92.                 print "%s\t%s\t%d" % (file, name, size)
  93.  
  94. def ViewPatchCache():
  95.     print "dir\tname\tsize"
  96.     rootDir = r"C:\Windows\Installer\$PatchCache$\Managed"
  97.     for dir in os.listdir(rootDir):
  98.         name = GetProductNameByProductGUID(dir)
  99.         size = GetDirectorySize(os.path.join(rootDir, dir))
  100.         print "%s\t%s\t%d" % (dir, name, size)
  101.  
  102. def ViewAll():
  103.     print "***** Installer *****"
  104.     ViewInstallerFile()
  105.     print
  106.     print "***** Installer\$PatchCache$\Managed *****"
  107.     ViewPatchCache()
  108.     print
  109.  
  110. ViewAll()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement