Skip to content

Commit

Permalink
Add CLI to dump environment and package info
Browse files Browse the repository at this point in the history
Resolves #161

**Why this change was necessary**
It's useful to the user to have a CLI tool to dump environment info
into stdout to help ST contributors with debugging.

**What this change does**
Adds 2 helper functions to get environment information including OS
and CPU details, as well as resource usage.

**Any side-effects?**
New dependency is added, so the shimmingtoolbox package will have to
be reinstalled locally.

**Additional context/notes/links**
#161 (comment)
  • Loading branch information
jidicula committed Nov 15, 2020
1 parent 15d2ec7 commit 8ab7468
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"scipy~=1.5.0",
"tqdm",
"matplotlib~=3.1.2",
"psutil~=5.7.3",
"pytest~=4.6.3",
"pytest-cov~=2.5.1",
],
Expand Down
64 changes: 64 additions & 0 deletions shimmingtoolbox/cli/check_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

import click
import subprocess
import os
import platform
import psutil

from typing import Dict, Tuple, List

CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
Expand Down Expand Up @@ -103,3 +107,63 @@ def get_dcm2niix_version() -> str:
version_output: str = dcm2niix_version.stderr.rstrip()
return version_output


@click.command(
context_settings=CONTEXT_SETTINGS,
help="Dumps environment and package details into stdout for debugging purposes."
)
def dump_env_info():
"""Dumps environment and package details into stdout for debugging purposes
by calling helper functions to retrieve these details.
"""
env_info = get_env_info()
pkg_version = get_pkg_info()

print(f"ENVIRONMENT INFO:\n{env_info}\n\nPACKAGE INFO:\n{pkg_version}")
return


def get_env_info() -> str:
"""Gets information about the environment.
This function gets information about the operating system, the host
machine hardware, Python version & implementation, and Python location.
Returns:
str: A multiline string containing environment info.
"""

os_name = os.name
cpu_arch = platform.machine()
platform_release = platform.release()
platform_system = platform.system()
platform_version = platform.version()
python_full_version = platform.python_version()
python_implementation = platform.python_implementation()

cpu_usage = f"CPU cores: Available: {psutil.cpu_count()}, Used by ITK functions: {int(os.getenv('ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS', 0))}"
ram = psutil.virtual_memory()
factor_MB = 1024 * 1024
ram_usage = f'RAM: Total: {ram.total // factor_MB}MB, Used: {ram.used // factor_MB}MB, Available: {ram.available // factor_MB}MB'

env_info = (f"{os_name} {cpu_arch}\n" +
f"{platform_system} {platform_release}\n" +
f"{platform_version}\n" +
f"{python_implementation} {python_full_version}\n\n" +
f"{cpu_usage}\n" +
f"{ram_usage}"
)
return env_info


def get_pkg_info() -> str:
"""Gets package version.
This function gets the version of shimming-toolbox.
Returns:
str: The version number of the shimming-toolbox installation.
"""
import shimmingtoolbox as st
pkg_version = st.__version__
return pkg_version

0 comments on commit 8ab7468

Please sign in to comment.