Skip to content

Instantly share code, notes, and snippets.

@ZedThree
Created August 20, 2015 08:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ZedThree/4f6ab6037e9dacf26d44 to your computer and use it in GitHub Desktop.
Save ZedThree/4f6ab6037e9dacf26d44 to your computer and use it in GitHub Desktop.
def moduleinit():
"""Make sure various environment variables are set correctly
"""
if 'MODULE_VERSION' not in os.environ:
os.environ['MODULE_VERSION_STACK'] = '3.2.10'
os.environ['MODULE_VERSION'] = '3.2.10'
else:
os.environ['MODULE_VERSION_STACK'] = os.environ['MODULE_VERSION']
os.environ['MODULESHOME'] = '/usr/share/Modules/3.2.10'
if 'MODULEPATH' not in os.environ:
f = open(os.environ['MODULESHOME'] + "/init/.modulespath", "r")
path = []
for line in f.readlines():
line = re.sub("#.*$", '', line)
if line is not '':
path.append(line)
os.environ['MODULEPATH'] = ':'.join(path)
if 'LOADEDMODULES' not in os.environ:
os.environ['LOADEDMODULES'] = ''
def module(*args):
"""Pass arguments as separate items in list"""
if type(args[0]) == type([]):
args = args[0]
else:
args = list(args)
(output, error) = subprocess.Popen(['/usr/share/Modules/{0:s}/bin/modulecmd'.format(os.environ['MODULE_VERSION']), 'python'] +
args, stdout=subprocess.PIPE).communicate()
exec(output)
# Use like:
moduleinit()
module(["load","foo"]) # equivalent to 'module load foo'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment