import os import ssl import urllib2 import ctypes from subprocess import PIPE, Popen URL = "https://cdn-patchportal-one.comodo.com/portal/packages/spm/TreeSize%20Free/x64/TreeSizeFreeSetup_4.8.1.exe" class disable_file_system_redirection: _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection def __enter__(self): self.old_value = ctypes.c_long() self.success = self._disable(ctypes.byref(self.old_value)) def __exit__(self, type, value, traceback): if self.success: self._revert(self.old_value) def ecmd(command): with disable_file_system_redirection(): obj = Popen(command, shell=True, stdout=PIPE, stderr=PIPE) out, err = obj.communicate() return obj.returncode def download_file(url, dest): headers = { 'User-Agent':'Mozilla/5.0' } context = ssl._create_unverified_context() req = urllib2.Request(url, headers=headers) response = urllib2.urlopen(req, context=context) with open(dest, 'wb') as f: while True: chunk = response.read(1024 * 1024) if not chunk: break f.write(chunk) def spm_install(dir, args): installer = os.path.join(os.environ['TEMP'], 'TreeSizeFreeSetup_4.8.1.exe') download_file(URL, installer) cmd = '"%s" /VERYSILENT /SUPPRESSMSGBOXES /NORESTART /ALLUSERS' % installer rc = ecmd(cmd) try: os.remove(installer) except: pass if rc == 0: print "Installation successful" else: raise Exception("Installation failed. Exit Code: %s" % rc) def spm_uninstall(dir, args): uninstall = r'"C:\Program Files\JAM Software\TreeSize Free\unins000.exe" /VERYSILENT /SUPPRESSMSGBOXES /NORESTART' rc = ecmd(uninstall) if rc == 0: print "Uninstallation successful" else: raise Exception("Uninstallation failed. Exit Code: %s" % rc) def spm_update(dir, args): spm_uninstall(None, None) spm_install(None, None)