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 | |
parent | d9ed46f8530856a9d8b1ee91fa5aa39175540f4c (diff) | |
download | logilab-common-7d0cd87c0bf2c2460a04975dc48d9e737598b824.tar.gz |
move contexts.py context managers into shellutils.py and deprecate this module
-rw-r--r-- | contexts.py | 57 | ||||
-rw-r--r-- | shellutils.py | 25 |
2 files changed, 29 insertions, 53 deletions
diff --git a/contexts.py b/contexts.py index 83c9603..d78c327 100644 --- a/contexts.py +++ b/contexts.py @@ -1,54 +1,5 @@ -# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of logilab-common. -# -# logilab-common is free software: you can redistribute it and/or modify it under -# the terms of the GNU Lesser General Public License as published by the Free -# Software Foundation, either version 2.1 of the License, or (at your option) any -# later version. -# -# logilab-common is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more -# details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with logilab-common. If not, see <http://www.gnu.org/licenses/>. -"""A few useful context managers""" - -__docformat__ = "restructuredtext en" - -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 - -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) +from warnings import warn +warn('logilab.common.contexts module is deprecated, use logilab.common.shellutils instead', + DeprecationWarning, stacklevel=1) +from logilab.common.shellutils import tempfile, pushd 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. |