Skip to content

Instantly share code, notes, and snippets.

@ZedThree
Created August 20, 2015 08:54
Show Gist options
  • Select an option

  • Save ZedThree/4f6ab6037e9dacf26d44 to your computer and use it in GitHub Desktop.

Select an option

Save ZedThree/4f6ab6037e9dacf26d44 to your computer and use it in GitHub Desktop.

Revisions

  1. ZedThree created this gist Aug 20, 2015.
    38 changes: 38 additions & 0 deletions python-env-modules.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    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'