Skip to content

Commit

Permalink
got environment variables working
Browse files Browse the repository at this point in the history
  • Loading branch information
toumorokoshi committed Feb 19, 2015
1 parent a97d4e6 commit 703c492
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
]

setup(name='uranium',
version='0.0.56',
version='0.0.57',
description='a build system for python',
long_description='a build system for python',
author='Yusuke Tsutsumi',
Expand Down
11 changes: 10 additions & 1 deletion uranium/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
from docopt import docopt
from virtualenv import make_environment_relocatable
from .uranium import Uranium
from .virtualenv_manager import install_virtualenv
from .virtualenv_manager import (
install_virtualenv, inject_into_activate_this
)
from .config import Config
from uranium.activate import generate_activate_this
import os
import sys

Expand All @@ -40,6 +43,7 @@ def main(argv=sys.argv[1:]):

with in_virtualenv(uranium_dir):
uranium.run()
_inject_activate_this(uranium_dir, uranium)


def _get_uranium(uranium_file):
Expand All @@ -64,6 +68,11 @@ def in_virtualenv(path):
make_environment_relocatable(path)


def _inject_activate_this(uranium_dir, uranium):
content = generate_activate_this(uranium)
inject_into_activate_this(uranium_dir, content)


def _activate_virtualenv(uranium_dir):
""" this will activate a virtualenv in the case one exists """
sys.prefix = os.path.join(*sys.prefix.split(os.sep)[:-2])
Expand Down
22 changes: 22 additions & 0 deletions uranium/activate/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def generate_activate_this(uranium):
"""
generate a text block which should be injected into an activate_this script.
"""
return (
_generate_environment_variables(uranium)
)


ENV_TEMPLATE = """
import os
""".strip()


def _generate_environment_variables(uranium):
content = ENV_TEMPLATE
for name, value in uranium.environment.items():
content += "\nos.environ['{name}'] = '{value}'".format(
name=name, value=value
)

return content
33 changes: 33 additions & 0 deletions uranium/tests/test_full/test_environment_variables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
tests for the warmup script
"""
import os
from uranium.tests.utils import FullUraniumBaseTest
from nose.tools import ok_

BASE = os.path.dirname(__file__)
URANIUM_ROOT_PATH = os.path.join(BASE, os.pardir, os.pardir, os.pardir)


class TestEggResolution(FullUraniumBaseTest):

config = {
'phases': {
'after-eggs': ['environment']
},
'parts': {
'environment': {
'_plugin': 'uranium.plugin.example',
'environment': {
'LD_LIBRARY_PATH': '/usr/lib'
}
}
}
}

def test_environment_variable_is_set(self):
activate_this = os.path.join(self.root, 'bin', 'activate_this.py')
with open(activate_this) as fh:
content = fh.read()

ok_("os.environ['LD_LIBRARY_PATH'] = '/usr/lib'" in content)
12 changes: 12 additions & 0 deletions uranium/uranium.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def __init__(self, config, root, state_file=None):
self._buildout = BuildoutAdapter(self, self._classloader)
self._plugin_runner = PluginRunner(self, self._classloader)
self._state = State(state_file)
self._environment = {}
self._validate_config()

@property
Expand All @@ -55,6 +56,17 @@ def bin(self):
os.path.join(self._root, BIN_DIRECTORY))
return self._bin

@property
def environment(self):
"""
environment is the dictionary that can be used to set environment
variables.
any key/value pairs set here will be injected into the
initialization scripts.
"""
return self._environment

@property
def parts_directory(self):
return os.path.join(self.root, PARTS_DIRECTORY)
Expand Down

0 comments on commit 703c492

Please sign in to comment.