Skip to content

Commit

Permalink
Improve move_to_correct_build_directory
Browse files Browse the repository at this point in the history
Why: Changes made are IMO much clearer, with a step by step flow to this
method that is easier to reason about.
  • Loading branch information
pradyunsg committed Sep 26, 2019
1 parent 5d74967 commit c4076ea
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/pip/_internal/req/req_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ def __init__(
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
Expand Down Expand Up @@ -367,29 +366,34 @@ def move_to_correct_build_directory(self):
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
Expand All @@ -398,6 +402,11 @@ def move_to_correct_build_directory(self):
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
Expand Down

0 comments on commit c4076ea

Please sign in to comment.