Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I build PEX with system wide site-packages? #3

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions pex/pex.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,21 @@ def minimum_sys_modules(cls, site_libs):

return new_modules

def minimum_sys_path(self, site_libs):
sys_path = OrderedSet(sys.path)

if not self._pex_info.inherit_path:
sys_path = list(sys_path - self._scrub_paths(site_libs))

scrub_from_importer_cache = filter(
lambda key: any(key.startswith(path) for path in sys_path),
sys.path_importer_cache.keys())
scrubbed_importer_cache = dict((key, value) for (key, value) in sys.path_importer_cache.items()
if key not in scrub_from_importer_cache)
return sys_path, scrubbed_importer_cache

@classmethod
def minimum_sys_path(cls, site_libs):
def _scrub_paths(cls, site_libs):
site_distributions = OrderedSet()
for path_element in sys.path:
if any(path_element.startswith(site_lib) for site_lib in site_libs):
Expand All @@ -132,31 +145,24 @@ def minimum_sys_path(cls, site_libs):
TRACER.log('Scrubbing from user site: %s' % path)

scrub_paths = site_distributions | user_site_distributions
scrubbed_sys_path = list(OrderedSet(sys.path) - scrub_paths)
scrub_from_importer_cache = filter(
lambda key: any(key.startswith(path) for path in scrub_paths),
sys.path_importer_cache.keys())
scrubbed_importer_cache = dict((key, value) for (key, value) in sys.path_importer_cache.items()
if key not in scrub_from_importer_cache)
return scrubbed_sys_path, scrubbed_importer_cache
return scrub_paths

@classmethod
def minimum_sys(cls):
def minimum_sys(self):
"""Return the minimum sys necessary to run this interpreter, a la python -S.

:returns: (sys.path, sys.path_importer_cache, sys.modules) tuple of a
bare python installation.
"""
site_libs = set(cls._site_libs())
site_libs = set(self._site_libs())
for site_lib in site_libs:
TRACER.log('Found site-library: %s' % site_lib)
for extras_path in cls._extras_paths():
for extras_path in self._extras_paths():
TRACER.log('Found site extra: %s' % extras_path)
site_libs.add(extras_path)
site_libs = set(os.path.normpath(path) for path in site_libs)

sys_modules = cls.minimum_sys_modules(site_libs)
sys_path, sys_path_importer_cache = cls.minimum_sys_path(site_libs)
sys_modules = self.minimum_sys_modules(site_libs)
sys_path, sys_path_importer_cache = self.minimum_sys_path(site_libs)

return sys_path, sys_path_importer_cache, sys_modules

Expand All @@ -178,9 +184,8 @@ def patch(working_set):
finally:
patch(old_working_set)

@classmethod
@contextmanager
def patch_sys(cls):
def patch_sys(self):
"""Patch sys with all site scrubbed."""
def patch_dict(old_value, new_value):
old_value.clear()
Expand All @@ -193,7 +198,7 @@ def patch_all(path, path_importer_cache, modules):

old_sys_path, old_sys_path_importer_cache, old_sys_modules = (
sys.path[:], sys.path_importer_cache.copy(), sys.modules.copy())
new_sys_path, new_sys_path_importer_cache, new_sys_modules = cls.minimum_sys()
new_sys_path, new_sys_path_importer_cache, new_sys_modules = self.minimum_sys()

patch_all(new_sys_path, new_sys_path_importer_cache, new_sys_modules)

Expand Down
1 change: 1 addition & 0 deletions tests/system_provided_packages/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fire `vagrant up` to check if `pex` imports system wide site-packages.
31 changes: 31 additions & 0 deletions tests/system_provided_packages/Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :

$install_packages = <<SCRIPT
which pip && exit 0
sudo apt-get install -y python-pip python-gtk2
sudo pip install wheel
SCRIPT

$import_gobject = <<SCRIPT
cd /pex_src
sudo python setup.py install
cd
cat > import_gobject.py << __EOF
import gobject
print gobject.__file__
__EOF
export PEX_VERBOSE=1
pex --inherit-path -- import_gobject.py
SCRIPT

Vagrant.configure(2) do |config|

config.vm.box = "ubuntu/trusty64"

config.vm.synced_folder "../../", "/pex_src"

config.vm.provision "shell", inline: $install_packages

config.vm.provision "shell", inline: $import_gobject
end