diff options
author | Eli Schwartz <eschwartz@archlinux.org> | 2023-02-07 23:26:50 -0500 |
---|---|---|
committer | Eli Schwartz <eschwartz@archlinux.org> | 2023-02-20 22:33:46 -0500 |
commit | 616bf1f79d855c6d5494f71806ce42961d8ce85e (patch) | |
tree | dbf6ee9754af7cf53d1d787fe560dd1f46e28ff8 | |
parent | 55abcbb8f60e1afe3af0133a6ff0e9dedc6df1b7 (diff) | |
download | meson-616bf1f79d855c6d5494f71806ce42961d8ce85e.zip meson-616bf1f79d855c6d5494f71806ce42961d8ce85e.tar.gz meson-616bf1f79d855c6d5494f71806ce42961d8ce85e.tar.bz2 |
minstall: add a timeout when waiting for user input for elevation
Do not allow hanging forever.
It's fine to use selectors here, which are unix-only, because we require
Unix + a system that has e.g. sudo installed, anyway.
-rw-r--r-- | mesonbuild/minstall.py | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/mesonbuild/minstall.py b/mesonbuild/minstall.py index 698feef..077823c 100644 --- a/mesonbuild/minstall.py +++ b/mesonbuild/minstall.py @@ -18,6 +18,7 @@ from pathlib import Path import argparse import errno import os +import selectors import shlex import shutil import subprocess @@ -566,12 +567,22 @@ class Installer: if rootcmd is not None: print('Installation failed due to insufficient permissions.') + s = selectors.DefaultSelector() + s.register(sys.stdin, selectors.EVENT_READ) + ans = None for attempt in range(5): - ans = input(f'Attempt to use {rootcmd} to gain elevated privileges? [y/n] ') + print(f'Attempt to use {rootcmd} to gain elevated privileges? [y/n] ', end='', flush=True) + if s.select(30): + # we waited on sys.stdin *only* + ans = sys.stdin.readline().rstrip('\n') + else: + print() + break if ans in {'y', 'n'}: break else: - raise MesonException('Answer not one of [y/n]') + if ans is not None: + raise MesonException('Answer not one of [y/n]') if ans == 'y': os.execlp(rootcmd, rootcmd, sys.executable, main_file, *sys.argv[1:], '-C', os.getcwd(), '--no-rebuild') |