From 81f3f88cb8f7e7669a07e5bb50bf8cefb54047be Mon Sep 17 00:00:00 2001 From: Einar Forselv Date: Tue, 3 Dec 2019 06:23:44 +0100 Subject: [PATCH] enviroment varaible context manager --- restic_volume_backup/utils.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/restic_volume_backup/utils.py b/restic_volume_backup/utils.py index d3314cc..dd212d3 100644 --- a/restic_volume_backup/utils.py +++ b/restic_volume_backup/utils.py @@ -1,3 +1,5 @@ +import os +from contextlib import contextmanager import docker from restic_volume_backup.config import Config @@ -36,3 +38,17 @@ def strip_root(path): return path[1:] return path + + +@contextmanager +def environment(name, value): + """Tempset env var""" + old_val = os.environ.get(name) + os.environ[name] = value + try: + yield + finally: + if old_val is None: + del os.environ[name] + else: + os.environ[name] = old_val