forked from pex-tool/pex
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix pex force local to handle PEP-420.
Previously, pex would blow up trying to adjust import paths if a PEP-420 implicit namespace package was encountered. Add a test of the path adjustment functionality both to confirm it was needed (it was) and that the fix technique works with all forms of namespace packages by only assuming they support `append`. Fixes pex-tool#598
- Loading branch information
Showing
5 changed files
with
208 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# coding=utf-8 | ||
# Copyright 2018 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
import os | ||
import pkgutil | ||
|
||
|
||
class Bootstrap(object): | ||
"""Supports introspection of the PEX bootstrap code.""" | ||
|
||
@staticmethod | ||
def _module_path(module_name): | ||
return module_name.split('.') | ||
|
||
_INSTANCE = None | ||
|
||
@classmethod | ||
def locate(cls): | ||
"""Locates the active PEX bootstrap. | ||
:rtype: :class:`Bootstrap` | ||
""" | ||
if cls._INSTANCE is None: | ||
bootstrap_path = __file__ | ||
module_import_path = cls._module_path(__name__) | ||
|
||
# For example, our __file__ might be requests.pex/.bootstrap/_pex/bootstrap.pyc and our import | ||
# path _pex.bootstrap; so we walk back through all the module components of our import path to | ||
# find the base sys.path entry where we were found (requests.pex/.bootstrap in this example). | ||
for _ in module_import_path: | ||
bootstrap_path = os.path.dirname(bootstrap_path) | ||
|
||
cls._INSTANCE = cls(path_entry=bootstrap_path) | ||
return cls._INSTANCE | ||
|
||
def __init__(self, path_entry): | ||
self._path_entry = path_entry | ||
self._root_module_names = None | ||
|
||
@property | ||
def path_entry(self): | ||
"""Return the ``sys.path`` entry that contains the PEX bootstrap code. | ||
:rtype: str | ||
""" | ||
return self._path_entry | ||
|
||
@property | ||
def root_module_names(self): | ||
"""Return the set of root modules found in the PEX bootstrap. | ||
:rtype: :class:`collections.Set` of str | ||
""" | ||
if self._root_module_names is None: | ||
def iter_module_names(): | ||
for _, module_name, _ in pkgutil.iter_modules([self.path_entry]): | ||
yield module_name | ||
self._root_module_names = frozenset(iter_module_names()) | ||
return self._root_module_names | ||
|
||
def is_bootstrap_module(self, module_name): | ||
"""Return ``True`` if the given ``module_name`` points into bootstrap code. | ||
:rtype: bool | ||
""" | ||
return self._module_path(module_name)[0] in self.root_module_names |
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
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