enviroment varaible context manager

This commit is contained in:
Einar Forselv 2019-12-03 06:23:44 +01:00
parent 174d9b9c18
commit 81f3f88cb8
1 changed files with 16 additions and 0 deletions

View File

@ -1,3 +1,5 @@
import os
from contextlib import contextmanager
import docker import docker
from restic_volume_backup.config import Config from restic_volume_backup.config import Config
@ -36,3 +38,17 @@ def strip_root(path):
return path[1:] return path[1:]
return path 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