import os from subprocess import PIPE, Popen import ctypes import ssl import urllib2 import datetime import time import shutil import re guid = r'{fca95908-8c70-405d-9e72-cd746e2f7786}' 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) with disable_file_system_redirection(): arch = os.popen("wmic os get OSArchitecture").read() userout = os.popen('query user').read() username = re.sub('^ ', '', re.findall("(.*)Active", userout)[0].split(' ')[0].replace('>', '')) # print username sid = os.popen("wmic useraccount where name=\"%s\" get sid" % username).read().splitlines()[1].strip() # print sid if '64' in arch: url = "https://www.python.org/ftp/python/3.11.1/python-3.11.1-amd64.exe" Bit = "64" else: url = "https://www.python.org/ftp/python/3.11.1/python-3.11.1.exe" Bit = "32" Down_path = os.environ['PROGRAMDATA'] fileName = url.split('/')[-1] DownTo = os.path.join(Down_path, fileName) def ecmd(command): from subprocess import Popen, PIPE import ctypes with disable_file_system_redirection(): obj = Popen(command, shell=True, stdout=PIPE, stderr=PIPE) out, err = obj.communicate() ret = obj.returncode if ret == 0: if out: return out.strip() else: return ret else: if err: return err.strip() else: return ret def downloadFile(DownTo, URL): request = urllib2.Request(URL, headers={'User-Agent': "Magic Browser"}) try: gcontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1) parsed = urllib2.urlopen(request, context=gcontext) except: parsed = urllib2.urlopen(request) try: with open(DownTo, 'wb') as f: while True: chunk = parsed.read(100 * 1000 * 1000) if chunk: f.write(chunk) else: break if os.path.isfile(DownTo): return '{} - {}KB'.format(DownTo, os.path.getsize(DownTo) / 1024) except: return 'Please Check URL or Download Path!' regname = "python.reg" reg_path = os.path.join(os.environ['TEMP'], regname) regfile = """Windows Registry Editor Version 5.00 [HKEY_USERS\\%s\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\{fca95908-8c70-405d-9e72-cd746e2f7786}] "BundleCachePath"=""" % ( sid) + r'"C:\\Users\\' + username + r'\\AppData\\Local\\Package Cache\\{fca95908-8c70-405d-9e72-cd746e2f7786}\\' + fileName + r'"' + r""" "BundleUpgradeCode"=hex(7):7b,00,37,00,36,00,37,00,42,00,33,00,31,00,45,00,42,\ 00,2d,00,30,00,31,00,30,00,36,00,2d,00,35,00,41,00,45,00,33,00,2d,00,42,00,\ 34,00,36,00,45,00,2d,00,37,00,37,00,46,00,33,00,32,00,44,00,31,00,42,00,41,\ 00,42,00,41,00,32,00,7d,00,00,00,00,00 "BundleAddonCode"=hex(7): "BundleDetectCode"=hex(7): "BundlePatchCode"=hex(7): "BundleVersion"="3.11.1150.0" "VersionMajor"=dword:00000003 "VersionMinor"=dword:0000000b "BundleProviderKey"="CPython-3.11" "BundleTag"="" "EngineVersion"="3.14.0.5722" "DisplayIcon"=""" + r'"C:\\Users\\' + username + r'\\AppData\\Local\\Package Cache\\{fca95908-8c70-405d-9e72-cd746e2f7786}\\' + fileName + r',0"' + r""" "DisplayName"="Python 3.11.1 (""" + Bit + r"""-bit)" "DisplayVersion"="3.11.1150.0" "Publisher"="Python Software Foundation" "URLInfoAbout"="https://www.python.org/" "ModifyPath"=""" + r'"\"C:\\Users\\' + username + r'\\AppData\\Local\\Package Cache\\{fca95908-8c70-405d-9e72-cd746e2f7786}\\' + fileName + r'\" /modify"' + r""" "NoElevateOnModify"=dword:00000001 "QuietUninstallString"=""" + r'"\"C:\\Users\\' + username + r'\\AppData\\Local\\Package Cache\\{fca95908-8c70-405d-9e72-cd746e2f7786}\\' + fileName + r'\" /uninstall /quiet"' + r""" "UninstallString"=""" + r'"\"C:\\Users\\' + username + r'\\AppData\\Local\\Package Cache\\{fca95908-8c70-405d-9e72-cd746e2f7786}\\' + fileName + r'\" /uninstall"' + r""" "EstimatedSize"=dword:000242e0 "Resume"=dword:00000003 "Installed"=dword:00000001 """ pathChanging = """ mkdir "C:\Users\%s\AppData\Local\Package Cache\%s" copy "%s" "C:\Users\%s\AppData\Local\Package Cache\%s" reg delete "HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Uninstall\%s" /f reg import %s """ % (username, guid, DownTo, username, guid, guid, reg_path) def spm_install(dir, args): with disable_file_system_redirection(): os.popen('RD /S /Q %s' % (Down_path)) print(downloadFile(DownTo, url)) print(ecmd('"%s" /quiet InstallAllUsers=1 PrependPath=1 Include_launcher=0' % (DownTo))) batch_name = 'batch_file.bat' batch_path = os.path.join(os.environ['PROGRAMDATA'], batch_name) with open(batch_path, 'wb') as wr: wr.write(pathChanging) wr.close() with open(reg_path, 'wb') as wr: wr.write(regfile) wr.close() print(ecmd(batch_path)) os.remove(DownTo) os.remove(reg_path) os.remove(batch_path) def spm_uninstall(dir, args): with disable_file_system_redirection(): if os.path.exists("C:\\Users\\%s\\AppData\\Local\\Package Cache" % username): for root, dirs, files in os.walk("C:\\Users\\%s\\AppData\\Local\\Package Cache" % username): flag = False if files: for i in files: if 'python' in i: print(ecmd('"%s" /uninstall /quiet' % (os.path.join(root, i)))) pythonid = root.split("\\")[-1] os.popen('reg delete "HKEY_USERS\%s\SOFTWARE\Microsoft\Windows\CurrentVersion' '\Uninstall\\%s" /f' % (sid, pythonid)) os.popen('RD /S /Q "%s"' % root) os.popen('RD /S /Q %s' % Down_path) flag = True break if flag: break def spm_update(dir, args): with disable_file_system_redirection(): spm_uninstall(None, None) time.sleep(20) spm_install(None, None)