Source code for schrodinger.application.desmond.cmdline
"""
Helper functions for dealing with desmond/multisim commandline.
"""
import base64
import os
import re
def _split_app_and_args(cmdline):
    """
    Split a desmond-like commandline into an app and the rest of arguments.
    :param cmdline: text of cmdline arg
    :type cmdline: str
    :return: user-facing name of application and remainder commandline
    :rtype: tuple(str,str)
    :raises: RuntimeError if we can't find a desmond-like program in input
    """
    appnames = (os.path.join("utilities", "multisim"), "fep_plus", "watermap",
                "fep_solubility", "mxmd")
    for app_name in appnames:
        # escape for re os.path.sep for windows
        app_name = app_name.replace('\\', "\\\\")
        match = re.match(r"(.*?%s) *(.*)" % app_name, cmdline)
        if not match:
            continue
        return os.path.join("$SCHRODINGER", app_name), match.group(2)
    raise ValueError(f"Could not find any desmond-like apps in {cmdline}")
[docs]def get_job_command_in_startup():
    """
    Return a job launching command suitable for printing in a log file.
    Munges common desmond applications (multisim, watermap, fep_plus)
    to generate a friendlier commandline.
    :param application_name: full application name
        ($SCHRODINGER/utilities/multisim or $SCHRODINGER/watermap)
    :type application_name: str
    :rtype: str
    """
    cmdline = os.path.normpath(os.environ.get("SCHRODINGER_COMMANDLINE"))
    try:
        app_name, cmdline = _split_app_and_args(cmdline)
    except ValueError:
        return cmdline
    return f"Job launching command:\n{app_name} {cmdline}\n" 
[docs]def get_b64encoded_str(string):
    """
    Encodes a string as base64 and returns a str, suitable for passing on a
    commandline.
    :param string: string to be encoded
    :type string: str
    :rtype: str
    """
    # command data is most likely to be in utf-8 but if it is not, just
    # replace with recognizable characters. This is for log display.
    desc_bytes = string.encode("utf-8", errors="xmlcharrefreplace")
    return base64.b64encode(desc_bytes).decode('ascii') 
[docs]def get_b64decoded_str(encoded_str):
    """
    Decodes a base64 string and returns as str.
    """
    byte_array = encoded_str.encode('ascii')
    return base64.b64decode(byte_array).decode('utf-8')