text
stringlengths
1
93.6k
loadedmodules = os.getenv('LOADEDMODULES')
lmfiles_ = os.getenv('_LMFILES_')
modulesbeginenv = os.getenv('MODULESBEGINENV')
modulesbeginenv_ = os.getenv('_MODULESBEGINENV_')
# File paths
filepath = {
#'moduleshome' : TODO /usr/local/Cellar/modules/3.2.10/Modules
'rc' : os.path.join(moduleshome, 'etc/rc'),
'modulerc' : os.path.join(_home, '.modulerc'),
'modulefiles' : os.path.join(moduleshome, 'modulefiles'),
'modulecmd' : os.path.join(moduleshome, 'bin/modulecmd'),
#'shell' : TODO os.path.join(moduleshome, '.init/shell'),
'moduleavailcache' : os.path.join(moduleshome, '.moduleavailcache'),
'moduleavailcachedir' : os.path.join(moduleshome, '.moduleavailcachedir'),
'modulesbeginenv' : os.path.join(_home, '.modulesbeginenv'),
}
# Modified Environment Module python module
def _setup():
"""Setup and confirmation that Environment Modules is installed"""
# Make sure MODULEPATH is defined
if 'MODULEPATH' not in os.environ.keys():
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)
# Make sure LOADEDMODULES is defined
if 'LOADEDMODULES' not in os.environ.keys():
os.environ['LOADEDMODULES'] = ''
# Make sure MODULESHOME is defined
if not os.environ.get('MODULESHOME'):
sys.stderr.write("Error: MODULESHOME must be set\n")
sys.exit(1)
def _get_modulecmd():
"""Get path to modulecmd."""
modulekeys = [m for m in os.environ.keys() if 'BASH_FUNC_module' in m]
if not modulekeys:
sys.stderr.write("Error: BASH_FUNC_module must be set\n")
sys.exit(1)
else:
modulekey = modulekeys[0]
modcmd = os.getenv(modulekey).split()
modulecmd = [c for c in modcmd if 'modulecmd' in c][0].strip('`')
return modulecmd
def module(*args):
"""Call modulecmd with 'python' argument to get python commands for exec"""
_setup()
modulecmd = _get_modulecmd()
args = ' '.join(args)
cmd = shlex.split('{0} python {1}'.format(modulecmd, args))
output, moduleoutput = Popen(cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE).communicate()
exec(output)
return moduleoutput.decode()
# A partial set of wrapper scripts for module
#
# Command line API
#
def help(modulefile='', flags=''):
"""
Print the usage of each sub-command. If an
argument is given, print the Module-specific help
information for the modulefile(s).
"""
return module('help {0} {1}'.format(modulefile, flags))
def version():
"""Version of modules"""
return module('--v')
def load(modulefile, flags=''):
"""Load modulefile(s) into the shell environment."""
return module('load {0} {1}'.format(modulefile, flags))