URL=r"https://cdn3.m-files.com/public/M-Fi...10524_3_EV.msi" #provide a URL 
import ctypes
import re
import time
import socket
import _winreg
import platform
import shutil
import ssl
import urllib2
import getpass
import zipfile
import shutil
import os
import glob
from subprocess import PIPE,Popen
import ctypes
start=time.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)
def permissions(dirpath):
    mode=0o777
    if os.path.isdir(dirpath):
        try:
            for root,dirs,files in os.walk(dirpath,topdown=False):
                for dircs in [os.path.join(root,d) for d in dirs]:
                    os.chmod(dircs,mode)
                for s_file in [os.path.join(root,f) for f in files]:
                    os.chmod(s_file,mode)
        except Exception as E:
            print "File being Used %s"
            
def Download(src_path, URL,fp):
    import urllib2
    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)
    if not os.path.exists(src_path):
        os.makedirs(src_path)
    with open(fp, 'wb') as f:
        while True:
            chunk=parsed.read(100*1000*1000)
            if chunk:
                f.write(chunk)
            else:
                break
    return fp
Folder=os.environ['programdata']+r"\extraction_file"
if not os.path.exists(Folder):
    os.mkdir(Folder)
fileName=r"M-Files_Online_x64_eng_client_21_8_10524_3_EV.msi"
src_path=Folder
fp = os.path.join(src_path, fileName)    
Excutable_path=Download(Folder, URL,fp)
print "Downloaded succesfully to "+Excutable_path+""

reg_file="""Windows Registry Editor Version 5.00


[HKEY_LOCAL_MACHINE\SOFTWARE\Motive\M-Files\11.3.4330.254\Client\MFClient\Vaults]
"MaxVaultMountingID"=dword:00000001

[HKEY_LOCAL_MACHINE\SOFTWARE\Motive\M-Files\11.3.4330.254\Client\MFClient\Vaults\DEVOTEAM]
"ID"=dword:00000001
"GUID"="{80010BD1-9E68-465D-9BEF-0BD97895C64A}"
"InfoTip"=""
"ProtocolSequence"="ncacn_http"
"NetworkAddress"="mfiles.devoteam.com"
"Endpoint"="4466"
"MinimumAuthenticationLevel"=dword:00000001
"ServerVaultGUID"="{C150B727-880C-4973-A369-B6DAAA4A0B0B}"
"ServerVaultName"="DEVOTEAM"
"AuthType"=dword:00000003
"AutoLogin"=dword:00000001
"SPN"=""

[HKEY_LOCAL_MACHINE\SOFTWARE\Motive\M-Files\11.3.4330.254\Client\MFShell]
"SharingEnabled"=dword:00000000

[HKEY_LOCAL_MACHINE\SOFTWARE\Motive\M-Files\Common]

[HKEY_LOCAL_MACHINE\SOFTWARE\Motive\M-Files\Common\VaultRegistration]

[HKEY_LOCAL_MACHINE\SOFTWARE\Motive\M-Files\Common\VaultRegistration\{80010BD1-9E68-465D-9BEF-0BD97895C64A}]
@="11.3.4330.254"



"""

with open('C:\\ProgramData\\\extraction_file\\MFilesDevoteamConfig.reg','w') as f:
    f.write(reg_file)

dest_path=os.environ['programdata']+r"\extraction_file"

permissions(dest_path)
os.chdir(dest_path)
ls=['reg import MFilesDevoteamConfig.reg','msiexec /i M-Files_Online_x64_eng_client_21_8_10524_3_EV.msi /quiet /qn /norestart']

with disable_file_system_redirection():
    for i in ls:
        process=Popen(i,stdout=PIPE,stderr=PIPE)
        r,e=process.communicate()
        if e:
            print e
        else:
            print r
    shutil.rmtree(dest_path,ignore_errors=True)

end=time.time()
t=end-start
print "Execution Time %s"%t