aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--allocbench/facter.py38
1 files changed, 18 insertions, 20 deletions
diff --git a/allocbench/facter.py b/allocbench/facter.py
index 5181d07..840aea6 100644
--- a/allocbench/facter.py
+++ b/allocbench/facter.py
@@ -24,18 +24,20 @@ import multiprocessing
import os
import platform
from subprocess import CalledProcessError
+from typing import Any, Dict, Tuple, Optional, Union
from allocbench.directories import get_allocbench_build_dir
from allocbench.util import run_cmd, get_logger
logger = get_logger(__file__)
-FACTS = {}
+FACTS: Dict[str, Any] = {}
+
+PathType = Union[str, os.PathLike]
def collect_facts():
"""Collect general facts about the benchmark environment"""
- # Populate allocbench.globalvars.facts on import
_uname = platform.uname()
FACTS["hostname"] = _uname.node
FACTS["system"] = _uname.system
@@ -56,10 +58,10 @@ def collect_facts():
FACTS["starttime"] = starttime
-def store_facts(path=None):
+def store_facts(path: Optional[PathType] = None):
"""Store facts to file"""
if not path:
- filename = "facts.json"
+ filename: PathType = "facts.json"
elif os.path.isdir(path):
filename = os.path.join(path, "facts.json")
else:
@@ -70,29 +72,25 @@ def store_facts(path=None):
json.dump(FACTS, facts_file)
-def load_facts(path=None):
+def load_facts(path: Optional[PathType] = None):
"""Load facts from file"""
if not path:
- filename = "facts"
- else:
- if os.path.isdir(path):
- filename = os.path.join(path, "facts")
- else:
- filename = os.path.splitext(path)[0]
-
- if os.path.exists(filename + ".json"):
- filename += ".json"
- with open(filename, "r") as facts_file:
- loaded_facts = json.load(facts_file)
+ filename: PathType = "facts.json"
+ elif os.path.isdir(path):
+ filename = os.path.join(path, "facts.json")
else:
+ filename = path
+
+ if not os.path.exists(filename):
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT),
filename)
- FACTS.update(loaded_facts)
logger.info("Loading facts from: %s", filename)
+ with open(filename, "r") as facts_file:
+ FACTS.update(json.load(facts_file))
-def allocbench_version():
+def allocbench_version() -> str:
"""Store and return allocbench version string."""
if "allocbench" in FACTS:
return FACTS["allocbench"]
@@ -109,7 +107,7 @@ def allocbench_version():
# Copied from pip.
# https://github.com/pypa/pip/blob/master/src/pip/_internal/utils/glibc.py
# Licensed under MIT.
-def glibc_version_string(executable=None):
+def glibc_version_string(executable=None) -> Optional[str]:
"Returns glibc version string, or None if not using glibc."
# ctypes.CDLL(None) internally calls dlopen(NULL), and as the dlopen
@@ -155,7 +153,7 @@ def glibc_version_string(executable=None):
# versions that was generated by pip 8.1.2 and earlier is useless and
# misleading. Solution: instead of using platform, use our code that actually
# works.
-def libc_ver(executable=None):
+def libc_ver(executable=None) -> Tuple:
"""Return glibc version or platform.libc_ver as fallback"""
glibc_version = glibc_version_string(executable)
if glibc_version is None: