You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
838 B

4 years ago
  1. __all__ = []
  2. __metaclass__ = type
  3. try:
  4. # Python >=3.2
  5. from tempfile import TemporaryDirectory
  6. except ImportError:
  7. import shutil
  8. import tempfile
  9. class TemporaryDirectory:
  10. """
  11. Very simple temporary directory context manager.
  12. Will try to delete afterward, but will also ignore OS and similar
  13. errors on deletion.
  14. """
  15. def __init__(self, **kwargs):
  16. self.name = None # Handle mkdtemp raising an exception
  17. self.name = tempfile.mkdtemp(**kwargs)
  18. def __enter__(self):
  19. return self.name
  20. def __exit__(self, exctype, excvalue, exctrace):
  21. try:
  22. shutil.rmtree(self.name, True)
  23. except OSError: # removal errors are not the only possible
  24. pass
  25. self.name = None