Skip to content

Commit

Permalink
Remove dependency on importlib-metadata in meta path finder (#176)
Browse files Browse the repository at this point in the history
importlib-metadata is a third-party module that isn't included in the
bootstrap pex, preventing the `ModuleDirImport` class from acting as a
meta path finder unless it happens to be included as a dependency of
downstream code. Depend on `importlib.metadata` instead, which has been
part of the standard library since 3.8.

Fixes #175.
  • Loading branch information
chrisnovakovic authored Oct 3, 2024
1 parent 907b778 commit fba6378
Showing 1 changed file with 44 additions and 49 deletions.
93 changes: 44 additions & 49 deletions tools/please_pex/pex/pex_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

from importlib import import_module
from importlib.abc import MetaPathFinder
from importlib.metadata import Distribution
from importlib.util import spec_from_loader
from zipfile import ZipFile, ZipInfo, is_zipfile
import os
import re
import runpy
import sys

Expand Down Expand Up @@ -210,55 +212,48 @@ def find_distributions(self, context):
loading the metadata for packages for the indicated ``context``.
"""

try:
from importlib_metadata import Distribution
import re
except:
pass
else:

class PexDistribution(Distribution):
template = r"{path}(-.*)?\.(dist|egg)-info/{filename}"

def __init__(self, name, prefix=MODULE_DIR):
"""Construct a distribution for a pex file to the metadata directory.
:param name: A module name
:param prefix: Modules prefix
"""
self._name = name
self._prefix = prefix

def _match_file(self, name, filename):
if re.match(
self.template.format(
path=os.path.join(self._prefix, self._name),
filename=filename,
),
name,
):
return name

def read_text(self, filename):
if is_zipfile(sys.argv[0]):
zf = ZipFileWithPermissions(sys.argv[0])
for name in zf.namelist():
if name and self._match_file(name, filename):
return zf.read(name).decode(encoding="utf-8")

read_text.__doc__ = Distribution.read_text.__doc__

def _has_distribution(self):
if is_zipfile(sys.argv[0]):
zf = ZipFileWithPermissions(sys.argv[0])
for name in zf.namelist():
if name and self._match_file(name, ""):
return True

if context.name in sys.modules:
distribution = PexDistribution(context.name)
if distribution._has_distribution():
yield distribution
class PexDistribution(Distribution):
template = r"{path}(-.*)?\.(dist|egg)-info/{filename}"

def __init__(self, name, prefix=MODULE_DIR):
"""Construct a distribution for a pex file to the metadata directory.
:param name: A module name
:param prefix: Modules prefix
"""
self._name = name
self._prefix = prefix

def _match_file(self, name, filename):
if re.match(
self.template.format(
path=os.path.join(self._prefix, self._name),
filename=filename,
),
name,
):
return name

def read_text(self, filename):
if is_zipfile(sys.argv[0]):
zf = ZipFileWithPermissions(sys.argv[0])
for name in zf.namelist():
if name and self._match_file(name, filename):
return zf.read(name).decode(encoding="utf-8")

read_text.__doc__ = Distribution.read_text.__doc__

def _has_distribution(self):
if is_zipfile(sys.argv[0]):
zf = ZipFileWithPermissions(sys.argv[0])
for name in zf.namelist():
if name and self._match_file(name, ""):
return True

if context.name in sys.modules:
distribution = PexDistribution(context.name)
if distribution._has_distribution():
yield distribution

def get_code(self, fullname):
module = self.load_module(fullname)
Expand Down

0 comments on commit fba6378

Please sign in to comment.