Use tempfile.TemporaryDirectory() with Python >=3.2.

parent 0daefd2b
...@@ -9,3 +9,29 @@ except ImportError: ...@@ -9,3 +9,29 @@ except ImportError:
if name not in ('platlib', 'purelib'): if name not in ('platlib', 'purelib'):
raise ValueError("Name must be purelib or platlib") raise ValueError("Name must be purelib or platlib")
return get_python_lib(name=='platlib') return get_python_lib(name=='platlib')
try:
# Python >=3.2
from tempfile import TemporaryDirectory
except ImportError:
import shutil
import tempfile
class TemporaryDirectory(object):
""""
Very simple temporary directory context manager.
Will try to delete afterward, but will also ignore OS and similar
errors on deletion.
"""
def __init__(self):
self.name = None # Handle mkdtemp raising an exception
self.name = tempfile.mkdtemp()
def __enter__(self):
return self.name
def __exit__(self, exctype, excvalue, exctrace):
try:
shutil.rmtree(self.name, True)
except OSError: #removal errors are not the only possible
pass
self.name = None
...@@ -5,12 +5,12 @@ from distutils import log ...@@ -5,12 +5,12 @@ from distutils import log
import xml.dom.pulldom import xml.dom.pulldom
import shlex import shlex
import shutil import shutil
import tempfile
import locale import locale
import codecs import codecs
import unicodedata import unicodedata
import warnings import warnings
from setuptools.compat import unicode from setuptools.compat import unicode
from setuptools.py31compat import TemporaryDirectory
from xml.sax.saxutils import unescape from xml.sax.saxutils import unescape
try: try:
...@@ -29,26 +29,6 @@ from subprocess import Popen as _Popen, PIPE as _PIPE ...@@ -29,26 +29,6 @@ from subprocess import Popen as _Popen, PIPE as _PIPE
# http://stackoverflow.com/questions/5658622/ # http://stackoverflow.com/questions/5658622/
# python-subprocess-popen-environment-path # python-subprocess-popen-environment-path
class TempDir(object):
""""
Very simple temporary directory context manager.
Will try to delete afterward, but will also ignore OS and similar
errors on deletion.
"""
def __init__(self):
self.path = None
def __enter__(self):
self.path = tempfile.mkdtemp()
return self
def __exit__(self, exctype, excvalue, exctrace):
try:
shutil.rmtree(self.path, True)
except OSError: #removal errors are not the only possible
pass
self.path = None
def _run_command(args, stdout=_PIPE, stderr=_PIPE, encoding=None, stream=0): def _run_command(args, stdout=_PIPE, stderr=_PIPE, encoding=None, stream=0):
#regarding the shell argument, see: http://bugs.python.org/issue8557 #regarding the shell argument, see: http://bugs.python.org/issue8557
try: try:
...@@ -256,9 +236,9 @@ class SvnInfo(object): ...@@ -256,9 +236,9 @@ class SvnInfo(object):
# This is needed because .svn always creates .subversion and # This is needed because .svn always creates .subversion and
# some operating systems do not handle dot directory correctly. # some operating systems do not handle dot directory correctly.
# Real queries in real svn repos with be concerned with it creation # Real queries in real svn repos with be concerned with it creation
with TempDir() as tempdir: with TemporaryDirectory() as tempdir:
code, data = _run_command(['svn', code, data = _run_command(['svn',
'--config-dir', tempdir.path, '--config-dir', tempdir,
'--version', '--version',
'--quiet']) '--quiet'])
...@@ -282,9 +262,9 @@ class SvnInfo(object): ...@@ -282,9 +262,9 @@ class SvnInfo(object):
# This is needed because .svn always creates .subversion and # This is needed because .svn always creates .subversion and
# some operating systems do not handle dot directory correctly. # some operating systems do not handle dot directory correctly.
# Real queries in real svn repos with be concerned with it creation # Real queries in real svn repos with be concerned with it creation
with TempDir() as tempdir: with TemporaryDirectory() as tempdir:
code, data = _run_command(['svn', code, data = _run_command(['svn',
'--config-dir', tempdir.path, '--config-dir', tempdir,
'info', normdir]) 'info', normdir])
# Must check for some contents, as some use empty directories # Must check for some contents, as some use empty directories
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment