XCS_Version = itsm.getParameter('XCS_Version')

URL_64=[
    'https://platform-downloads.comodo.com/itsm/release/win/endpoint_security/%s/ces_updates.7z', 
    'https://platform-downloads.comodo.com/itsm/release/win/endpoint_security/%s/ces_updates/x64/cfpconfg.exe', 
    'https://platform-downloads.comodo.com/itsm/release/win/endpoint_security/%s/ces_updates/x64/cmdres.dll', 
    'https://platform-downloads.comodo.com/itsm/release/win/endpoint_security/%s/ces_updates/x64/7za.dll'
    ]
URL_32=[
    'https://platform-downloads.comodo.com/itsm/release/win/endpoint_security/%s/ces_updates.7z', 
    'https://platform-downloads.comodo.com/itsm/release/win/endpoint_security/%s/ces_updates/x32/cfpconfg.exe', 
    'https://platform-downloads.comodo.com/itsm/release/win/endpoint_security/%s/ces_updates/x32/cmdres.dll', 
    'https://platform-downloads.comodo.com/itsm/release/win/endpoint_security/%s/ces_updates/x32/7za.dll'
    ]

import os
import ctypes
import re
import time
import shutil

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 download(path,i):
    import urllib2
    url=i
    usock = urllib2.urlopen(url)                                 
    file_name = url.split('/')[-1]
    fp = os.path.join(path, file_name)
    file_size = int(usock.info().getheaders("Content-Length")[0]) 
    print("Downloading: %s Bytes: %s" % (file_name, file_size))
    downloaded = 0
    block_size = 8192
    with open(fp, 'wb') as f:
        while True:         
           buff = usock.read(block_size)
           if not buff:                                            
               break
           downloaded = downloaded + len(buff)
           f.write(buff)
    print('Downloading completed successfully in',fp)
    return fp
     

def update(fil,workdir):
    print("Updating XCS to Specified Version\n")
    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():
        os.chdir(workdir)
        CMD='cfpconfg.exe --binaryUpdate --file="'+fil+'"'
        os.popen(CMD)

def execute():
    try:
        workdir=os.environ['PROGRAMDATA']+r'\temp'
        if not os.path.exists(workdir):
            os.mkdir(workdir)      
    except:
        workdir=os.environ['SYTEMDRIVE']       
    URL=[]
    get=[]
    if 'PROGRAMFILES(X86)' in os.environ.keys():
        print("Downloading Required Files...\n")
        URL=URL_64
    else:
        print("Downloading Required Files...\n")
        URL=URL_32        
    for i in URL:
        get.append(download(workdir, i%(XCS_Version)))
    update(get[0],workdir)
    if os.path.exists("C:\Program Files\COMODO\COMODO Internet Security - Update"):
        print("System is ready to Reboot for Update...")
        os.popen("shutdown -r")
    else:
        print("XCS updation is not found")

    try:
        for i in get:
            os.remove(i)
    except:
        print(' ')

with disable_file_system_redirection():
    chk=os.popen('wmic product get name').read()         
    if 'Xcitium Client - Security' in chk:
        execute()
    else:
        print('Xcitium Client - Security is not Installed')
