From a4e881942a17cde05cd16b2bc34e92998ed9dea7 Mon Sep 17 00:00:00 2001 From: Micah Jerome Ellison <4383304+micahellison@users.noreply.github.com> Date: Sat, 24 Aug 2019 15:01:23 -0700 Subject: [PATCH] [GH-632] raising exception in upgrade.py on fail Handling it in install.py to prevent config from being overwritten when upgrade fails --- jrnl/install.py | 10 +++++++++- jrnl/upgrade.py | 8 +++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/jrnl/install.py b/jrnl/install.py index 7cc8f880..68cfb6ea 100644 --- a/jrnl/install.py +++ b/jrnl/install.py @@ -14,6 +14,7 @@ from .Journal import PlainJournal from .EncryptedJournal import EncryptedJournal import yaml import logging +import sys DEFAULT_CONFIG_NAME = 'jrnl.yaml' DEFAULT_JOURNAL_NAME = 'journal.txt' @@ -85,8 +86,15 @@ def load_or_install_jrnl(): if os.path.exists(config_path): log.debug('Reading configuration from file %s', config_path) config = util.load_config(config_path) - upgrade.upgrade_jrnl_if_necessary(config_path) + + try: + upgrade.upgrade_jrnl_if_necessary(config_path) + except upgrade.UpgradeValidationException: + util.prompt("Aborting upgrade. Exiting.") + sys.exit(1) + upgrade_config(config) + return config else: log.debug('Configuration file not found, installing jrnl...') diff --git a/jrnl/upgrade.py b/jrnl/upgrade.py index 1f4f999a..b3d39984 100644 --- a/jrnl/upgrade.py +++ b/jrnl/upgrade.py @@ -96,12 +96,14 @@ older versions of jrnl anymore. # loop through lists to validate failed_journals = [j for j in all_journals if not j.validate_parsing()] + if len(failed_journals) > 0: util.prompt("\nThe following journal{} failed to upgrade:\n{}".format( 's' if len(failed_journals) > 1 else '', "\n".join(j.name for j in failed_journals)) ) - util.prompt("Aborting upgrade.") + raise UpgradeValidationException + return # write all journals - or - don't @@ -112,3 +114,7 @@ older versions of jrnl anymore. backup(config_path) util.prompt("\nWe're all done here and you can start enjoying jrnl 2.".format(config_path)) + +class UpgradeValidationException(Exception): + """Raised when the contents of an upgraded journal do not match the old journal""" + pass