From ef81730d8a157d650bf484f0fc8fb0c2edde5eb3 Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Fri, 21 Jun 2019 20:36:09 -0700 Subject: [PATCH] Python 2.7 doesn't have TemporaryDirectory --- build.py | 7 ++++--- buildtools/config.py | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/build.py b/build.py index b3c7e340..a9a2c1e2 100755 --- a/build.py +++ b/build.py @@ -34,7 +34,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 @@ -837,7 +838,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 @@ -846,7 +847,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 diff --git a/buildtools/config.py b/buildtools/config.py index a1030768..ec7ae1f1 100644 --- a/buildtools/config.py +++ b/buildtools/config.py @@ -970,3 +970,21 @@ def updateLicenseFiles(cfg): text += f.read() + '\n\n' 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)