Skip to content

Commit

Permalink
move TemporaryDirectory from conda.common into gateways.disk.create (c…
Browse files Browse the repository at this point in the history
…onda#8671)

move TemporaryDirectory from conda.common into gateways.disk.create
  • Loading branch information
msarahan authored May 14, 2019
2 parents 63fa8ce + b0c13e4 commit 078fb4e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 49 deletions.
44 changes: 0 additions & 44 deletions conda/common/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,47 +295,3 @@ def ensure_utf8_encoding(value):
return value
except UnicodeEncodeError:
return value


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 everything contained
in it are removed.
"""

# Handle mkdtemp raising an exception
name = None
_closed = False

def __init__(self, suffix="", prefix='tmp', dir=None):
self.name = mkdtemp(suffix, prefix, dir)

def __repr__(self):
return "<{} {!r}>".format(self.__class__.__name__, self.name)

def __enter__(self):
return self.name

def cleanup(self, _warn=False, _warnings=_warnings):
from ..gateways.disk.delete import rm_rf as _rm_rf
if self.name and not self._closed:
try:
_rm_rf(self.name)
except (TypeError, AttributeError) as ex:
if "None" not in '%s' % (ex,):
raise
_rm_rf(self.name)
self._closed = True

def __exit__(self, exc, value, tb):
self.cleanup()

def __del__(self):
# Issue a ResourceWarning if implicit cleanup needed
self.cleanup(_warn=True)
6 changes: 4 additions & 2 deletions conda/exports.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@

from .common import compat as _compat # NOQA
compat = _compat
from .common.compat import PY3, StringIO, input, iteritems, on_win, string_types, text_type, itervalues, TemporaryDirectory # NOQA
PY3, StringIO, input, iteritems, string_types, text_type, TemporaryDirectory = PY3, StringIO, input, iteritems, string_types, text_type, TemporaryDirectory # NOQA
from .common.compat import PY3, StringIO, input, iteritems, on_win, string_types, text_type, itervalues # NOQA
PY3, StringIO, input, iteritems, string_types, text_type, = PY3, StringIO, input, iteritems, string_types, text_type # NOQA
from .gateways.connection.session import CondaSession # NOQA
CondaSession = CondaSession
from .gateways.disk.create import TemporaryDirectory # NOQA
TemporaryDirectory = TemporaryDirectory

from .common.toposort import _toposort # NOQA
_toposort = _toposort
Expand Down
48 changes: 48 additions & 0 deletions conda/gateways/disk/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from shutil import copyfileobj, copystat
import sys
import tempfile
import warnings as _warnings

import conda_package_handling.api

Expand All @@ -30,6 +31,53 @@
from ...exceptions import BasicClobberError, CondaOSError, maybe_raise
from ...models.enums import FileMode, LinkType

try:
from tempfile import TemporaryDirectory
except ImportError:
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 everything contained
in it are removed.
"""

# Handle mkdtemp raising an exception
name = None
_closed = False

def __init__(self, suffix="", prefix='tmp', dir=None):
self.name = tempfile.mkdtemp(suffix, prefix, dir)

def __repr__(self):
return "<{} {!r}>".format(self.__class__.__name__, self.name)

def __enter__(self):
return self.name

def cleanup(self, _warn=False, _warnings=_warnings):
from .delete import rm_rf as _rm_rf
if self.name and not self._closed:
try:
_rm_rf(self.name)
except (TypeError, AttributeError) as ex:
if "None" not in '%s' % (ex,):
raise
_rm_rf(self.name)
self._closed = True

def __exit__(self, exc, value, tb):
self.cleanup()

def __del__(self):
# Issue a ResourceWarning if implicit cleanup needed
self.cleanup(_warn=True)


log = getLogger(__name__)
stdoutlog = getLogger('conda.stdoutlog')

Expand Down
2 changes: 1 addition & 1 deletion conda/gateways/disk/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import conda_package_handling.api

from .link import islink, lexists
from ...common.compat import TemporaryDirectory
from .create import TemporaryDirectory
from ..._vendor.auxlib.collection import first
from ..._vendor.auxlib.compat import shlex_split_unicode
from ..._vendor.auxlib.ish import dals
Expand Down
3 changes: 1 addition & 2 deletions tests/gateways/disk/test_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@

import pytest

from conda.compat import TemporaryDirectory
from conda.common.compat import on_win
from conda.gateways.disk.create import create_link, mkdir_p
from conda.gateways.disk.create import create_link, mkdir_p, TemporaryDirectory
from conda.gateways.disk.delete import move_to_trash, rm_rf
from conda.gateways.disk.link import islink, symlink
from conda.gateways.disk.test import softlink_supported
Expand Down

0 comments on commit 078fb4e

Please sign in to comment.