Skip to content

Commit

Permalink
putting the pieces in place to inject environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
toumorokoshi committed Feb 17, 2015
1 parent 2764afc commit a97d4e6
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
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.55',
version='0.0.56',
description='a build system for python',
long_description='a build system for python',
author='Yusuke Tsutsumi',
Expand Down
44 changes: 44 additions & 0 deletions uranium/tests/test_virtualenv_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import os
import tempfile
from nose.tools import ok_
from uranium.virtualenv_manager import inject_into_file


class TestInjectIntoFile(object):

def setUp(self):
_, self.temp_file = tempfile.mkstemp()

def tearDown(self):
os.unlink(self.temp_file)

def test_inject_empty_file(self):
inject_into_file(self.temp_file, "foo")
with open(self.temp_file) as fh:
ok_("foo" in fh.read())

def test_inject_twice(self):
"""
injecting twice should only result
in the latest entry in the file.
"""
inject_into_file(self.temp_file, "foo")
inject_into_file(self.temp_file, "boo")

with open(self.temp_file) as fh:
content = fh.read()

ok_("foo" not in content)
ok_("boo" in content)

def test_injection_with_content(self):
with open(self.temp_file, 'w+') as fh:
fh.write("this is already in here")

inject_into_file(self.temp_file, "foo")

with open(self.temp_file) as fh:
content = fh.read()

ok_("this is already in here" in content)
ok_("foo" in content)
33 changes: 33 additions & 0 deletions uranium/virtualenv_manager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import re
import os
from virtualenv import create_environment

Expand Down Expand Up @@ -26,3 +27,35 @@ def is_virtualenv(path):
if not os.path.exists(target_path):
return False
return True

INJECT_WRAPPER = "# URANIUM_INJECT THIS"

INJECT_MATCH = re.compile("(\n?{0}.*{0}\n)".format(INJECT_WRAPPER), re.DOTALL)

INJECT_TEMPLATE = """
{0}
{{body}}
{0}
""".format(INJECT_WRAPPER)


def inject_into_activate_this(venv_root, body):
"""
inject a body into activate_this.py.
this will overwrite any values previously injected into activate_this.
"""
activate_this_file = os.path.join(venv_root, 'bin', 'activate_this.py')
inject_into_file(activate_this_file, body)


def inject_into_file(path, body):
""" inject into a file """
with open(path) as fh:
content = fh.read()

content = INJECT_MATCH.sub("", content)
content += INJECT_TEMPLATE.format(body=body)

with open(path, 'w+') as fh:
fh.write(content)

0 comments on commit a97d4e6

Please sign in to comment.