-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sys_info utility to display dependencies version (#122)
* add sys_info utility * fix import * fix style
- Loading branch information
1 parent
a8416cc
commit 45659fa
Showing
8 changed files
with
187 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import argparse | ||
|
||
from .. import sys_info | ||
|
||
|
||
def run(): | ||
"""Run sys_info() command.""" | ||
parser = argparse.ArgumentParser( | ||
prog=f"{__package__.split('.')[0]}-sys_info", description="sys_info" | ||
) | ||
parser.add_argument( | ||
"--developer", | ||
help="display information for optional dependencies", | ||
action="store_true", | ||
) | ||
args = parser.parse_args() | ||
|
||
sys_info(developer=args.developer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import platform | ||
import sys | ||
from functools import partial | ||
from importlib.metadata import requires, version | ||
from typing import IO, Callable, List, Optional | ||
|
||
import psutil | ||
from packaging.requirements import Requirement | ||
|
||
from ._checks import _check_type | ||
|
||
|
||
def sys_info(fid: Optional[IO] = None, developer: bool = False): | ||
"""Print the system information for debugging. | ||
Parameters | ||
---------- | ||
fid : file-like | None | ||
The file to write to, passed to :func:`print`. Can be None to use | ||
:data:`sys.stdout`. | ||
developer : bool | ||
If True, display information about optional dependencies. | ||
""" | ||
_check_type(developer, (bool,), "developer") | ||
|
||
ljust = 26 | ||
out = partial(print, end="", file=fid) | ||
package = __package__.split(".")[0] | ||
|
||
# OS information - requires python 3.8 or above | ||
out("Platform:".ljust(ljust) + platform.platform() + "\n") | ||
# python information | ||
out("Python:".ljust(ljust) + sys.version.replace("\n", " ") + "\n") | ||
out("Executable:".ljust(ljust) + sys.executable + "\n") | ||
# CPU information | ||
out("CPU:".ljust(ljust) + platform.processor() + "\n") | ||
out("Physical cores:".ljust(ljust) + str(psutil.cpu_count(False)) + "\n") | ||
out("Logical cores:".ljust(ljust) + str(psutil.cpu_count(True)) + "\n") | ||
# memory information | ||
out("RAM:".ljust(ljust)) | ||
out(f"{psutil.virtual_memory().total / float(2 ** 30):0.1f} GB\n") | ||
out("SWAP:".ljust(ljust)) | ||
out(f"{psutil.swap_memory().total / float(2 ** 30):0.1f} GB\n") | ||
# package information | ||
out(f"{package}:".ljust(ljust) + version(package) + "\n") | ||
|
||
# dependencies | ||
out("\nCore dependencies\n") | ||
dependencies = [Requirement(elt) for elt in requires(package)] | ||
core_dependencies = [dep for dep in dependencies if "extra" not in str(dep.marker)] | ||
_list_dependencies_info(out, ljust, package, core_dependencies) | ||
|
||
# extras | ||
if developer: | ||
keys = ( | ||
"build", | ||
"docs", | ||
"test", | ||
"style", | ||
) | ||
for key in keys: | ||
extra_dependencies = [ | ||
dep | ||
for dep in dependencies | ||
if all(elt in str(dep.marker) for elt in ("extra", key)) | ||
] | ||
if len(extra_dependencies) == 0: | ||
continue | ||
out(f"\nOptional '{key}' dependencies\n") | ||
_list_dependencies_info(out, ljust, package, extra_dependencies) | ||
|
||
|
||
def _list_dependencies_info( | ||
out: Callable, ljust: int, package: str, dependencies: List[Requirement] | ||
): | ||
"""List dependencies names and versions.""" | ||
unicode = sys.stdout.encoding.lower().startswith("utf") | ||
if unicode: | ||
ljust += 1 | ||
|
||
not_found: List[Requirement] = list() | ||
for dep in dependencies: | ||
if dep.name == package: | ||
continue | ||
try: | ||
version_ = version(dep.name) | ||
except Exception: | ||
not_found.append(dep) | ||
continue | ||
|
||
# build the output string step by step | ||
output = f"✔︎ {dep.name}" if unicode else dep.name | ||
# handle version specifiers | ||
if len(dep.specifier) != 0: | ||
output += f" ({str(dep.specifier)})" | ||
output += ":" | ||
output = output.ljust(ljust) + version_ | ||
|
||
# handle special dependencies with backends, C dep, .. | ||
if dep.name in ("matplotlib", "seaborn") and version_ != "Not found.": | ||
try: | ||
from matplotlib import pyplot as plt | ||
|
||
backend = plt.get_backend() | ||
except Exception: | ||
backend = "Not found" | ||
|
||
output += f" (backend: {backend})" | ||
out(output + "\n") | ||
|
||
if len(not_found) != 0: | ||
not_found = [ | ||
f"{dep.name} ({str(dep.specifier)})" | ||
if len(dep.specifier) != 0 | ||
else dep.name | ||
for dep in not_found | ||
] | ||
if unicode: | ||
out(f"✘ Not installed: {', '.join(not_found)}\n") | ||
else: | ||
out(f"Not installed: {', '.join(not_found)}\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"""Test config.py""" | ||
|
||
from io import StringIO | ||
|
||
from pycrostates.utils.sys_info import sys_info | ||
|
||
|
||
def test_sys_info(): | ||
"""Test info-showing utility.""" | ||
out = StringIO() | ||
sys_info(fid=out) | ||
value = out.getvalue() | ||
out.close() | ||
assert "Platform:" in value | ||
assert "Executable:" in value | ||
assert "CPU:" in value | ||
assert "Physical cores:" in value | ||
assert "Logical cores" in value | ||
assert "RAM:" in value | ||
assert "SWAP:" in value | ||
|
||
assert "numpy" in value | ||
assert "psutil" in value | ||
|
||
assert "style" not in value | ||
assert "test" not in value | ||
|
||
out = StringIO() | ||
sys_info(fid=out, developer=True) | ||
value = out.getvalue() | ||
out.close() | ||
|
||
assert "build" in value | ||
assert "docs" in value | ||
assert "style" in value | ||
assert "test" in value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters