def check(): if is_running: sites_dict = site.get_sites_dict() for i, j in sites_dict.items(): logging.info("Checking " + i) check_site(i, j) time.sleep(1)
def extract_new_version(): """Extract files from archive.""" logging.info('Unzipping new version') update_file = ZipFile(UPDATE_FILE, 'r') update_file.extractall('.') update_file.close()
def is_up_to_date() -> bool: """Check if newest version is installed.""" if get_latest_version_id() == appinfo.APP_VERSION_ID: logging.info('App is up to date!') return True else: logging.warning('App is not up to date!') return False
def remove_old_installation(): """Remove old files.""" logging.info('Removing old updater installation') if osinfo.is_win(): os.remove('updater.exe') else: os.remove('updater')
def restore_backup(): """Copy files from backup to folder.""" logging.info('Restoring from last backup') for file in os.listdir(BACKUP_FOLDER): if os.path.isdir(BACKUP_FOLDER + '/' + file): shutil.copytree(BACKUP_FOLDER + '/' + file, file) else: shutil.copy2(BACKUP_FOLDER + '/' + file, '.')
def remove_old_installation(): """Remove old files.""" logging.info('Removing old app installation') for file in os.listdir('.'): if BACKUP_FOLDER not in file: if os.path.isdir(file): shutil.rmtree(file) else: os.remove(file)
def download_new_version(): """Download zip of latest version from server.""" logging.info('Downloading new app version binaries') update_file = open(UPDATE_FILE, 'wb') update_file.write( requests.get(appinfo.API_DOMAIN + UPDATE_FILE_ON_SERVER).content) update_file.close()
def backup_all(): """Backup app files.""" logging.info('Making an backup') if not os.path.exists(BACKUP_FOLDER): os.makedirs(BACKUP_FOLDER) for file in os.listdir('.'): if BACKUP_FOLDER not in file: if os.path.isdir(file): shutil.copytree(file, BACKUP_FOLDER + '/' + file) else: shutil.copy2(file, BACKUP_FOLDER)
def get_changelog() -> str: """Get changelog from server.""" logging.info("Getting changelog from server") changelog_request = requests.get(appinfo.API_DOMAIN + VERSION_CHANGELOG_FILE_ON_SERVER) if changelog_request.status_code != 200: logging.error("Can't get app changelog, status code - " + str(changelog_request.status_code)) raise ConnectionError("Can't get app changelog, status code - " + str(changelog_request.status_code)) return changelog_request.text
def get_latest_version_name() -> str: """Get name of latest version from server. (e.g. v1.0)""" logging.info("Getting latest version's name id from server") version_request = requests.get(appinfo.API_DOMAIN + VERSION_NAME_FILE_ON_SERVER) if version_request.status_code != 200: logging.error("Can't get app version name, status code - " + str(version_request.status_code)) raise ConnectionError("Can't get app version name, status code - " + str(version_request.status_code)) return version_request.text
def get_latest_version_id() -> int: """Get id of latest version from server.""" logging.info("Getting latest version's id id from server") version_request = requests.get(appinfo.API_DOMAIN + VERSION_ID_FILE_ON_SERVER, timeout=3) if version_request.status_code != 200: logging.error("Can't get app version, status code - " + str(version_request.status_code)) raise ConnectionError("Can't get app version, status code - " + str(version_request.status_code)) return int(version_request.text)
def check(site: dict) -> bool: """Check if site is available.""" logging.info('Checking ' + site['url']) settings = site['settings'] success = True r = None url = site['url'] try: r = requests.request(settings['method'], url, headers=settings['headers'], data=settings['body'], proxies=settings['proxy']) except Exception: success = False if not success: logging.info("Can't send request to " + url) site_ = site.copy() if '://' not in url: site_['url'] = 'http://' + url c = check(site_) if not c: site_['url'] = 'https://' + url c = check(site_) return c return False if not is_status_code_good(settings['expected_code'], r.status_code): logging.info("Invalid response's status code: expected " + str(settings['expected_code']) + ', got ' + str(r.status_code) + ' on ' + url) return False if not is_answer_good(settings['expected_answer'], r.text): logging.info("Invalid server's response: expected" + str(settings['expected_answer']) + ', got ' + r.text + ' on ' + url) return False return True
def run(self): """Begin updating.""" logging.info("Update started") if is_up_to_date(): logging.error('Already up to date') raise ValueError('Already up to date') download_new_version() remove_old_installation() extract_new_version() logging.info('Update finished') delete_update_archive() logging.info('Cleaned, exiting from thread')
def run(self): """Begin updating.""" logging.info("Update started") if is_up_to_date(): logging.critical('Already up to date') raise ValueError('Already up to date') self.status = 'Downloading update...' download_new_version() if self.cancel: logging.info('Canceling update') self.status = 'Removing update files...' delete_update_archive() return self.status = 'Creating backup...' backup_all() if self.cancel: logging.info('Canceling update') self.status = 'Removing update files...' delete_update_archive() delete_backup_folder() return self.status = 'Removing old installation...' remove_old_installation() if self.cancel: logging.info('Canceling update') self.status = 'Restoring old version...' restore_backup() self.status = 'Removing update files...' delete_update_archive() delete_backup_folder() return self.status = 'Installing new update...' extract_new_version() if self.cancel: logging.info('Canceling update') self.status = 'Removing update files...' remove_old_installation() self.status = 'Restoring old version...' restore_backup() self.status = 'Removing update files...' delete_update_archive() delete_backup_folder() return logging.info('Update finished') self.status = 'Removing update files...' delete_update_archive() delete_backup_folder() logging.info('Cleaned, exiting from thread')