Improve move_to_correct_build_directory

Why: Changes made are IMO much clearer, with a step by step flow to this
method that is easier to reason about.
This commit is contained in:
Pradyun Gedam 2019-09-27 02:25:35 +05:30
parent b01f301c75
commit ec5e9d30d3
No known key found for this signature in database
GPG Key ID: DA17C4B29CB32E4B
1 changed files with 18 additions and 9 deletions

View File

@ -121,7 +121,6 @@ class InstallRequirement(object):
markers = req.marker
self.markers = markers
self._egg_info_path = None # type: Optional[str]
# This holds the pkg_resources.Distribution object if this requirement
# is already available:
self.satisfied_by = None
@ -367,29 +366,34 @@ class InstallRequirement(object):
return
assert self.req is not None
assert self._temp_build_dir
assert (self._ideal_build_dir is not None and
self._ideal_build_dir.path) # type: ignore
assert (
self._ideal_build_dir is not None and
self._ideal_build_dir.path # type: ignore
)
old_location = self._temp_build_dir
self._temp_build_dir = None
self._temp_build_dir = None # checked inside ensure_build_location
# Figure out the correct place to put the files.
new_location = self.ensure_build_location(self._ideal_build_dir)
if os.path.exists(new_location):
raise InstallationError(
'A package already exists in %s; please remove it to continue'
% display_path(new_location))
% display_path(new_location)
)
# Move the files to the correct location.
logger.debug(
'Moving package %s from %s to new location %s',
self, display_path(old_location.path), display_path(new_location),
)
shutil.move(old_location.path, new_location)
# Update directory-tracking variables, to be in line with new_location
self.source_dir = os.path.normpath(os.path.abspath(new_location))
self._temp_build_dir = TempDirectory(
path=new_location, kind="req-install",
)
self._ideal_build_dir = None
self.source_dir = os.path.normpath(os.path.abspath(new_location))
self._egg_info_path = None
# Correct the metadata directory, if it exists
if self.metadata_directory:
old_meta = self.metadata_directory
@ -398,6 +402,11 @@ class InstallRequirement(object):
new_meta = os.path.normpath(os.path.abspath(new_meta))
self.metadata_directory = new_meta
# Done with any "move built files" work, since have moved files to the
# "ideal" build location. Setting to None allows to clearly flag that
# no more moves are needed.
self._ideal_build_dir = None
def remove_temporary_source(self):
# type: () -> None
"""Remove the source files from this requirement, if they are marked