# give here the 64bit msi direct download link of the updated version of MSedge
url_64bit_msi=r"https://msedge.sf.dl.delivery.mp.microsoft.com/filestreamingservice/files/04953c72-9e0d-420a-950c-15e0e659a225/MicrosoftEdgeEnterpriseX64.msi"

# give here the 32bit msi direct download link of the updated version of MSedge
url_32bit_msi =r"https://msedge.sf.dl.delivery.mp.microsoft.com/filestreamingservice/files/6af62ba8-6b07-4436-9279-5257062cd345/MicrosoftEdgeEnterpriseX86.msi"

import os
from subprocess import PIPE, Popen
import ctypes
import ssl
import urllib2
import time

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)
    
    
Down_path=os.environ['TEMP']
fileName = "MSEdge.msi"
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, fromURL):
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'}
    context = ssl._create_unverified_context()
    request = urllib2.Request(fromURL, headers=headers)
    req = urllib2.urlopen(request,context=context)
    try:
        with open(DownTo, 'wb') as f:
            while True:
                chunk = req.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!'

with disable_file_system_redirection():
    arch=os.popen("wmic os get OSArchitecture").read()

if '64' in arch:
    url = url_64bit_msi
else:
    url = url_32bit_msi
        


def install():
    with disable_file_system_redirection():
        print(downloadFile(DownTo, url))
        obj = Popen('msiexec /i %s /quiet'%(DownTo), shell = True, stdout = PIPE, stderr = PIPE)
        out, err = obj.communicate()
        ret=obj.returncode
        if ret==0:
            print("the MSEdge Browser has been successfully updated")
            if out:
                print(out)
        else:
            if err:
                print("update failed. an error has occurred: \n%s"%(err))
            else:
                print("something went wrong while updating the application")
                print("return code: %s"%(ret))
        os.remove(DownTo)

install()