-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from typing import Any, Dict | ||
|
||
from pip._internal.req.req_install import InstallRequirement | ||
from pip._internal.req.req_set import RequirementSet | ||
from pip._internal.utils.direct_url_helpers import ( | ||
direct_url_for_editable, | ||
direct_url_from_link, | ||
) | ||
|
||
|
||
class InstallationReportItem: | ||
def __init__(self, install_req: InstallRequirement): | ||
self._install_req = install_req | ||
|
||
def to_json(self) -> Dict[str, Any]: | ||
if self._install_req.editable: | ||
is_direct = True | ||
direct_url = direct_url_for_editable( | ||
self._install_req.unpacked_source_directory | ||
) | ||
elif self._install_req.original_link: | ||
is_direct = True | ||
direct_url = direct_url_from_link( | ||
self._install_req.original_link, | ||
self._install_req.source_dir, | ||
self._install_req.original_link_is_in_wheel_cache, | ||
) | ||
else: | ||
assert self._install_req.link | ||
is_direct = False | ||
direct_url = direct_url_from_link(self._install_req.link) | ||
res = { | ||
# is_direct is true if requirement came from a direct URL reference (which | ||
# includes editable requirements), and false if the requirement was | ||
# downloaded from a PEP 503 index or --find-links. | ||
"is_direct": is_direct, | ||
# PEP 610 json for the download URL | ||
"download_info": direct_url.to_dict(), | ||
# PEP 566 json encoding for metadata | ||
# https://www.python.org/dev/peps/pep-0566/#json-compatible-metadata | ||
# TODO (MVP) self._install_req.metadata.to_json() | ||
"metadata": {}, | ||
} | ||
if self._install_req.user_supplied: | ||
# TODO (MVP) investigate why this does not reproduce the user supplied URL | ||
# in case of direct requirements | ||
res["requested"] = str(self._install_req.req) | ||
# TODO (LATER) information about the index for find-links for non-direct reqs | ||
# TODO (LATER) information about pip install options | ||
# TODO (MVP?) platform information (python version, etc) | ||
return res | ||
|
||
|
||
class InstallationReport: | ||
def __init__(self, items: Dict[str, InstallationReportItem]): | ||
self._items = items | ||
|
||
@classmethod | ||
def from_requirement_set( | ||
cls, requirement_set: RequirementSet | ||
) -> "InstallationReport": | ||
items = {} | ||
for name, requirement in requirement_set.requirements.items(): | ||
item = InstallationReportItem(requirement) | ||
items[name] = item | ||
return InstallationReport(items) | ||
|
||
def to_json(self) -> Dict[str, Any]: | ||
return { | ||
"installed": {name: item.to_json() for name, item in self._items.items()} | ||
} |