diff --git a/weboob/tools/config/sqliteconfig.py b/weboob/tools/config/sqliteconfig.py index efef60f380e1a08e422a79b06c2b25d02648e870..1fe21f457831cbcd7761caf62cb642c8286f481a 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 0000000000000000000000000000000000000000..0ee7acfbf7b0a099d823a1398ed50124668cf767 --- /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 2f5916426b118914a39a7f4054617793d3955435..59c3aed4e59b5ba5ed39497bb6924469f706e744 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