Source code for schrodinger.utils.platform
import sys
import inflect
# The values are same as what is returned by sys.platform in version 3.6
# See https://docs.python.org/3/library/sys.html#sys.platform
LINUX = 'linux'
WINDOWS = 'win32'
DARWIN = 'darwin'
_PLATFORM_DISPLAY_NAME = {LINUX: 'Linux', WINDOWS: 'Windows', DARWIN: 'Mac'}
[docs]def validate_localhost_platform_compatibility(compatible_platforms):
"""
:param compatible_platforms: Compatible platforms. Use LINUX, WINDOWS or
DARWIN as values for platforms.
:type compatible_platforms: list[str]
:raises ValueError: If a value in `compatible_platforms` is not LINUX,
WINDOWS or DARWIN.
:return: Whether the localhost's platform is in `compatible_platforms`.
Also returns a non empty message if platform is incompatible. The
return value can be used directly with af2 validators and task
preprocessors.
:rtype: tuple(bool, str)
"""
if is_localhost_platform_compatible(compatible_platforms):
return True, ''
return False, _get_platform_incompatibility_message(compatible_platforms)
[docs]def is_localhost_platform_compatible(compatible_platforms):
"""
:param compatible_platforms: Compatible platforms. Use LINUX, WINDOWS or
DARWIN as values for platforms.
:type compatible_platforms: list[str]
:raises ValueError: If a value in `compatible_platforms` is not LINUX,
WINDOWS or DARWIN.
:return: Whether the localhost's platform is compatible.
:rtype: bool
"""
unknown_platforms = [
platform for platform in compatible_platforms
if platform not in (LINUX, WINDOWS, DARWIN)
]
if unknown_platforms:
platform_str = inflect.engine().plural('platform',
len(unknown_platforms))
msg = f"Unknown compatible {platform_str} - {', '.join(unknown_platforms)}"
raise ValueError(msg)
# We use the 'startswith' idiom to ensure backward compatibility as
# sys.platform also returned the major version previously.
# See https://docs.python.org/3/library/sys.html#sys.platform
return any([
sys.platform.startswith(platform) for platform in compatible_platforms
])
def _get_platform_incompatibility_message(compatible_platforms):
"""
:param compatible_platforms: Compatible platforms.
:type compatible_platforms: list[str]
:return: Platform incompatibility message.
:rtype: str
"""
compatible_platforms_str = ' or a '.join([
_PLATFORM_DISPLAY_NAME[platform]
if platform in _PLATFORM_DISPLAY_NAME else platform
for platform in compatible_platforms
])
return (f"The requested job requires a {compatible_platforms_str} "
"host. If your installation has been configured to include a "
f"{compatible_platforms_str} server, select that machine "
"from the Host list in Job Settings before launching the job.")