From 4259852ec305a41375a2b77afef283264c73b5e7 Mon Sep 17 00:00:00 2001 From: Laurent Bachelier Date: Thu, 28 Mar 2019 12:18:13 +0100 Subject: [PATCH] config: Cross-platform file replace --- weboob/tools/config/sqliteconfig.py | 3 ++- weboob/tools/config/util.py | 35 +++++++++++++++++++++++++++++ weboob/tools/config/yamlconfig.py | 3 ++- 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 weboob/tools/config/util.py diff --git a/weboob/tools/config/sqliteconfig.py b/weboob/tools/config/sqliteconfig.py index efef60f380..1fe21f4578 100644 --- a/weboob/tools/config/sqliteconfig.py +++ b/weboob/tools/config/sqliteconfig.py @@ -30,6 +30,7 @@ from .extra import time_buffer from .iconfig import ConfigError, IConfig +from .util import replace from .yamlconfig import WeboobDumper try: @@ -146,7 +147,7 @@ def dump(self, **kwargs): for line in self.storage.iterdump(): f.write(unicode(line).encode('utf-8')) f.write(b'\n') - os.rename(f.name, target) + replace(f.name, target) def ensure_table(self, name): if name not in self._tables: diff --git a/weboob/tools/config/util.py b/weboob/tools/config/util.py new file mode 100644 index 0000000000..0ee7acfbf7 --- /dev/null +++ b/weboob/tools/config/util.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2019 Laurent Bachelier +# +# This file is part of weboob. +# +# weboob is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# weboob is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with weboob. If not, see . + + +import os + +__all__ = ['replace'] + + +try: + from os import replace +except ImportError: + def replace(src, dst, *args, **kwargs): + if os.name != 'posix': + try: + os.remove(dst) + except OSError: + pass + return os.rename(src, dst, *args, **kwargs) diff --git a/weboob/tools/config/yamlconfig.py b/weboob/tools/config/yamlconfig.py index 2f5916426b..59c3aed4e5 100644 --- a/weboob/tools/config/yamlconfig.py +++ b/weboob/tools/config/yamlconfig.py @@ -27,6 +27,7 @@ import yaml from .iconfig import ConfigError, IConfig +from .util import replace try: from yaml import CLoader as Loader @@ -86,7 +87,7 @@ def save(self): f = tempfile.NamedTemporaryFile(mode='w', dir=os.path.dirname(self.path), delete=False, encoding='utf-8') with f: yaml.dump(self.values, f, Dumper=self.DUMPER, default_flow_style=False) - os.rename(f.name, self.path) + replace(f.name, self.path) def get(self, *args, **kwargs): v = self.values -- GitLab