Example #1
0
    def get_dist_to_uninstall(self, candidate):
        # type: (Candidate) -> Optional[Distribution]
        # TODO: Are there more cases this needs to return True? Editable?
        dist = self._installed_dists.get(candidate.name)
        if dist is None:  # Not installed, no uninstallation required.
            return None

        # We're installing into global site. The current installation must
        # be uninstalled, no matter it's in global or user site, because the
        # user site installation has precedence over global.
        if not self._use_user_site:
            return dist

        # We're installing into user site. Remove the user site installation.
        if dist_in_usersite(dist):
            return dist

        # We're installing into user site, but the installed incompatible
        # package is in global site. We can't uninstall that, and would let
        # the new user installation to "shadow" it. But shadowing won't work
        # in virtual environments, so we error out.
        if running_under_virtualenv() and dist_in_site_packages(dist):
            raise InstallationError(
                "Will not install to the user site because it will "
                "lack sys.path precedence to {} in {}".format(
                    dist.project_name,
                    dist.location,
                ))
        return None
Example #2
0
    def check_if_exists(self, use_user_site):
        # type: (bool) -> None
        """Find an installed distribution that satisfies or conflicts
        with this requirement, and set self.satisfied_by or
        self.should_reinstall appropriately.
        """
        if self.req is None:
            return
        existing_dist = get_distribution(self.req.name)
        if not existing_dist:
            return

        existing_version = existing_dist.parsed_version
        if not self.req.specifier.contains(existing_version, prereleases=True):
            self.satisfied_by = None
            if use_user_site:
                if dist_in_usersite(existing_dist):
                    self.should_reinstall = True
                elif (running_under_virtualenv() and
                        dist_in_site_packages(existing_dist)):
                    raise InstallationError(
                        "Will not install to the user site because it will "
                        "lack sys.path precedence to {} in {}".format(
                            existing_dist.project_name, existing_dist.location)
                    )
            else:
                self.should_reinstall = True
        else:
            if self.editable:
                self.should_reinstall = True
                # when installing editables, nothing pre-existing should ever
                # satisfy
                self.satisfied_by = None
            else:
                self.satisfied_by = existing_dist
Example #3
0
    def check_if_exists(self, use_user_site):
        # type: (bool) -> None
        """Find an installed distribution that satisfies or conflicts
        with this requirement, and set self.satisfied_by or
        self.should_reinstall appropriately.
        """
        if self.req is None:
            return
        # get_distribution() will resolve the entire list of requirements
        # anyway, and we've already determined that we need the requirement
        # in question, so strip the marker so that we don't try to
        # evaluate it.
        no_marker = Requirement(str(self.req))
        no_marker.marker = None

        # pkg_resources uses the canonical name to look up packages, but
        # the name passed passed to get_distribution is not canonicalized
        # so we have to explicitly convert it to a canonical name
        no_marker.name = canonicalize_name(no_marker.name)
        try:
            self.satisfied_by = pkg_resources.get_distribution(str(no_marker))
        except pkg_resources.DistributionNotFound:
            return
        except pkg_resources.VersionConflict:
            existing_dist = get_distribution(
                self.req.name
            )
            if use_user_site:
                if dist_in_usersite(existing_dist):
                    self.should_reinstall = True
                elif (running_under_virtualenv() and
                      dist_in_site_packages(existing_dist)):
                    raise InstallationError(
                        "Will not install to the user site because it will "
                        "lack sys.path precedence to {} in {}".format(
                            existing_dist.project_name, existing_dist.location)
                    )
            else:
                self.should_reinstall = True
        else:
            if self.editable and self.satisfied_by:
                self.should_reinstall = True
                # when installing editables, nothing pre-existing should ever
                # satisfy
                self.satisfied_by = None
Example #4
0
    def check_if_exists(self, use_user_site):
        # type: (bool) -> None
        """Find an installed distribution that satisfies or conflicts
        with this requirement, and set self.satisfied_by or
        self.should_reinstall appropriately.
        """
        if self.req is None:
            return
        existing_dist = get_distribution(self.req.name)
        if not existing_dist:
            return

        # pkg_resouces may contain a different copy of packaging.version from
        # pip in if the downstream distributor does a poor job debundling pip.
        # We avoid existing_dist.parsed_version and let SpecifierSet.contains
        # parses the version instead.
        existing_version = existing_dist.version
        version_compatible = (
            existing_version is not None and
            self.req.specifier.contains(existing_version, prereleases=True)
        )
        if not version_compatible:
            self.satisfied_by = None
            if use_user_site:
                if dist_in_usersite(existing_dist):
                    self.should_reinstall = True
                elif (running_under_virtualenv() and
                        dist_in_site_packages(existing_dist)):
                    raise InstallationError(
                        "Will not install to the user site because it will "
                        "lack sys.path precedence to {} in {}".format(
                            existing_dist.project_name, existing_dist.location)
                    )
            else:
                self.should_reinstall = True
        else:
            if self.editable:
                self.should_reinstall = True
                # when installing editables, nothing pre-existing should ever
                # satisfy
                self.satisfied_by = None
            else:
                self.satisfied_by = existing_dist
Example #5
0
 def check_if_exists(self, use_user_site):
     # type: (bool) -> bool
     """Find an installed distribution that satisfies or conflicts
     with this requirement, and set self.satisfied_by or
     self.conflicts_with appropriately.
     """
     if self.req is None:
         return False
     # get_distribution() will resolve the entire list of requirements
     # anyway, and we've already determined that we need the requirement
     # in question, so strip the marker so that we don't try to
     # evaluate it.
     no_marker = Requirement(str(self.req))
     no_marker.marker = None
     try:
         self.satisfied_by = pkg_resources.get_distribution(str(no_marker))
     except pkg_resources.DistributionNotFound:
         return False
     except pkg_resources.VersionConflict:
         existing_dist = pkg_resources.get_distribution(
             self.req.name
         )
         if use_user_site:
             if dist_in_usersite(existing_dist):
                 self.conflicts_with = existing_dist
             elif (running_under_virtualenv() and
                     dist_in_site_packages(existing_dist)):
                 raise InstallationError(
                     "Will not install to the user site because it will "
                     "lack sys.path precedence to %s in %s" %
                     (existing_dist.project_name, existing_dist.location)
                 )
         else:
             self.conflicts_with = existing_dist
     else:
         if self.editable and self.satisfied_by:
             self.conflicts_with = self.satisfied_by
             # when installing editables, nothing pre-existing should ever
             # satisfy
             self.satisfied_by = None
             return True
     return True
Example #6
0
 def check_if_exists(self, use_user_site):
     # type: (bool) -> bool
     """Find an installed distribution that satisfies or conflicts
     with this requirement, and set self.satisfied_by or
     self.conflicts_with appropriately.
     """
     if self.req is None:
         return False
     try:
         # get_distribution() will resolve the entire list of requirements
         # anyway, and we've already determined that we need the requirement
         # in question, so strip the marker so that we don't try to
         # evaluate it.
         no_marker = Requirement(str(self.req))
         no_marker.marker = None
         self.satisfied_by = pkg_resources.get_distribution(str(no_marker))
         if self.editable and self.satisfied_by:
             self.conflicts_with = self.satisfied_by
             # when installing editables, nothing pre-existing should ever
             # satisfy
             self.satisfied_by = None
             return True
     except pkg_resources.DistributionNotFound:
         return False
     except pkg_resources.VersionConflict:
         existing_dist = pkg_resources.get_distribution(
             self.req.name
         )
         if use_user_site:
             if dist_in_usersite(existing_dist):
                 self.conflicts_with = existing_dist
             elif (running_under_virtualenv() and
                     dist_in_site_packages(existing_dist)):
                 raise InstallationError(
                     "Will not install to the user site because it will "
                     "lack sys.path precedence to %s in %s" %
                     (existing_dist.project_name, existing_dist.location)
                 )
         else:
             self.conflicts_with = existing_dist
     return True
Example #7
0
 def in_site_packages(self) -> bool:
     return misc.dist_in_site_packages(self._dist)