Skip to content

Commit

Permalink
Support .egg-info dist metadata. (pex-tool#2264)
Browse files Browse the repository at this point in the history
This will allow inventorying system interpreters and venvs; which will
enable strict-checking support for PEX exlucions via `--provided` as 
well as more general resolving from venvs (i.e.: adding a new option to
the `--index`/`--find-links`, `--pex-repository` and `--lock` set of
resolve repository options).

The new `-d/--distributions` option is added to
`pex3 interpreter inspect` to support creating the aforementioned
inventories.
  • Loading branch information
jsirois authored Nov 4, 2023
1 parent 9e168be commit 9f8c763
Show file tree
Hide file tree
Showing 16 changed files with 628 additions and 363 deletions.
6 changes: 3 additions & 3 deletions pex/build_system/pep_517.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from pex.build_system import DEFAULT_BUILD_BACKEND
from pex.build_system.pep_518 import BuildSystem, load_build_system
from pex.common import safe_mkdtemp
from pex.dist_metadata import DistMetadata, Distribution
from pex.dist_metadata import DistMetadata, Distribution, MetadataType
from pex.jobs import Job, SpawnedJob
from pex.pip.version import PipVersion, PipVersionValue
from pex.resolve.resolvers import Resolver
Expand All @@ -24,7 +24,7 @@
from pex.util import named_temporary_file

if TYPE_CHECKING:
from typing import Any, Dict, Iterable, Mapping, Optional, Text, Tuple, Union
from typing import Any, Dict, Iterable, Mapping, Optional, Text, Union

_DEFAULT_BUILD_SYSTEMS = {} # type: Dict[PipVersionValue, BuildSystem]

Expand Down Expand Up @@ -272,4 +272,4 @@ def spawn_prepare_metadata(
pip_version=pip_version,
)
)
return spawned_job.map(lambda _: DistMetadata.load(build_dir))
return spawned_job.map(lambda _: DistMetadata.load(build_dir, MetadataType.DIST_INFO))
23 changes: 23 additions & 0 deletions pex/cli/commands/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from pex.cli.command import BuildTimeCommand
from pex.commands.command import JsonMixin, OutputMixin
from pex.dist_metadata import find_distributions
from pex.interpreter import PythonInterpreter
from pex.interpreter_constraints import InterpreterConstraint
from pex.resolve import target_options
Expand Down Expand Up @@ -61,6 +62,15 @@ def _add_inspect_arguments(cls, parser):
"and will contain the path to the interpreter."
),
)
parser.add_argument(
"-d",
"--distributions",
action="store_true",
help=(
"Include information about the distributions installed on the interpreter's"
"`sys.path`."
),
)
cls.add_json_options(parser, entity="verbose output")

interpreter_options_parser = parser.add_argument_group(
Expand Down Expand Up @@ -125,6 +135,7 @@ def _inspect(self):
interpreter_info.update(
version=interpreter.identity.version_str,
requirement=str(InterpreterConstraint.exact_version(interpreter)),
sys_path=interpreter.sys_path,
platform=str(interpreter.platform),
venv=interpreter.is_venv,
)
Expand All @@ -151,6 +162,18 @@ def _inspect(self):
interpreter_info[
"marker_environment"
] = interpreter.identity.env_markers.as_dict()
if self.options.distributions:
interpreter_info["distributions"] = {
dist.project_name: {
"version": dist.version,
"location": dist.location,
"requires_python": str(dist.metadata.requires_python),
"requires_dists": [
str(req) for req in dist.metadata.requires_dists
],
}
for dist in find_distributions(search_path=interpreter.sys_path)
}
self.dump_json(self.options, interpreter_info, out)
else:
out.write(interpreter.binary)
Expand Down
Loading

0 comments on commit 9f8c763

Please sign in to comment.