From e1fde7f6a55fb2a2fbf456fa06ad574250c79ad3 Mon Sep 17 00:00:00 2001 From: Tomas Groth Date: Mon, 13 Oct 2014 12:26:23 +0200 Subject: [PATCH 1/4] Ask the user if a backup of the data folder should be created after upgrading --- openlp/core/__init__.py | 41 ++++++++++++++++++++++++++++++++-- openlp/core/common/settings.py | 1 + 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/openlp/core/__init__.py b/openlp/core/__init__.py index cb9105797..789b585f3 100644 --- a/openlp/core/__init__.py +++ b/openlp/core/__init__.py @@ -39,11 +39,12 @@ import sys import logging from optparse import OptionParser from traceback import format_exception - +import shutil +import time from PyQt4 import QtCore, QtGui from openlp.core.common import Registry, OpenLPMixin, AppLocation, Settings, UiStrings, check_directory_exists, \ - is_macosx, is_win + is_macosx, is_win, translate from openlp.core.lib import ScreenList from openlp.core.resources import qInitResources from openlp.core.ui.mainwindow import MainWindow @@ -136,6 +137,8 @@ class OpenLP(OpenLPMixin, QtGui.QApplication): self.splash.show() # make sure Qt really display the splash screen self.processEvents() + # Check if OpenLP has been upgrade and if a backup of data should be created + self.backup_on_upgrade(has_run_wizard) # start the main app window self.main_window = MainWindow() Registry().execute('bootstrap_initialise') @@ -192,6 +195,40 @@ class OpenLP(OpenLPMixin, QtGui.QApplication): self.set_normal_cursor() self.exception_form.exec_() + def backup_on_upgrade(self, old_install): + """ + Check if OpenLP has been upgraded, and ask if a backup of data should be made + + :param old_install: OpenLP has been run before + """ + data_version = Settings().value('core/data version') + openlp_version = get_application_version()['version'] + # New installation, no need to create backup + if not old_install: + Settings().setValue('core/data version', openlp_version) + # If data_version is different from the current version ask if we should backup the data folder + elif data_version != openlp_version: + if QtGui.QMessageBox.question(None, translate('OpenLP', 'Backup'), + translate('OpenLP', 'OpenLP has been upgraded, ' + 'do you want to create a backup of OpenLPs data folder?'), + QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, + QtGui.QMessageBox.Yes) == QtGui.QMessageBox.Yes: + # Create copy of data folder + data_folder_path = AppLocation.get_data_path() + timestamp = time.strftime("%Y%m%d-%H%M%S") + data_folder_backup_path = data_folder_path + '-' + timestamp + try: + shutil.copytree(data_folder_path, data_folder_backup_path) + except OSError: + QtGui.QMessageBox.warning(None, translate('OpenLP', 'Backup'), + translate('OpenLP', 'Backup of the data folder failed!')) + return + QtGui.QMessageBox.information(None, translate('OpenLP', 'Backup'), + translate('OpenLP', 'A backup of the data folder has been created at %s') + % data_folder_backup_path) + # Update the version in the settings + Settings().setValue('core/data version', openlp_version) + def process_events(self): """ Wrapper to make ProcessEvents visible and named correctly diff --git a/openlp/core/common/settings.py b/openlp/core/common/settings.py index f7202b590..a0adbe906 100644 --- a/openlp/core/common/settings.py +++ b/openlp/core/common/settings.py @@ -137,6 +137,7 @@ class Settings(QtCore.QSettings): # circular dependency. 'core/display on monitor': True, 'core/override position': False, + 'core/data version': '0.0', 'images/background color': '#000000', 'media/players': 'webkit', 'media/override player': QtCore.Qt.Unchecked, From 5091b3de372a9ccebeedc25ee5eec497292c8d38 Mon Sep 17 00:00:00 2001 From: Tomas Groth Date: Mon, 13 Oct 2014 16:08:06 +0200 Subject: [PATCH 2/4] Added a test --- openlp/core/__init__.py | 4 +-- tests/functional/test_init.py | 53 ++++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/openlp/core/__init__.py b/openlp/core/__init__.py index 789b585f3..9646ae77e 100644 --- a/openlp/core/__init__.py +++ b/openlp/core/__init__.py @@ -226,8 +226,8 @@ class OpenLP(OpenLPMixin, QtGui.QApplication): QtGui.QMessageBox.information(None, translate('OpenLP', 'Backup'), translate('OpenLP', 'A backup of the data folder has been created at %s') % data_folder_backup_path) - # Update the version in the settings - Settings().setValue('core/data version', openlp_version) + # Update the version in the settings + Settings().setValue('core/data version', openlp_version) def process_events(self): """ diff --git a/tests/functional/test_init.py b/tests/functional/test_init.py index 6fc7d7f70..1c2ce4339 100644 --- a/tests/functional/test_init.py +++ b/tests/functional/test_init.py @@ -33,9 +33,10 @@ import os from unittest import TestCase from unittest.mock import MagicMock, patch -from PyQt4 import QtCore +from PyQt4 import QtCore, QtGui from openlp.core import OpenLP +from openlp.core.common import Settings from tests.helpers.testmixin import TestMixin @@ -44,11 +45,13 @@ TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'resou class TestInit(TestCase, TestMixin): def setUp(self): + self.build_settings() with patch('openlp.core.common.OpenLPMixin.__init__') as constructor: constructor.return_value = None self.openlp = OpenLP(list()) def tearDown(self): + self.destroy_settings() del self.openlp def event_test(self): @@ -68,3 +71,51 @@ class TestInit(TestCase, TestMixin): self.assertTrue(result, "The method should have returned True.") mocked_file_method.assert_called_once_with() self.assertEqual(self.openlp.args[0], file_path, "The path should be in args.") + + def backup_on_upgrade_first_install_test(self): + """ + Test that we don't try to backup on a new install + """ + # GIVEN: Mocked data version and OpenLP version which are the same + old_install = False + MOCKED_VERSION = { + 'full': '2.2.0-bzr000', + 'version': '2.2.0', + 'build': 'bzr000' + } + Settings().setValue('core/data version', '2.2.0') + with patch('openlp.core.get_application_version') as mocked_get_application_version,\ + patch('openlp.core.QtGui.QMessageBox.question') as mocked_question: + mocked_get_application_version.return_value = MOCKED_VERSION + mocked_question.return_value = QtGui.QMessageBox.No + + # WHEN: We check if a backup should be created + self.openlp.backup_on_upgrade(old_install) + + # THEN: It should not ask if we want to create a backup + self.assertEqual(Settings().value('core/data version'), '2.2.0', 'Version should be the same!') + self.assertEqual(mocked_question.call_count, 0, 'No question should have been asked!') + + def backup_on_upgrade_test(self): + """ + Test that we try to backup on a new install + """ + # GIVEN: Mocked data version and OpenLP version which are different + old_install = True + MOCKED_VERSION = { + 'full': '2.2.0-bzr000', + 'version': '2.2.0', + 'build': 'bzr000' + } + Settings().setValue('core/data version', '2.0.5') + with patch('openlp.core.get_application_version') as mocked_get_application_version,\ + patch('openlp.core.QtGui.QMessageBox.question') as mocked_question: + mocked_get_application_version.return_value = MOCKED_VERSION + mocked_question.return_value = QtGui.QMessageBox.No + + # WHEN: We check if a backup should be created + self.openlp.backup_on_upgrade(old_install) + + # THEN: It should ask if we want to create a backup + self.assertEqual(Settings().value('core/data version'), '2.2.0', 'Version should be upgraded!') + self.assertEqual(mocked_question.call_count, 1, 'A question should have been asked!') From f36c8660ab28ac72225ec5bd29e3f3c4c7918805 Mon Sep 17 00:00:00 2001 From: Tomas Groth Date: Mon, 13 Oct 2014 20:24:19 +0100 Subject: [PATCH 3/4] Changed a variable name --- openlp/core/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/openlp/core/__init__.py b/openlp/core/__init__.py index 9646ae77e..eae3d0ab9 100644 --- a/openlp/core/__init__.py +++ b/openlp/core/__init__.py @@ -195,16 +195,16 @@ class OpenLP(OpenLPMixin, QtGui.QApplication): self.set_normal_cursor() self.exception_form.exec_() - def backup_on_upgrade(self, old_install): + def backup_on_upgrade(self, has_run_wizard): """ Check if OpenLP has been upgraded, and ask if a backup of data should be made - :param old_install: OpenLP has been run before + :param has_run_wizard: OpenLP has been run before """ data_version = Settings().value('core/data version') openlp_version = get_application_version()['version'] # New installation, no need to create backup - if not old_install: + if not has_run_wizard: Settings().setValue('core/data version', openlp_version) # If data_version is different from the current version ask if we should backup the data folder elif data_version != openlp_version: From cbd1b7c223965c64b783ed2049a187225a8b1e43 Mon Sep 17 00:00:00 2001 From: Tomas Groth Date: Tue, 14 Oct 2014 09:52:59 +0200 Subject: [PATCH 4/4] Changed setting version name --- openlp/core/__init__.py | 6 +++--- openlp/core/common/settings.py | 2 +- tests/functional/test_init.py | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/openlp/core/__init__.py b/openlp/core/__init__.py index eae3d0ab9..fa952162d 100644 --- a/openlp/core/__init__.py +++ b/openlp/core/__init__.py @@ -201,11 +201,11 @@ class OpenLP(OpenLPMixin, QtGui.QApplication): :param has_run_wizard: OpenLP has been run before """ - data_version = Settings().value('core/data version') + data_version = Settings().value('core/application version') openlp_version = get_application_version()['version'] # New installation, no need to create backup if not has_run_wizard: - Settings().setValue('core/data version', openlp_version) + Settings().setValue('core/application version', openlp_version) # If data_version is different from the current version ask if we should backup the data folder elif data_version != openlp_version: if QtGui.QMessageBox.question(None, translate('OpenLP', 'Backup'), @@ -227,7 +227,7 @@ class OpenLP(OpenLPMixin, QtGui.QApplication): translate('OpenLP', 'A backup of the data folder has been created at %s') % data_folder_backup_path) # Update the version in the settings - Settings().setValue('core/data version', openlp_version) + Settings().setValue('core/application version', openlp_version) def process_events(self): """ diff --git a/openlp/core/common/settings.py b/openlp/core/common/settings.py index a0adbe906..dc61b10fd 100644 --- a/openlp/core/common/settings.py +++ b/openlp/core/common/settings.py @@ -137,7 +137,7 @@ class Settings(QtCore.QSettings): # circular dependency. 'core/display on monitor': True, 'core/override position': False, - 'core/data version': '0.0', + 'core/application version': '0.0', 'images/background color': '#000000', 'media/players': 'webkit', 'media/override player': QtCore.Qt.Unchecked, diff --git a/tests/functional/test_init.py b/tests/functional/test_init.py index 1c2ce4339..7897895c7 100644 --- a/tests/functional/test_init.py +++ b/tests/functional/test_init.py @@ -83,7 +83,7 @@ class TestInit(TestCase, TestMixin): 'version': '2.2.0', 'build': 'bzr000' } - Settings().setValue('core/data version', '2.2.0') + Settings().setValue('core/application version', '2.2.0') with patch('openlp.core.get_application_version') as mocked_get_application_version,\ patch('openlp.core.QtGui.QMessageBox.question') as mocked_question: mocked_get_application_version.return_value = MOCKED_VERSION @@ -93,7 +93,7 @@ class TestInit(TestCase, TestMixin): self.openlp.backup_on_upgrade(old_install) # THEN: It should not ask if we want to create a backup - self.assertEqual(Settings().value('core/data version'), '2.2.0', 'Version should be the same!') + self.assertEqual(Settings().value('core/application version'), '2.2.0', 'Version should be the same!') self.assertEqual(mocked_question.call_count, 0, 'No question should have been asked!') def backup_on_upgrade_test(self): @@ -107,7 +107,7 @@ class TestInit(TestCase, TestMixin): 'version': '2.2.0', 'build': 'bzr000' } - Settings().setValue('core/data version', '2.0.5') + Settings().setValue('core/application version', '2.0.5') with patch('openlp.core.get_application_version') as mocked_get_application_version,\ patch('openlp.core.QtGui.QMessageBox.question') as mocked_question: mocked_get_application_version.return_value = MOCKED_VERSION @@ -117,5 +117,5 @@ class TestInit(TestCase, TestMixin): self.openlp.backup_on_upgrade(old_install) # THEN: It should ask if we want to create a backup - self.assertEqual(Settings().value('core/data version'), '2.2.0', 'Version should be upgraded!') + self.assertEqual(Settings().value('core/application version'), '2.2.0', 'Version should be upgraded!') self.assertEqual(mocked_question.call_count, 1, 'A question should have been asked!')