X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/e6c778f6e92e584815a14d3e6a31a03a5e2ab7e1..e0f595e44766e352edfce0aaf5d32be57f448882:/src/sources/utils.py diff --git a/src/sources/utils.py b/src/sources/utils.py new file mode 100644 index 00000000..1326ea3a --- /dev/null +++ b/src/sources/utils.py @@ -0,0 +1,25 @@ +from contextlib import contextmanager +import os +import shutil +from time import time + + +@contextmanager +def replace_dir(d): + # create tmp dir + d = d.rstrip('/') + ts = int(time()) + dnew = f'{d}.{ts}.new' + dold = f'{d}.{ts}.old' + os.makedirs(dnew) + try: + yield dnew + except: + shutil.rmtree(dnew) + raise + else: + if os.path.exists(d): + shutil.move(d, dold) + shutil.move(dnew, d) + if os.path.exists(dold): + shutil.rmtree(dold)