From be145b7675b83e237d069bb839c9b49b44532dc9 Mon Sep 17 00:00:00 2001 From: Tim Bentley Date: Fri, 9 Jun 2017 21:53:13 +0100 Subject: [PATCH] handle the version from the web site --- openlp/core/__init__.py | 6 ++-- openlp/core/api/tab.py | 14 ++++++++-- openlp/core/common/httputils.py | 15 ++++++++++ openlp/core/common/versionchecker.py | 21 +++++++++----- openlp/plugins/remotes/remoteplugin.py | 14 +++++++++- .../openlp_core_common/test_httputils.py | 28 ++++++++++++++++++- 6 files changed, 82 insertions(+), 16 deletions(-) diff --git a/openlp/core/__init__.py b/openlp/core/__init__.py index c527a4e20..a611fca36 100644 --- a/openlp/core/__init__.py +++ b/openlp/core/__init__.py @@ -153,10 +153,8 @@ class OpenLP(OpenLPMixin, QtWidgets.QApplication): self.processEvents() if not has_run_wizard: self.main_window.first_time() - # update_check = Settings().value('core/update check') - # if update_check: - # version = VersionThread(self.main_window) - # version.start() + version = VersionThread(self.main_window) + version.start() self.main_window.is_display_blank() self.main_window.app_startup() return self.exec() diff --git a/openlp/core/api/tab.py b/openlp/core/api/tab.py index e37a283b9..daf89f8e1 100644 --- a/openlp/core/api/tab.py +++ b/openlp/core/api/tab.py @@ -22,7 +22,7 @@ from PyQt5 import QtCore, QtGui, QtNetwork, QtWidgets -from openlp.core.common import UiStrings, Settings, translate +from openlp.core.common import UiStrings, Registry, Settings, translate from openlp.core.lib import SettingsTab ZERO_URL = '0.0.0.0' @@ -38,6 +38,7 @@ class ApiTab(SettingsTab): super(ApiTab, self).__init__(parent, 'api', advanced_translated) self.define_main_window_icon() self.generate_icon() + Registry().register_function('set_website_version', self.set_website_version) def setupUi(self): self.setObjectName('ApiTab') @@ -93,7 +94,6 @@ class ApiTab(SettingsTab): self.user_login_group_box.setCheckable(True) self.user_login_group_box.setChecked(False) self.user_login_group_box.setObjectName('user_login_group_box') - self.user_login_layout = QtWidgets.QFormLayout(self.user_login_group_box) self.user_login_layout.setObjectName('user_login_layout') self.user_id_label = QtWidgets.QLabel(self.user_login_group_box) @@ -180,7 +180,6 @@ class ApiTab(SettingsTab): def retranslateUi(self): self.tab_title_visible = translate('RemotePlugin.RemoteTab', 'Remote Interface') - self.server_settings_group_box.setTitle(translate('RemotePlugin.RemoteTab', 'Server Settings')) self.address_label.setText(translate('RemotePlugin.RemoteTab', 'Serve on IP address:')) self.port_label.setText(translate('RemotePlugin.RemoteTab', 'Port number:')) @@ -306,3 +305,12 @@ class ApiTab(SettingsTab): painter.end() self.remote_server_icon.setPixmap(QtGui.QPixmap.fromImage(icon)) self.remote_server_icon.show() + + def set_website_version(self): + """ + Update the website version when it has been downloaded + :return: + """ + self.master_version_value.setText(Registry().get_flag('website_version')) + if self.master_version_value.text() == self.current_version_value.text(): + self.update_site_group_box.setEnabled(False) diff --git a/openlp/core/common/httputils.py b/openlp/core/common/httputils.py index 5adb0959d..0294623a5 100644 --- a/openlp/core/common/httputils.py +++ b/openlp/core/common/httputils.py @@ -25,8 +25,10 @@ The :mod:`openlp.core.utils` module provides the utility libraries for OpenLP. import hashlib import logging import os +import platform import socket import sys +import subprocess import time import urllib.error import urllib.parse @@ -254,4 +256,17 @@ def url_get_file(callback, url, f_path, sha256=None): return True +def ping(host): + """ + Returns True if host responds to a ping request + """ + # Ping parameters as function of OS + ping_str = "-n 1" if platform.system().lower()=="windows" else "-c 1" + args = "ping " + " " + ping_str + " " + host + need_sh = False if platform.system().lower()=="windows" else True + + # Ping + return subprocess.call(args, shell=need_sh) == 0 + + __all__ = ['get_web_page'] diff --git a/openlp/core/common/versionchecker.py b/openlp/core/common/versionchecker.py index 25479884f..863bd9730 100644 --- a/openlp/core/common/versionchecker.py +++ b/openlp/core/common/versionchecker.py @@ -12,7 +12,8 @@ from subprocess import Popen, PIPE from PyQt5 import QtCore -from openlp.core.common import AppLocation, Settings +from openlp.core.common import AppLocation, Registry, Settings +from openlp.core.common.httputils import ping log = logging.getLogger(__name__) @@ -42,12 +43,18 @@ class VersionThread(QtCore.QThread): """ self.sleep(1) log.debug('Version thread - run') - app_version = get_application_version() - version = check_latest_version(app_version) - log.debug("Versions {version1} and {version2} ".format(version1=LooseVersion(str(version)), - version2=LooseVersion(str(app_version['full'])))) - if LooseVersion(str(version)) > LooseVersion(str(app_version['full'])): - self.main_window.openlp_version_check.emit('{version}'.format(version=version)) + found = ping("openlp.io") + Registry().set_flag('internet_present', found) + update_check = Settings().value('core/update check') + if found: + Registry().execute('get_website_version') + if update_check: + app_version = get_application_version() + version = check_latest_version(app_version) + log.debug("Versions {version1} and {version2} ".format(version1=LooseVersion(str(version)), + version2=LooseVersion(str(app_version['full'])))) + if LooseVersion(str(version)) > LooseVersion(str(app_version['full'])): + self.main_window.openlp_version_check.emit('{version}'.format(version=version)) def get_application_version(): diff --git a/openlp/plugins/remotes/remoteplugin.py b/openlp/plugins/remotes/remoteplugin.py index e5b19c94e..150ba6157 100644 --- a/openlp/plugins/remotes/remoteplugin.py +++ b/openlp/plugins/remotes/remoteplugin.py @@ -27,7 +27,7 @@ from openlp.core.api.http import register_endpoint from openlp.core.common import AppLocation, Registry, OpenLPMixin, check_directory_exists from openlp.core.lib import Plugin, StringContent, translate, build_icon from openlp.plugins.remotes.endpoint import remote_endpoint -from openlp.plugins.remotes.deploy import download_and_check +from openlp.plugins.remotes.deploy import download_and_check, download_sha256 log = logging.getLogger(__name__) __default_settings__ = { @@ -48,6 +48,8 @@ class RemotesPlugin(Plugin, OpenLPMixin): self.weight = -1 register_endpoint(remote_endpoint) Registry().register_function('download_website', self.first_time) + Registry().register_function('get_website_version', self.website_version) + Registry().set_flag('website_version', '0001_01_01') def initialise(self): """ @@ -91,3 +93,13 @@ class RemotesPlugin(Plugin, OpenLPMixin): self.application.process_events() download_and_check() self.application.process_events() + + def website_version(self): + """ + Download and save the website version and sha256 + :return: None + """ + sha256, version = download_sha256() + Registry().set_flag('website_sha256', sha256) + Registry().set_flag('website_version', version) + Registry().execute('set_website_version') diff --git a/tests/functional/openlp_core_common/test_httputils.py b/tests/functional/openlp_core_common/test_httputils.py index c44f28198..26ac297dd 100644 --- a/tests/functional/openlp_core_common/test_httputils.py +++ b/tests/functional/openlp_core_common/test_httputils.py @@ -28,7 +28,7 @@ import socket from unittest import TestCase from unittest.mock import MagicMock, patch -from openlp.core.common.httputils import get_user_agent, get_web_page, get_url_file_size, url_get_file +from openlp.core.common.httputils import get_user_agent, get_web_page, get_url_file_size, url_get_file, ping from tests.helpers.testmixin import TestMixin @@ -272,3 +272,29 @@ class TestHttpUtils(TestCase, TestMixin): # THEN: socket.timeout should have been caught # NOTE: Test is if $tmpdir/tempfile is still there, then test fails since ftw deletes bad downloaded files self.assertFalse(os.path.exists(self.tempfile), 'FTW url_get_file should have caught socket.timeout') + + def test_ping_valid(self): + """ + Test ping for OpenLP + """ + # GIVEN: a valid url to test + url = "openlp.io" + + # WHEN: Attempt to check the url exists + url_found = ping(url) + + # THEN: It should be found + self.assertTrue(url_found, 'OpenLP.io is not found') + + def test_ping_invalid(self): + """ + Test ping for OpenLP + """ + # GIVEN: a valid url to test + url = "trb143.io" + + # WHEN: Attempt to check the url exists + url_found = ping(url) + + # THEN: It should be found + self.assertFalse(url_found, 'TRB143.io is found')