Skip to content

Commit

Permalink
dev(hansbug): add backport support for tempfile in Python3.10
Browse files Browse the repository at this point in the history
  • Loading branch information
HansBug committed Oct 27, 2023
1 parent aaf9278 commit ce336bb
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
77 changes: 76 additions & 1 deletion hbutils/system/filesystem/tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import platform
import shutil
import tempfile
import types
import warnings
import weakref

Expand All @@ -15,8 +16,82 @@

_python_version_tuple = tuple(map(int, platform.python_version_tuple()))

if _python_version_tuple >= (3, 8):
if _python_version_tuple >= (3, 10):
from tempfile import TemporaryDirectory

elif _python_version_tuple >= (3, 8):
class TemporaryDirectory:
"""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.
"""

def __init__(self, suffix=None, prefix=None, dir=None,
ignore_cleanup_errors=False):
self.name = tempfile.mkdtemp(suffix, prefix, dir)
self._ignore_cleanup_errors = ignore_cleanup_errors
self._finalizer = weakref.finalize(
self, self._cleanup, self.name,
warn_message="Implicitly cleaning up {!r}".format(self),
ignore_errors=self._ignore_cleanup_errors)

@classmethod
def _rmtree(cls, name, ignore_errors=False):
def onerror(func, path, exc_info):
if issubclass(exc_info[0], PermissionError):
def resetperms(path):
try:
os.chflags(path, 0)
except AttributeError:
pass
os.chmod(path, 0o700)

try:
if path != name:
resetperms(os.path.dirname(path))
resetperms(path)

try:
os.unlink(path)
# PermissionError is raised on FreeBSD for directories
except (IsADirectoryError, PermissionError):
cls._rmtree(path, ignore_errors=ignore_errors)
except FileNotFoundError:
pass
elif issubclass(exc_info[0], FileNotFoundError):
pass

Check warning on line 68 in hbutils/system/filesystem/tempfile.py

View check run for this annotation

Codecov / codecov/patch

hbutils/system/filesystem/tempfile.py#L63-L68

Added lines #L63 - L68 were not covered by tests
else:
if not ignore_errors:
raise

Check warning on line 71 in hbutils/system/filesystem/tempfile.py

View check run for this annotation

Codecov / codecov/patch

hbutils/system/filesystem/tempfile.py#L70-L71

Added lines #L70 - L71 were not covered by tests

shutil.rmtree(name, onerror=onerror)

@classmethod
def _cleanup(cls, name, warn_message, ignore_errors=False):
cls._rmtree(name, ignore_errors=ignore_errors)
warnings.warn(warn_message, ResourceWarning)

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

def __enter__(self):
return self.name

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

def cleanup(self):
if self._finalizer.detach() or os.path.exists(self.name):
self._rmtree(self.name, ignore_errors=self._ignore_cleanup_errors)

__class_getitem__ = classmethod(types.GenericAlias)

else:
class TemporaryDirectory(object):
"""
Expand Down
2 changes: 1 addition & 1 deletion requirements-doc.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Jinja2~=3.0.0
sphinx~=3.2.0
sphinx_rtd_theme~=0.4.3
enum_tools
enum_tools~=0.9.0
sphinx-toolbox
plantumlcli>=0.0.2
packaging
Expand Down

0 comments on commit ce336bb

Please sign in to comment.