From ebae1e1440af5a2732e22ccd80152ca63644c9cd Mon Sep 17 00:00:00 2001 From: planet36 Date: Thu, 8 Oct 2020 17:11:32 -0400 Subject: [PATCH 1/4] Backup config before writing to it Fixes #234 --- terminatorlib/config.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/terminatorlib/config.py b/terminatorlib/config.py index f58a0716..81e9c20a 100644 --- a/terminatorlib/config.py +++ b/terminatorlib/config.py @@ -71,6 +71,7 @@ """ import os +import shutil from copy import copy from configobj import ConfigObj, flatten_errors from validate import Validator @@ -687,7 +688,7 @@ def reload(self): """Force a reload of the base config""" self.loaded = False self.load() - + def save(self): """Save the config to a file""" dbg('ConfigBase::save: saving config') @@ -719,12 +720,14 @@ def save(self): if not os.path.isdir(config_dir): os.makedirs(config_dir) try: - temp_file = self.command_line_options.config + '.tmp' + backup_file = self.command_line_options.config + '~' + + shutil.copy2(self.command_line_options.config, backup_file) - with open(temp_file, 'wb') as fh: + with open(self.command_line_options.config, 'wb') as fh: parser.write(fh) - os.rename(temp_file, self.command_line_options.config) + os.remove(backup_file) except Exception as ex: err('ConfigBase::save: Unable to save config: %s' % ex) From 310f70ac5f3b72fcc3a9ad93a255f5e7a4a4ebb0 Mon Sep 17 00:00:00 2001 From: planet36 Date: Fri, 9 Oct 2020 20:28:48 -0400 Subject: [PATCH 2/4] Create config file if it doesn't exist Use pathlib.Path for config file and backup file --- terminatorlib/config.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/terminatorlib/config.py b/terminatorlib/config.py index 81e9c20a..4ab34d57 100644 --- a/terminatorlib/config.py +++ b/terminatorlib/config.py @@ -71,6 +71,7 @@ """ import os +import pathlib import shutil from copy import copy from configobj import ConfigObj, flatten_errors @@ -719,15 +720,19 @@ def save(self): config_dir = get_config_dir() if not os.path.isdir(config_dir): os.makedirs(config_dir) + + config_file = pathlib.Path(self.command_line_options.config) + if not config_file.is_file(): + config_file.touch() try: - backup_file = self.command_line_options.config + '~' + backup_file = config_file.with_suffix('.bak') - shutil.copy2(self.command_line_options.config, backup_file) + shutil.copy2(config_file, backup_file) - with open(self.command_line_options.config, 'wb') as fh: + with open(config_file, 'wb') as fh: parser.write(fh) - os.remove(backup_file) + backup_file.unlink() except Exception as ex: err('ConfigBase::save: Unable to save config: %s' % ex) From 610580b7044000c03b158ea50cf1ebff15dd2345 Mon Sep 17 00:00:00 2001 From: planet36 Date: Sun, 11 Oct 2020 23:16:46 -0400 Subject: [PATCH 3/4] Revert "Create config file if it doesn't exist" This reverts commit 310f70ac5f3b72fcc3a9ad93a255f5e7a4a4ebb0. --- terminatorlib/config.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/terminatorlib/config.py b/terminatorlib/config.py index 4ab34d57..81e9c20a 100644 --- a/terminatorlib/config.py +++ b/terminatorlib/config.py @@ -71,7 +71,6 @@ """ import os -import pathlib import shutil from copy import copy from configobj import ConfigObj, flatten_errors @@ -720,19 +719,15 @@ def save(self): config_dir = get_config_dir() if not os.path.isdir(config_dir): os.makedirs(config_dir) - - config_file = pathlib.Path(self.command_line_options.config) - if not config_file.is_file(): - config_file.touch() try: - backup_file = config_file.with_suffix('.bak') + backup_file = self.command_line_options.config + '~' - shutil.copy2(config_file, backup_file) + shutil.copy2(self.command_line_options.config, backup_file) - with open(config_file, 'wb') as fh: + with open(self.command_line_options.config, 'wb') as fh: parser.write(fh) - backup_file.unlink() + os.remove(backup_file) except Exception as ex: err('ConfigBase::save: Unable to save config: %s' % ex) From 2d7f9133b0f47b46058e9c76d325a126da1d7459 Mon Sep 17 00:00:00 2001 From: planet36 Date: Sun, 11 Oct 2020 23:28:04 -0400 Subject: [PATCH 4/4] Create config file if it doesn't exist --- terminatorlib/config.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/terminatorlib/config.py b/terminatorlib/config.py index 81e9c20a..ffbbfdb6 100644 --- a/terminatorlib/config.py +++ b/terminatorlib/config.py @@ -719,7 +719,11 @@ def save(self): config_dir = get_config_dir() if not os.path.isdir(config_dir): os.makedirs(config_dir) + try: + if not os.path.isfile(self.command_line_options.config): + open(self.command_line_options.config, 'a').close() + backup_file = self.command_line_options.config + '~' shutil.copy2(self.command_line_options.config, backup_file)