Skip to content

Commit

Permalink
sandboxing uranium separately from the application
Browse files Browse the repository at this point in the history
  • Loading branch information
toumorokoshi committed Jan 19, 2015
1 parent 9beffd7 commit 39478a7
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
]

setup(name='uranium',
version='0.0.25',
version='0.0.26',
description='a build system for python',
long_description='a build system for python',
author='Yusuke Tsutsumi',
Expand Down
4 changes: 4 additions & 0 deletions uranium/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from docopt import docopt
from virtualenv import make_environment_relocatable
from .uranium import Uranium
from .virtualenv_manager import install_virtualenv
from .config import load_config_from_file
import os
import sys
Expand All @@ -44,6 +45,8 @@ def _get_uranium(uranium_file):

@contextmanager
def in_virtualenv(path):
install_virtualenv(path)

# we activate the virtualenv
_activate_virtualenv(path)

Expand All @@ -63,6 +66,7 @@ def _activate_virtualenv(uranium_dir):
# we modify the executable directly, because pip invokes this to install packages.
sys.executable = os.path.join(uranium_dir, 'bin', 'python')


def _create_stdout_logger():
""" create a logger to stdout """
log = logging.getLogger(__name__)
Expand Down
71 changes: 71 additions & 0 deletions uranium/virtualenv_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import io
import logging
import os
import subprocess
import shutil
import sys
import tarfile
import tempfile

try:
from urllib2 import urlopen as urlopen
except:
from urllib.request import urlopen as urlopen

VENV_URL = "https://pypi.python.org/packages/source/v/virtualenv/virtualenv-{major}.{minor}.{rev}.tar.gz"
VENV_MAJOR = 1
VENV_MINOR = 11
VENV_REV = 6

LOGGER = logging.getLogger(__name__)


def install_virtualenv(install_dir):
if is_virtualenv(install_dir):
return

temp_dir = tempfile.mkdtemp()
try:
download_virtualenv(temp_dir)
virtualenv_dir = os.path.join(temp_dir, "virtualenv-{major}.{minor}.{rev}".format(
major=VENV_MAJOR, minor=VENV_MINOR, rev=VENV_REV
))
virtualenv_executable = os.path.join(virtualenv_dir, 'virtualenv.py')
os.chdir(virtualenv_dir) # virtualenv only works in the cwd it is installed in
subprocess.call([sys.executable, virtualenv_executable,
'--no-site-packages',
'--always-copy',
install_dir])
os.chdir(install_dir)
finally:
shutil.rmtree(temp_dir)


def download_virtualenv(target_dir=None):
target_dir = target_dir or os.path.abspath(os.curdir)
venv_url = VENV_URL.format(
major=VENV_MAJOR, minor=VENV_MINOR, rev=VENV_REV
)
extract_tar(venv_url, target_dir)


def extract_tar(url, target_dir):
""" Return a bytesio object with a download bar """
LOGGER.info("Downloading url: {0}".format(url))
fileobj = io.BytesIO(urlopen(url).read())
tf = tarfile.TarFile.open(fileobj=fileobj)
LOGGER.info("extracting to {0}...".format(target_dir))
tf.extractall(target_dir)

VIRTUALENV_FILES = {
'activate file': os.path.join('bin', 'activate')
}


def is_virtualenv(path):
""" validate if the path is already a virtualenv """
for name, venv_path in VIRTUALENV_FILES.items():
target_path = os.path.join(path, venv_path)
if not os.path.exists(target_path):
return False
return True

0 comments on commit 39478a7

Please sign in to comment.