Skip to content

Commit

Permalink
Merge pull request #7747 from pfmoore/parsed_requirement
Browse files Browse the repository at this point in the history
Refactor parse_requirements to be independent of InstallRequirement
  • Loading branch information
chrahunt authored Feb 23, 2020
2 parents 082c0f0 + 2b6fb95 commit 4052379
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 106 deletions.
Empty file.
20 changes: 15 additions & 5 deletions src/pip/_internal/cli/req_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from pip._internal.req.constructors import (
install_req_from_editable,
install_req_from_line,
install_req_from_parsed_requirement,
install_req_from_req_string,
)
from pip._internal.req.req_file import parse_requirements
Expand Down Expand Up @@ -287,10 +288,15 @@ def get_requirements(
check_supported_wheels=check_supported_wheels
)
for filename in options.constraints:
for req_to_add in parse_requirements(
for parsed_req in parse_requirements(
filename,
constraint=True, finder=finder, options=options,
session=session, wheel_cache=wheel_cache):
session=session):
req_to_add = install_req_from_parsed_requirement(
parsed_req,
isolated=options.isolated_mode,
wheel_cache=wheel_cache,
)
req_to_add.is_direct = True
requirement_set.add_requirement(req_to_add)

Expand All @@ -315,11 +321,15 @@ def get_requirements(

# NOTE: options.require_hashes may be set if --require-hashes is True
for filename in options.requirements:
for req_to_add in parse_requirements(
for parsed_req in parse_requirements(
filename,
finder=finder, options=options, session=session,
finder=finder, options=options, session=session):
req_to_add = install_req_from_parsed_requirement(
parsed_req,
isolated=options.isolated_mode,
wheel_cache=wheel_cache,
use_pep517=options.use_pep517):
use_pep517=options.use_pep517
)
req_to_add.is_direct = True
requirement_set.add_requirement(req_to_add)

Expand Down
11 changes: 9 additions & 2 deletions src/pip/_internal/commands/uninstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
from pip._internal.cli.req_command import SessionCommandMixin
from pip._internal.exceptions import InstallationError
from pip._internal.req import parse_requirements
from pip._internal.req.constructors import install_req_from_line
from pip._internal.req.constructors import (
install_req_from_line,
install_req_from_parsed_requirement,
)
from pip._internal.utils.misc import protect_pip_from_modification_on_windows


Expand Down Expand Up @@ -58,10 +61,14 @@ def run(self, options, args):
if req.name:
reqs_to_uninstall[canonicalize_name(req.name)] = req
for filename in options.requirements:
for req in parse_requirements(
for parsed_req in parse_requirements(
filename,
options=options,
session=session):
req = install_req_from_parsed_requirement(
parsed_req,
isolated=options.isolated_mode
)
if req.name:
reqs_to_uninstall[canonicalize_name(req.name)] = req
if not reqs_to_uninstall:
Expand Down
32 changes: 32 additions & 0 deletions src/pip/_internal/req/constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
Any, Dict, Optional, Set, Tuple, Union,
)
from pip._internal.cache import WheelCache
from pip._internal.req.req_file import ParsedRequirement


__all__ = [
Expand Down Expand Up @@ -441,3 +442,34 @@ def install_req_from_req_string(
req, comes_from, isolated=isolated, wheel_cache=wheel_cache,
use_pep517=use_pep517
)


def install_req_from_parsed_requirement(
parsed_req, # type: ParsedRequirement
isolated=False, # type: bool
wheel_cache=None, # type: Optional[WheelCache]
use_pep517=None # type: Optional[bool]
):
# type: (...) -> InstallRequirement
if parsed_req.is_editable:
req = install_req_from_editable(
parsed_req.requirement,
comes_from=parsed_req.comes_from,
use_pep517=use_pep517,
constraint=parsed_req.constraint,
isolated=isolated,
wheel_cache=wheel_cache
)

else:
req = install_req_from_line(
parsed_req.requirement,
comes_from=parsed_req.comes_from,
use_pep517=use_pep517,
isolated=isolated,
options=parsed_req.options,
wheel_cache=wheel_cache,
constraint=parsed_req.constraint,
line_source=parsed_req.line_source,
)
return req
Loading

0 comments on commit 4052379

Please sign in to comment.