/usr/share/pyshared/nipype/utils/tmpdirs.py is in python-nipype 0.9.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
import os
import shutil
from tempfile import template, mkdtemp
class TemporaryDirectory(object):
"""Create and return a temporary directory. This has the same
behavior as mkdtemp but can be used as a context manager. For
example:
with TemporaryDirectory() as tmpdir:
...
Upon exiting the context, the directory and everthing contained
in it are removed.
"""
def __init__(self, suffix="", prefix=template, dir=None):
self.name = mkdtemp(suffix, prefix, dir)
self._closed = False
def __enter__(self):
return self.name
def cleanup(self):
if not self._closed:
shutil.rmtree(self.name)
self._closed = True
def __exit__(self, exc, value, tb):
self.cleanup()
return False
class InTemporaryDirectory(TemporaryDirectory):
def __enter__(self):
self._pwd = os.getcwd()
os.chdir(self.name)
return super(InTemporaryDirectory, self).__enter__()
def __exit__(self, exc, value, tb):
os.chdir(self._pwd)
return super(InTemporaryDirectory, self).__exit__(exc, value, tb)
|