Merge pull request #235 from planet36/save_config_file

Change how config file is saved
This commit is contained in:
Matt Rose 2020-10-14 11:54:20 -04:00 committed by GitHub
commit 44f538f3f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 5 deletions

View File

@ -71,6 +71,7 @@ KeyError: 'ConfigBase::get_item: unknown key algo'
""" """
import os import os
import shutil
from copy import copy from copy import copy
from configobj import ConfigObj, flatten_errors from configobj import ConfigObj, flatten_errors
from validate import Validator from validate import Validator
@ -718,13 +719,19 @@ class ConfigBase(Borg):
config_dir = get_config_dir() config_dir = get_config_dir()
if not os.path.isdir(config_dir): if not os.path.isdir(config_dir):
os.makedirs(config_dir) os.makedirs(config_dir)
try:
temp_file = self.command_line_options.config + '.tmp'
with open(temp_file, 'wb') as fh: 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)
with open(self.command_line_options.config, 'wb') as fh:
parser.write(fh) parser.write(fh)
os.rename(temp_file, self.command_line_options.config) os.remove(backup_file)
except Exception as ex: except Exception as ex:
err('ConfigBase::save: Unable to save config: %s' % ex) err('ConfigBase::save: Unable to save config: %s' % ex)