Source code for schrodinger.utils.sysinfo
"""
Gather information about current system.
"""
import os
import platform
import re
import sys
import distro
from decorator import decorator
from . import subprocess
EXE = ""
if sys.platform == "win32":
EXE = ".exe"
MMSHARE_EXEC = os.environ["MMSHARE_EXEC"]
LMUTIL = os.path.join(MMSHARE_EXEC, "lmutil" + EXE)
UNKNOWN = "Unknown"
PYTHON_EXE = "python3"
"""
Versioned Python executable to use in calls to Python. Useful when running a
Python command (python3 -c ""). Prefer $SCHRODINGER/run <scriptname.py>
"""
[docs]@decorator
def catch_subprocess_errors(f):
"""
Catches non-zero return code for any operations that might
fail. Use this to return UNKNOWN for any values we can't
query.
"""
try:
return f()
except:
return UNKNOWN
[docs]@catch_subprocess_errors
def get_glibc_version():
"""
Return string of glibc version.
"""
locations = [
"/lib64/libc.so.6",
"/lib/x86_64-linux-gnu/libc.so.6",
]
for location in locations:
if os.path.exists(location):
glibc = location
break
else:
return
output = subprocess.check_output([glibc], universal_newlines=True)
if len(output.splitlines()) > 0:
tokens = output.splitlines()[0].split()
for i, token in enumerate(tokens):
if token == "version":
return tokens[i + 1].strip(",")
[docs]@catch_subprocess_errors
def get_kernel_version():
"""
Return string of Linux kernel verison.
"""
return subprocess.check_output(["uname", "-r"],
universal_newlines=True).strip()
[docs]@catch_subprocess_errors
def get_cpu():
"""
Return a descriptive string of CPU type.
"""
if sys.platform.startswith("darwin"):
command_output = subprocess.check_output(
["sysctl", "-n", "machdep.cpu.brand_string"],
universal_newlines=True).strip()
elif sys.platform.startswith("linux"):
with open("/proc/cpuinfo") as fh:
for line in fh:
if line.startswith("model name"):
command_output = line.split(":")[1].strip()
break
elif sys.platform == "win32":
return os.environ.get("PROCESSOR_IDENTIFIER", UNKNOWN)
return command_output
[docs]def query_registry(key, value):
regquery = os.path.join(os.environ["SCHRODINGER"], "regquery")
output = subprocess.check_output([regquery, key, value],
universal_newlines=True)
return output.replace("Value:", "").strip()
[docs]@catch_subprocess_errors
def get_osname():
"""
Return descriptive name of OS for reporting.
"""
if sys.platform.startswith("darwin"):
return platform.mac_ver()[0]
elif sys.platform.startswith("linux"):
return distro.name(pretty=True)
elif sys.platform == "win32":
key = "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion"
name = query_registry(key, "ProductName")
_, version, service_pack, _ = platform.win32_ver()
output = name + " " + service_pack + " (" + version + ")"
return output
[docs]@catch_subprocess_errors
def get_hostid():
output = subprocess.check_output([LMUTIL, "lmhostid"],
universal_newlines=True)
for line in output.splitlines():
if re.search("flexnet", line, re.IGNORECASE):
# matching first of ""0123456789ab 01333b9414863""
start = output.find('""')
end = output.find(" ", start + 1)
if start == -1:
#matching "0123456789ab"
start = output.find('"')
end = output.find('"', start + 1)
if start == -1 or end == -1:
continue
hostid = output[start + 1:end]
break
if start == -1 or end == -1:
continue
hostid = output[start + 2:end]
break
else:
hostid = UNKNOWN
return hostid
[docs]@catch_subprocess_errors
def get_hostname():
"""
Return the FLEXlm hostname.
"""
# FLEXlm license administration guide suggest to use
# the following to get hostname usable to specify as
# server in the license.
if sys.platform.startswith(('darwin', 'linux')):
output = subprocess.check_output(["uname", "-n"],
universal_newlines=True)
hostname = output.strip()
if hostname == "":
hostname = UNKNOWN
else:
output = subprocess.check_output(
[LMUTIL, "lmhostid", "-hostname", "-n"], universal_newlines=True)
try:
hostname = output.strip().split("=")[1]
except:
hostname = UNKNOWN
return hostname
[docs]@catch_subprocess_errors
def get_uname():
if sys.platform != "win32":
output = subprocess.check_output(["uname", "-a"],
universal_newlines=True).strip()
return output
[docs]def is_display_present():
"""
Returns TRUE if user can access display.
"""
if sys.platform.startswith("linux"):
if 'DISPLAY' not in os.environ or not os.environ['DISPLAY']:
return False
elif sys.platform.startswith('darwin'):
from schrodinger.test import darwinconsole
return darwinconsole.is_console_user(os.environ['USER'])
return True