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
193 additions
and
32 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,53 @@ | ||
# 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 | ||
|
||
|
||
def module_path(module_name): | ||
"""Converts a python module name into a list of the components of its package hierarchy.""" | ||
return module_name.split('.') | ||
|
||
|
||
class Bootstrap(object): | ||
"""Supports introspection of the PEX bootstrap code.""" | ||
|
||
_INSTANCE = None | ||
|
||
@classmethod | ||
def locate(cls): | ||
"""Locates the active PEX bootstrap.""" | ||
if cls._INSTANCE is None: | ||
bootstrap_path = __file__ | ||
module_import_path = module_path(__name__) | ||
pex_package = module_import_path[0] | ||
|
||
# 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(pex_module_name=pex_package, path_entry=bootstrap_path) | ||
return cls._INSTANCE | ||
|
||
def __init__(self, pex_module_name, path_entry): | ||
self._pex_module_name = pex_module_name | ||
self._path_entry = path_entry | ||
|
||
@property | ||
def pex_module_name(self): | ||
"""Returns the name of the root `pex` module in the context of the bootstrap.""" | ||
return self._pex_module_name | ||
|
||
@property | ||
def path_entry(self): | ||
"""Returns the sys.path entry that contains the PEX bootstrap code.""" | ||
return self._path_entry | ||
|
||
def iter_root_module_names(self): | ||
"""Returns an iterator over the root modules found in the PEX bootstrap.""" | ||
for _, module_name, _ in pkgutil.iter_modules([self.path_entry]): | ||
yield module_name |
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