Source code for schrodinger.job.util
"""
Contains a utility to find a product within a SCHRODINGER directory.
@copyright: Schrodinger, LLC. All rights reserved.
"""
import enum
import glob
import os
[docs]@enum.unique
class DirectoryType(enum.Enum):
EXEC = "exec"
LIB = "lib"
DATA = "data"
[docs]def hunt(product, dir=DirectoryType.EXEC):
"""
Look for the exec, lib, or data directory for a given Schrodinger product.
:param dir: The type of directory to find; must be 'exec', 'lib', or 'data'
and defaults to 'exec'.
:type dir: DirectoryType
:return: The executable, lib, or data directory of the desired product.
If no directory is found, the empty string is returned.
:rtype: str
"""
# dir is an unfortunate name but it is used in calling conventions
if not isinstance(dir, DirectoryType):
try:
directory = DirectoryType[dir.upper()]
except KeyError:
raise TypeError("dir ({}) needs to be one of DirectoryTypes")
else:
directory = dir
globpattern = "{}/{}-v*/bin/[A-Z]*".format(os.environ.get("SCHRODINGER"),
product)
execdirs = [d for d in glob.glob(globpattern) if os.path.isdir(d)]
if execdirs:
exec_dir = execdirs[0]
else:
return ""
if directory == DirectoryType.EXEC:
return exec_dir
bin_dir, os_cpu = os.path.split(exec_dir)
base_dir = os.path.dirname(bin_dir)
if not base_dir:
return ""
if directory == DirectoryType.LIB:
return os.path.join(base_dir, "lib", os_cpu)
elif directory == DirectoryType.DATA:
return os.path.join(base_dir, "data")