Skip to content

Instantly share code, notes, and snippets.

@proppy
Last active August 29, 2015 14:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save proppy/a1b251c43bd39c9e8b90 to your computer and use it in GitHub Desktop.
Save proppy/a1b251c43bd39c9e8b90 to your computer and use it in GitHub Desktop.
--- sandbox.py.orig 2014-09-23 13:26:01.151339089 -0700
+++ sandbox.py 2014-09-23 13:28:04.452163114 -0700
@@ -21,6 +21,7 @@
import imp
import os
import re
+import site
import sys
import traceback
import types
@@ -209,6 +210,17 @@
sys.stdin = devnull
sys.stdout = sys.stderr
+ python_path = _get_env(config.environ, 'PYTHONPATH')
+ site_index = 1
+ if python_path:
+ sys.path[site_index:site_index] = [os.path.realpath(p) for p in python_path.split(os.pathsep)]
+ site_index += len(python_path)
+
+ user_base = _get_env(config.environ, 'PYTHONUSERBASE')
+ if user_base:
+ site_dir = os.path.join(user_base, 'lib', 'python' + sys.version[:3], 'site-packages')
+ _insert_site_dir(site_index, site_dir)
+
def _find_shared_object_c_module():
for module_name in ['_sqlite3', '_multiprocessing', '_ctypes', 'bz2']:
@@ -310,6 +322,34 @@
return library_dirs
+def _get_env(environ, key):
+ """Get a variable from the runtime environment.
+
+ Args:
+ environ: A repeated Config.Environ containing the user defined environment.
+
+ Returns:
+ Matching environment variable or None.
+ """
+ for var in environ:
+ if var.key == key:
+ return var.value
+
+def _insert_site_dir(index, site_dir):
+ """Prepend site dir to sys.path.
+
+ Args:
+ site_dir: a site dir path relative to the application directory.
+ """
+ # copy sys.path
+ sys_path = sys.path[:]
+ # application root
+ sys.path = sys_path[:index]
+ # site defined search paths
+ site.addsitedir(site_dir)
+ # SDK defined search paths
+ sys.path.extend(sys_path[index:])
+
class BaseImportHook(object):
"""A base class implementing common import hook functionality.
import site
import os.path
import sys
def _insert_site_dir(index, site_dir):
"""Prepend site dir to sys.path.
Args:
site_dir: a site dir path relative to the application directory.
"""
# copy sys.path
sys_path = sys.path[:]
# application root
sys.path = sys_path[:index]
# site defined search paths
site.addsitedir(site_dir)
# SDK defined search paths
sys.path.extend(sys_path[index:])
user_base = os.environ.get('PYTHONUSERBASE', None)
if user_base:
site_dir = os.path.join(user_base, 'lib', 'python' + sys.version[:3], 'site-packages')
_insert_site_dir(1, site_dir)
"""Dependencies vendoring helpers"""
import site
import os.path
import sys
def add_virtualenv(path):
"""Insert virtualenv site dir in sys.path[1].
Args:
path: relative path to a virtualenv
"""
site_dir = os.path.join(path, 'lib', 'python' + sys.version[:3], 'site-packages')
add(site_dir)
def add(path):
"""Insert site dir in sys.path[1].
Args:
path: relative path to site dir.
"""
# copy sys.path
sys_path = sys.path[:]
# application root
sys.path = sys_path[:1]
# site defined search paths
site.addsitedir(path)
# SDK defined search paths
sys.path.extend(sys_path[1:])
"""Tests for the vendor module."""
import os.path
import sys
import unittest
import vendor
class VendorTestCase(unittest.TestCase):
"""Tests for the vendoring virtualenv and paths."""
def setUp(self):
self.orig_sys_path = sys.path
def tearDown(self):
sys.path = self.orig_sys_path
def isFooBar(self):
import foo
return foo.is_bar
def isBarBar(self):
import bar
return bar.is_bar
def testAddVirtualenv(self):
self.assertRaises(ImportError, self.isFooBar)
vendor.add_virtualenv(os.path.join(os.path.dirname(__file__), 'testdata', 'venv'))
self.assertEquals(False, self.isFooBar())
def testAddSite(self):
self.assertRaises(ImportError, self.isFooBar)
vendor.add(os.path.join(os.path.dirname(__file__), 'testdata', 'site'))
self.assertEquals(True, self.isBarBar())
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment