Python 2.7 doesn't have TemporaryDirectory

(cherry picked from commit ef81730d8a)
This commit is contained in:
Robin Dunn
2019-06-21 20:36:09 -07:00
parent 6dab1307fa
commit 03ee6dcd95
2 changed files with 21 additions and 3 deletions

View File

@@ -43,7 +43,8 @@ from buildtools.config import Config, msg, opj, posixjoin, loadETG, etg2sip, fi
phoenixDir, wxDir, copyIfNewer, copyFile, \
macSetLoaderNames, \
getVcsRev, runcmd, textfile_open, getSipFiles, \
getVisCVersion, getToolsPlatformName, updateLicenseFiles
getVisCVersion, getToolsPlatformName, updateLicenseFiles, \
TemporaryDirectory
import buildtools.version as version
@@ -890,7 +891,7 @@ def do_regenerate_sysconfig():
TODO: Can this be done in a way that doesn't require overwriting a file in
the environment?
"""
with tempfile.TemporaryDirectory() as td:
with TemporaryDirectory() as td:
pwd = pushDir(td)
# generate a new sysconfig data file
@@ -899,7 +900,7 @@ def do_regenerate_sysconfig():
# On success the new data module will have been written to a subfolder
# of the current folder, which is recorded in ./pybuilddir.tx
with open('pybuilddir.txt', 'r', encoding='utf-8') as fp:
with open('pybuilddir.txt', 'r') as fp:
pybd = fp.read()
# grab the file in that folder and copy it into the Python lib

View File

@@ -971,3 +971,20 @@ def updateLicenseFiles(cfg):
with open('LICENSE.txt', 'w') as f:
f.write(text)
try:
from tempfile import TemporaryDirectory
except ImportError:
from tempfile import mkdtemp
class TemporaryDirectory(object):
def __init__(self, suffix='', prefix='tmp', dir=None):
self.name = mkdtemp(suffix, prefix, dir)
def __enter__(self):
return self.name
def __exit__(self, exc, value, tb):
self.cleanup()
def cleanup(self):
shutil.rmtree(self.name)