.

[Python] Bandizip Updater

by 담배맛구마

 

별건 아니고... Bandizip가 업데이트 강제로 돌리는게 필요해서 만듬

 

Bandizip Updater.py
0.01MB

from urllib import request
from urllib import parse
import urllib.error
import winreg
import sys
import platform
import zlib
import subprocess

class bandizip:
    def __init__(self, debug=False):
        self.debug = debug

    def run(self):
        self.cur_version = self.get_installed_version()
        if self.cur_version:
            self.debuging('[C] Current version of Bandizip is {}'.format(self.cur_version))
        else:
            return False
        
        self.last_version = self.get_last_information()
        if self.last_version:
            self.debuging('[C] Lastest version of Bandizip is {}'.format(self.last_version[0]))
        else:
            return False

        self.last_version = ('99999', 'BANDIZIP-SETUP-KR.EXE', 'https://dl.bandisoft.com/bandizip.kr/BANDIZIP-SETUP-KR.EXE', 'a8e54941')
        if int(self.cur_version) == int(self.last_version[0]):
            self.debuging('[C] Bandizip is latest version')
        elif int(self.cur_version) < int(self.last_version[0]):
            self.debuging('[C] Bandizip needs to update')
            self.debuging('[C] Update file is "{}"'.format(self.last_version[1]))
            self.debuging('[C] Update file URL is "{}"'.format(self.last_version[2]))
            self.debuging('[C] Update file CRC32 is "{}"'.format(self.last_version[3]))
            
            if self.get_update_file((self.last_version[1]), self.last_version[2], self.last_version[3]):
                self.debuging('[C] Complete to donwload update file')
                self.install_update_file(self.last_version[1])
                self.debuging('[C] Complete to install')
        else:
            self.debuging('[C] Wrong version value.')

    def get_installed_version(self):
        key = None
        installLoc = None
        version = None
        try:
            key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Bandizip')
        except FileNotFoundError:
            arch = platform.architecture()[0]
            if arch == '32bit':
                flag = winreg.KEY_WOW64_64KEY
            elif arch == '64bit':
                flag = winreg.KEY_WOW64_32KEY

            try:
                key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Bandizip', access=winreg.KEY_READ | flag)
            except FileNotFoundError:
                self.debuging('[E] Registry not found (SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Bandizip).')
                return False
        try:
            installLoc = winreg.QueryValueEx(key, 'InstallLocation')[0]
        except FileNotFoundError:
            self.debuging('[E] Registry not found (InstallLocation).')
            return False
        
        try:
            version = open('{}\\VersionNo.ini'.format(installLoc)).read().strip().split('=')[1]
        except FileNotFoundError:
            self.debuging('[E] "VersionNo.ini" not found')
            return False

        return version

    def get_last_information(self):
        data = None
        try:
            data = request.urlopen('https://ver.bandi.so/bandizip.kr/versioninfo.ini').read().decode('utf-16')
        except urllib.error.URLError as E:
            self.debuging('[E] Fail to get versioninfo.ini - {}'.format(E))
            return False

        last_version = None
        filename = None
        url = None
        crc32 = None
        for info_list in data.split('\r\n\r\n'):
            info = info_list.split('\r\n')
            # Getting Version.
            if info[0] == '[Version]':
                last_version = info[2].split('=')[1].strip()
                continue
            # Getting Download infomation.
            elif info[0].startswith('[DOWNLOAD'):
                for idx in range(len(info)):
                    if info[idx].startswith('FILENAME'):
                        filename = info[idx].split('=')[1].strip()
                    elif info[idx].startswith('URL'):
                        if info[idx].split('=')[1].strip():
                            url = info[idx].split('=')[1].strip()
                    elif info[idx].startswith('CRC32'):
                        crc32 = info[idx].split('=')[1].strip()
            if url and crc32:
                break
        return (last_version, filename, url, crc32)

    def get_update_file(self, filename, url, crc32):
        # print(filename, url, crc32)
        try:
            request.urlretrieve(url, filename)
            self.debuging('[C] Download update file')
        except urllib.error.URLError as E:
            self.debuging('[E] Urlretrieve - {}'.format(E))
            return False
        
        try:
            cur_crc32 = zlib.crc32(open(filename, 'rb').read())
            if cur_crc32 == int(crc32, 16):
                self.debuging('[C] CRC32 checksom clear')
                return True
            else:
                self.debuging('[E] CRC32 checksom error({} --X--> {})'.format(cur_crc32, crc32))
                return False
        except zlib.error as E:
            self.debuging('[E] fail to compute crc32 - {}'.format(E))
            return False

    def install_update_file(self, filename):
        info = subprocess.STARTUPINFO()
        info.dwFlags = subprocess.STARTF_USESHOWWINDOW
        info.wShowWindow = 0
        subprocess.Popen([filename, '/update'], startupinfo=info)
        

    def debuging(self, ment):
        if self.debug:
            print(ment)




            

if __name__ == "__main__":
    obj = bandizip(debug=True)
    obj.run()
반응형

블로그의 정보

정윤상이다.

담배맛구마

활동하기