From 85357ee284a99b2af1a90630f5b2e0f5822ec9dc Mon Sep 17 00:00:00 2001 From: sylvain thenault Date: Mon, 26 Jan 2009 13:13:32 +0100 Subject: new pushd context manager --- contexts.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/contexts.py b/contexts.py index a1ade00..d88abfb 100644 --- a/contexts.py +++ b/contexts.py @@ -11,6 +11,7 @@ import sys if sys.version_info < (2, 5): raise ImportError("python >= 2.5 is required to import logilab.common.contexts") +import os import tempfile import shutil @@ -24,4 +25,17 @@ class tempdir(object): # rmtree in all cases shutil.rmtree(self.path) return traceback is None - + + +class pushd(object): + def __init__(self, directory): + self.directory = directory + + def __enter__(self): + self.cwd = os.getcwd() + os.chdir(self.directory) + return self.directory + + def __exit__(self, exctype, value, traceback): + os.chdir(self.cwd) + -- cgit v1.2.1