Skip to content

Commit

Permalink
fail if ui asks for input without a terminal
Browse files Browse the repository at this point in the history
This commit attempts to fix the issue where
`get` stalls if an annex remote tries to ask for
credentials in a process that has no terminal
attached.
  • Loading branch information
christian-monch committed Apr 10, 2024
1 parent b50f5d3 commit c8239fe
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
26 changes: 26 additions & 0 deletions datalad/ui/dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from ..utils import auto_repr
from ..utils import on_windows
from .base import InteractiveUI
from .utils import has_terminal
from datalad.support.exceptions import CapturedException

# Example APIs which might be useful to look for "inspiration"
Expand Down Expand Up @@ -357,6 +358,31 @@ def get_progressbar(self, *args, **kwargs):
return super(UnderAnnexUI, self).get_progressbar(
*args, **kwargs)

def input(self, prompt, hidden=False):
if not has_terminal():
# we are not interactive
raise RuntimeError('Interactive input not available for `ui.input()` in annex remotes')
return super(UnderAnnexUI, self).input(prompt, hidden)

def question(self,
text,
title=None,
choices=None,
default=None,
hidden=False,
repeat=None):
if not has_terminal():
# we are not interactive
raise RuntimeError('Interactive input not available for `ui.question()` in annex remotes')
return super(UnderAnnexUI, self).question(
text,
title=title,
choices=choices,
default=default,
hidden=hidden,
repeat=repeat
)


@auto_repr
class UnderTestsUI(DialogUI):
Expand Down
14 changes: 13 additions & 1 deletion datalad/ui/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,16 @@ def show_hint(msg):
ui.message("{}".format(
ansi_colors.color_word(
msg,
ansi_colors.YELLOW)))
ansi_colors.YELLOW)))


def has_terminal():
"""Return True if the process has a controlling terminal
This checks for a controlling terminal on Linux
"""
try:
open('/dev/tty', 'r').close()
return True
except IOError as exc:
return False

0 comments on commit c8239fe

Please sign in to comment.