Source code for schrodinger.utils.machid
import argparse
import os
import re
import socket
import sys
import time
from past.utils import old_div
import psutil
import pymmlibs
from . import sysinfo
pymmlibs.mmerr_set_mmlibs()
EXE = ""
if sys.platform == "win32":
EXE = ".exe"
MMSHARE_EXEC = os.environ["MMSHARE_EXEC"]
MACHID = os.path.join(MMSHARE_EXEC, "machid" + EXE)
UNKNOWN = "Unknown"
[docs]def main(argv=sys.argv[1:]):
parser = argparse.ArgumentParser(
prog="machid",
description="Display machine and product information for licensing.")
parser.add_argument("-hostid", action="store_true", help="Show the host id")
args = parser.parse_args(argv)
if not args.hostid:
print_fields("Date:",
time.strftime("%a %d %b %Y %H:%M:%S", time.localtime()))
print_fields("SCHRODINGER:", os.environ["SCHRODINGER"])
if sys.platform != "win32":
print_fields("uname -a:", sysinfo.get_uname())
print_fields("FQDN:", socket.getfqdn())
print_fields(
"Main memory:",
str(int(psutil.virtual_memory().total / 1024 / 1024)) + " MB")
print_fields(
"Free disk:",
str(int(psutil.disk_usage(os.getcwd()).free / 1024 / 1024)) + " MB")
hostid = sysinfo.get_hostid()
print_fields("Host ID:", hostid)
hostname = sysinfo.get_hostname()
print_fields("Machine name:", hostname)
if args.hostid:
return 0
print_fields("OS:", sysinfo.get_osname())
print_fields("CPU:", sysinfo.get_cpu())
print_fields("Processors:", psutil.cpu_count())
print_fields("Memory:",
str(old_div(psutil.virtual_memory().total, 1000)) + " kB")
print_fields("Swap:",
str(old_div(psutil.swap_memory().total, 1000)) + " kB")
if sys.platform.startswith("linux"):
print_fields("kernel:", sysinfo.get_kernel_version())
print_fields("glibc:", sysinfo.get_glibc_version())
os_cpu_name = os.environ["MMSHARE_EXEC"].split(os.path.sep)[-1]
print_product_versions(os_cpu_name)
if os.path.exists(
os.path.join(os.environ['SCHRODINGER'], 'internal', '.buildtime')):
print_fields('Build Start Time: ', read_build_time())
# Check to make sure our commands succeeded
if UNKNOWN in (hostname, hostid):
return 1
return 0
[docs]def get_product_description(product_name, git_hashes):
"""
Return formatted string giving product name + version and corresponding
git repository hashes.
"""
product_string = product_name
product_string += " ("
padding = len(product_string)
separator = "\n" + (" " * padding)
formatted_hashes = [
f" {repo}:{git_hashes[repo]}" for repo in sorted(git_hashes)
]
product_string += separator.join(formatted_hashes)
product_string += " )"
return product_string
[docs]def get_product_git_hashes():
product_git_hashes = {}
SCHRODINGER = os.environ["SCHRODINGER"]
for filename in os.listdir(SCHRODINGER):
dirname = os.path.join(SCHRODINGER, filename)
if not os.path.isdir(dirname):
continue
if not re.search(r"-v[0-9]+(.[0-9]+)?$", filename):
continue
product_name = filename.split("-v")[0]
git_hashes = read_git_hash(os.path.join(dirname, ".git_hash"))
product_git_hashes[product_name] = git_hashes
return product_git_hashes
[docs]def print_product_versions(os_cpu_name):
product_git_hashes = get_product_git_hashes()
for product_name, git_hashes in sorted(product_git_hashes.items()):
product_version = str(
pymmlibs.mmfile_get_product_version(product_name)[1])
# Legacy format of mmshare-vxxxxx-OS_CPU
product_string = "-".join(
[product_name, "v" + product_version, os_cpu_name])
product = get_product_description(product_string, git_hashes)
print_fields("Product:", product)
[docs]def read_git_hash(filename):
"""
Reads hashes out of a file, and a dict with repo to repo hash mapping.
:param filename: name of file with hashes
:type filename: str
:rtype: dict
"""
if not os.path.exists(filename):
return {}
repo_hashes = {}
with open(filename) as fh:
for line in fh:
tokens = line.split()
if not tokens:
continue
if len(tokens) == 2:
repo, git_hash = tokens
repo_hashes[repo] = git_hash
return repo_hashes
[docs]def print_fields(name, value):
column_width = 15
# If value has newlines in it, make sure that they line up under previous
# values
value = str(value).replace("\n", "\n" + (" " * column_width))
print(f"{name:<{column_width}}{value}")
[docs]def get_buildtime_file():
schrodinger = os.environ['SCHRODINGER']
filename = os.path.join(schrodinger, 'internal', '.buildtime')
return filename
[docs]def read_build_time():
"""
Read from a file created by buildinger.py.
"""
filename = get_buildtime_file()
with open(filename) as fh:
for line in fh:
buildtime = line
return buildtime
if __name__ == "__main__":
sys.exit(main())