Skip to content

Commit

Permalink
fix for use function
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-hunt committed Jan 25, 2017
1 parent a11615d commit d203aa9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
2 changes: 1 addition & 1 deletion hublib/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.02"
__version__ = "0.5"
38 changes: 24 additions & 14 deletions hublib/use/__init__.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import sys
import os
from IPython.core.magic import register_line_magic

from string import Template

"""
Line magic to allow the 'use' command to load environment modules.
Line magic to allow the 'use' command to load hub environment modules.
This works on the current environment, so we cannot simply fork a shell.
Maybe some trick to fork a shell and return the modified environment would work?
For now lets see how this works. Just implement "setenv", "prepend", and "use".
Works for all currently installed modules except two old ones that use local shell variables.
"""

EPATH = '/apps/share64/debian7/environ.d'
d = {}


def setenv(line):
# print 'SETENV', line
name, val = line
os.environ[name] = val


def prepend(line):
# print 'PREPEND', line
name, val = line
try:
oldval = os.environ[name]
Expand All @@ -31,23 +30,34 @@ def prepend(line):
os.environ[name] = val


def _set(a, b):
global d
t = Template(b)
b = t.safe_substitute(d)
d[a] = b


def _use(name):
fname = os.path.join(EPATH, name)
if not name[0] == '.':
fname = os.path.join(EPATH, name)

with open(fname) as fp:
for line in fp:
line = line.strip().split()
if line == []:
sline = line.strip().split()
if sline == []:
continue
if line[0] == 'prepend':
prepend(line[1:])
if sline[0] == 'prepend':
prepend(sline[1:])
continue
if line[0] == 'setenv':
setenv(line[1:])
if sline[0] == 'setenv':
setenv(sline[1:])
continue
if line[0] == 'use':
_use(line[-1])

if sline[0] == 'use':
_use(sline[-1])
continue
line = line.split("=")
if len(line) == 2:
_set(line[0].strip(), line[1].strip())

@register_line_magic
def use(name):
Expand Down

0 comments on commit d203aa9

Please sign in to comment.