Unconditionally create TempDirectory.path

This commit is contained in:
Chris Hunt 2019-09-19 22:32:23 -04:00
parent dd62731107
commit 85dcaa74bb
8 changed files with 16 additions and 38 deletions

View File

@ -54,7 +54,6 @@ class BuildEnvironment(object):
def __init__(self):
# type: () -> None
self._temp_dir = TempDirectory(kind="build-env")
self._temp_dir.create()
self._prefixes = OrderedDict((
(name, _Prefix(os.path.join(self._temp_dir.path, name)))

View File

@ -193,7 +193,6 @@ class EphemWheelCache(SimpleWheelCache):
def __init__(self, format_control):
# type: (FormatControl) -> None
self._temp_dir = TempDirectory(kind="ephem-wheel-cache")
self._temp_dir.create()
super(EphemWheelCache, self).__init__(
self._temp_dir.path, format_control

View File

@ -318,7 +318,6 @@ class InstallCommand(RequirementCommand):
# Create a target directory for using with the target option
target_temp_dir = TempDirectory(kind="target")
target_temp_dir.create()
target_temp_dir_path = target_temp_dir.path
install_options.append('--home=' + target_temp_dir_path)

View File

@ -338,7 +338,6 @@ class InstallRequirement(object):
# builds (such as numpy). Thus, we ensure that the real path
# is returned.
self._temp_build_dir = TempDirectory(kind="req-build")
self._temp_build_dir.create()
self._ideal_build_dir = build_dir
return self._temp_build_dir.path
@ -606,7 +605,6 @@ class InstallRequirement(object):
# NOTE: This needs to be refactored to stop using atexit
self._temp_dir = TempDirectory(delete=False, kind="req-install")
self._temp_dir.create()
metadata_dir = os.path.join(
self._temp_dir.path,
'pip-wheel-metadata',

View File

@ -28,7 +28,6 @@ class RequirementTracker(object):
self._root = os.environ.get('PIP_REQ_TRACKER')
if self._root is None:
self._temp_dir = TempDirectory(delete=False, kind='req-tracker')
self._temp_dir.create()
self._root = os.environ['PIP_REQ_TRACKER'] = self._temp_dir.path
logger.debug('Created requirements tracker %r', self._root)
else:

View File

@ -227,10 +227,8 @@ class StashedUninstallPathSet(object):
try:
save_dir = AdjacentTempDirectory(path) # type: TempDirectory
save_dir.create()
except OSError:
save_dir = TempDirectory(kind="uninstall")
save_dir.create()
self._save_dirs[os.path.normcase(path)] = save_dir
return save_dir.path
@ -256,7 +254,6 @@ class StashedUninstallPathSet(object):
# Did not find any suitable root
head = os.path.dirname(path)
save_dir = TempDirectory(kind='uninstall')
save_dir.create()
self._save_dirs[head] = save_dir
relpath = os.path.relpath(path, head)

View File

@ -19,21 +19,17 @@ class TempDirectory(object):
Attributes:
path
Location to the created temporary directory or None
Location to the created temporary directory
delete
Whether the directory should be deleted when exiting
(when used as a contextmanager)
Methods:
create()
Creates a temporary directory and stores its path in the path
attribute.
cleanup()
Deletes the temporary directory and sets path attribute to None
Deletes the temporary directory
When used as a context manager, a temporary directory is created on
entering the context and, if the delete attribute is True, on exiting the
context the created directory is deleted.
When used as a context manager, if the delete attribute is True, on
exiting the context the temporary directory is deleted.
"""
def __init__(self, path=None, delete=None, kind="temp"):
@ -44,6 +40,9 @@ class TempDirectory(object):
# an explicit delete option, then we'll default to deleting.
delete = True
if path is None:
path = self._create(kind)
self.path = path
self.delete = delete
self.kind = kind
@ -52,30 +51,21 @@ class TempDirectory(object):
return "<{} {!r}>".format(self.__class__.__name__, self.path)
def __enter__(self):
self.create()
return self
def __exit__(self, exc, value, tb):
if self.delete:
self.cleanup()
def create(self):
self.path = self._create()
def _create(self):
def _create(self, kind):
"""Create a temporary directory and store its path in self.path
"""
if self.path is not None:
logger.debug(
"Skipped creation of temporary directory: {}".format(self.path)
)
return self.path
# We realpath here because some systems have their default tmpdir
# symlinked to another directory. This tends to confuse build
# scripts, so we canonicalize the path by traversing potential
# symlinks here.
path = os.path.realpath(
tempfile.mkdtemp(prefix="pip-{}-".format(self.kind))
tempfile.mkdtemp(prefix="pip-{}-".format(kind))
)
logger.debug("Created temporary directory: {}".format(path))
return path
@ -83,9 +73,8 @@ class TempDirectory(object):
def cleanup(self):
"""Remove the temporary directory created and reset state
"""
if self.path is not None and os.path.exists(self.path):
if os.path.exists(self.path):
rmtree(self.path)
self.path = None
class AdjacentTempDirectory(TempDirectory):
@ -110,8 +99,8 @@ class AdjacentTempDirectory(TempDirectory):
LEADING_CHARS = "-~.=%0123456789"
def __init__(self, original, delete=None):
super(AdjacentTempDirectory, self).__init__(delete=delete)
self.original = original.rstrip('/\\')
super(AdjacentTempDirectory, self).__init__(delete=delete)
@classmethod
def _generate_names(cls, name):
@ -137,7 +126,7 @@ class AdjacentTempDirectory(TempDirectory):
if new_name != name:
yield new_name
def _create(self):
def _create(self, kind):
root, name = os.path.split(self.original)
for candidate in self._generate_names(name):
path = os.path.join(root, candidate)
@ -153,7 +142,7 @@ class AdjacentTempDirectory(TempDirectory):
else:
# Final fallback on the default behavior.
path = os.path.realpath(
tempfile.mkdtemp(prefix="pip-{}-".format(self.kind))
tempfile.mkdtemp(prefix="pip-{}-".format(kind))
)
logger.debug("Created temporary directory: {}".format(path))

View File

@ -588,19 +588,17 @@ class TestTempDirectory(object):
create_file(tmp_dir.path, "subfolder", "readonly-file")
readonly_file(tmp_dir.path, "subfolder", "readonly-file")
assert tmp_dir.path is None
assert not os.path.exists(tmp_dir.path)
def test_create_and_cleanup_work(self):
tmp_dir = TempDirectory()
assert tmp_dir.path is None
tmp_dir.create()
created_path = tmp_dir.path
assert tmp_dir.path is not None
assert os.path.exists(created_path)
tmp_dir.cleanup()
assert tmp_dir.path is None
assert tmp_dir.path is not None
assert not os.path.exists(created_path)
@pytest.mark.parametrize("name", [