1
1
Fork 0
mirror of https://github.com/pypa/pip synced 2023-12-13 21:30:23 +01:00

Move InstallRequirement.from_req to constructors module

This commit is contained in:
Pradyun Gedam 2018-08-21 20:41:36 +05:30
parent a5a07fe61c
commit 688bc1ee6f
No known key found for this signature in database
GPG key ID: DA17C4B29CB32E4B
3 changed files with 29 additions and 27 deletions

View file

@ -22,6 +22,7 @@ from pip._internal.download import (
is_archive_file, is_url, path_to_url, url_to_path,
)
from pip._internal.exceptions import InstallationError
from pip._internal.models.index import PyPI, TestPyPI
from pip._internal.models.link import Link
from pip._internal.req.req_install import InstallRequirement
from pip._internal.utils.misc import is_installable_dir
@ -270,3 +271,28 @@ def install_req_from_line(
constraint=constraint,
extras=extras,
)
def install_req_from_req(
req, comes_from=None, isolated=False, wheel_cache=None
):
try:
req = Requirement(req)
except InvalidRequirement:
raise InstallationError("Invalid requirement: '%s'" % req)
domains_not_allowed = [
PyPI.file_storage_domain,
TestPyPI.file_storage_domain,
]
if req.url and comes_from.link.netloc in domains_not_allowed:
# Explicitly disallow pypi packages that depend on external urls
raise InstallationError(
"Packages installed from PyPI cannot depend on packages "
"which are not also hosted on PyPI.\n"
"%s depends on %s " % (comes_from.name, req)
)
return InstallRequirement(
req, comes_from, isolated=isolated, wheel_cache=wheel_cache
)

View file

@ -9,7 +9,7 @@ import zipfile
from distutils.util import change_root
from pip._vendor import pkg_resources, six
from pip._vendor.packaging.requirements import InvalidRequirement, Requirement
from pip._vendor.packaging.requirements import Requirement
from pip._vendor.packaging.utils import canonicalize_name
from pip._vendor.packaging.version import Version
from pip._vendor.packaging.version import parse as parse_version
@ -21,7 +21,6 @@ from pip._internal.exceptions import InstallationError
from pip._internal.locations import (
PIP_DELETE_MARKER_FILENAME, running_under_virtualenv,
)
from pip._internal.models.index import PyPI, TestPyPI
from pip._internal.models.link import Link
from pip._internal.pyproject import load_pyproject_toml
from pip._internal.req.req_uninstall import UninstallPathSet
@ -125,29 +124,6 @@ class InstallRequirement(object):
# but after loading this flag should be treated as read only.
self.use_pep517 = None
# Constructors
# TODO: Move these out of this class into custom methods.
@classmethod
def from_req(cls, req, comes_from=None, isolated=False, wheel_cache=None):
try:
req = Requirement(req)
except InvalidRequirement:
raise InstallationError("Invalid requirement: '%s'" % req)
domains_not_allowed = [
PyPI.file_storage_domain,
TestPyPI.file_storage_domain,
]
if req.url and comes_from.link.netloc in domains_not_allowed:
# Explicitly disallow pypi packages that depend on external urls
raise InstallationError(
"Packages installed from PyPI cannot depend on packages "
"which are not also hosted on PyPI.\n"
"%s depends on %s " % (comes_from.name, req)
)
return cls(req, comes_from, isolated=isolated, wheel_cache=wheel_cache)
def __str__(self):
if self.req:
s = str(self.req)

View file

@ -18,7 +18,7 @@ from pip._internal.exceptions import (
BestVersionAlreadyInstalled, DistributionNotFound, HashError, HashErrors,
UnsupportedPythonVersion,
)
from pip._internal.req.req_install import InstallRequirement
from pip._internal.req.constructors import install_req_from_req
from pip._internal.utils.logging import indent_log
from pip._internal.utils.misc import dist_in_usersite, ensure_dir
from pip._internal.utils.packaging import check_dist_requires_python
@ -268,7 +268,7 @@ class Resolver(object):
more_reqs = []
def add_req(subreq, extras_requested):
sub_install_req = InstallRequirement.from_req(
sub_install_req = install_req_from_req(
str(subreq),
req_to_install,
isolated=self.isolated,