diff options
author | Sylvain Th?nault <sylvain.thenault@logilab.fr> | 2010-08-26 09:14:52 +0200 |
---|---|---|
committer | Sylvain Th?nault <sylvain.thenault@logilab.fr> | 2010-08-26 09:14:52 +0200 |
commit | 7d0cd87c0bf2c2460a04975dc48d9e737598b824 (patch) | |
tree | 1bb1059333e43c9534828447b6ce5bfb260d1c30 /shellutils.py | |
parent | d9ed46f8530856a9d8b1ee91fa5aa39175540f4c (diff) | |
download | logilab-common-7d0cd87c0bf2c2460a04975dc48d9e737598b824.tar.gz |
move contexts.py context managers into shellutils.py and deprecate this module
Diffstat (limited to 'shellutils.py')
-rw-r--r-- | shellutils.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/shellutils.py b/shellutils.py index 853e7f7..4c6e8a8 100644 --- a/shellutils.py +++ b/shellutils.py @@ -42,6 +42,31 @@ except ImportError: raise NoSuchProcess() +class tempdir(object): + + def __enter__(self): + self.path = tempfile.mkdtemp() + return self.path + + def __exit__(self, exctype, value, traceback): + # 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) + + def chown(path, login=None, group=None): """Same as `os.chown` function but accepting user login or group name as argument. If login or group is omitted, it's left unchanged. |