Skip to content

Commit

Permalink
Merge pull request #6664 from adswa/wtf-fs
Browse files Browse the repository at this point in the history
NF: Teach WTF to report the filesystem of CWD, TMP, and HOME
  • Loading branch information
bpoldrack committed May 11, 2022
2 parents 055c383 + 34738bf commit efc7d21
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
48 changes: 48 additions & 0 deletions datalad/local/wtf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import os
import os.path as op
import sys
import tempfile
from functools import partial
from collections import OrderedDict

Expand Down Expand Up @@ -138,9 +139,39 @@ def _describe_system():
_t2s(pl.win32_ver())]).rstrip(),
'max_path_length': get_max_path_length(getpwd()),
'encoding': get_encoding_info(),
'filesystem': {l: _get_fs_type(l, p) for l, p in
[('CWD', Path.cwd()),
('TMP', Path(tempfile.gettempdir())),
('HOME', Path.home())]}
}


def _get_fs_type(loc, path):
"""Return file system info for given Paths. Provide pathlib path as input"""
res = {'path': path}
try:
from psutil import disk_partitions
parts = {Path(p.mountpoint): p for p in disk_partitions()}
match = None
for mp in parts:
# if the mountpoint is the test path or its parent
# take it, whenever there is no match, or a longer match
if (mp == path or mp in path.parents) and (
match is None or len(p.parents) > len(match.parents)):
match = mp
match = parts[match]
for sattr, tattr in (('fstype', 'type'),
('maxpath', 'max_pathlength'),
('opts', 'mount_opts')):
if hasattr(match, sattr):
res[tattr] = getattr(match, sattr)
except Exception as exc:
ce = CapturedException(exc)
# if an exception occurs, leave out the fs type. The result renderer can
# display a hint based on its lack in the report
return res


def _describe_environment():
from datalad import get_envvars_info
return get_envvars_info()
Expand Down Expand Up @@ -481,6 +512,23 @@ def custom_result_renderer(res, **kwargs):
from datalad.ui import ui
out = _render_report(res)
ui.message(out)
# add any necessary hints afterwards
maybe_show_hints(res)


def maybe_show_hints(res):
"""Helper to add hints to custom result rendering"""
# check for missing file system info
# if the system record lacks file system information, hint at a psutil
# problem. Query for TMP should be safe, is included in system info
from datalad.ui.utils import show_hint

if 'system' in res.get('infos', {}) and \
'type' not in res['infos']['system']['filesystem']['TMP']:
try:
import psutil
except ImportError:
show_hint('Hint: install psutil to get filesystem information')


def _render_report(res):
Expand Down
9 changes: 9 additions & 0 deletions datalad/ui/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
"""Various utils oriented to UI"""

from datalad.support import ansi_colors
from datalad.utils import on_windows
import struct

Expand Down Expand Up @@ -65,3 +66,11 @@ def get_console_width(default_min=20):
# to prevent crashes below guarantee that it is at least 20
console_width = 20
return console_width


def show_hint(msg):
from datalad.ui import ui
ui.message("{}".format(
ansi_colors.color_word(
msg,
ansi_colors.YELLOW)))

0 comments on commit efc7d21

Please sign in to comment.