do not try to read from stdin when it is non-interactive (#5733)

add '--no-input' command-line option and 'input' config file option
to allow users to manually configure this
pull/5748/head
Mike Fährmann 3 months ago
parent 76385f5f18
commit a1bb32792b
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

@ -461,6 +461,17 @@ Description
(see `getpass() <https://docs.python.org/3/library/getpass.html#getpass.getpass>`__). (see `getpass() <https://docs.python.org/3/library/getpass.html#getpass.getpass>`__).
extractor.*.input
-----------------
Type
``bool``
Default
``true`` if `stdin` is attached to a terminal ,
``false`` otherwise
Description
Allow prompting the user for interactive input.
extractor.*.netrc extractor.*.netrc
----------------- -----------------
Type Type

@ -30,6 +30,7 @@
-x, --input-file-delete FILE -x, --input-file-delete FILE
Download URLs found in FILE. Delete them after Download URLs found in FILE. Delete them after
they were downloaded successfully. they were downloaded successfully.
--no-input Do not prompt for passwords/tokens
## Output Options: ## Output Options:
-q, --quiet Activate quiet mode -q, --quiet Activate quiet mode

@ -11,6 +11,7 @@
import os import os
import re import re
import ssl import ssl
import sys
import time import time
import netrc import netrc
import queue import queue
@ -263,6 +264,10 @@ class Extractor():
time.sleep(seconds) time.sleep(seconds)
def input(self, prompt, echo=True): def input(self, prompt, echo=True):
if not self._input:
raise exception.StopExtraction(
"User input required (%s)", prompt.strip(" :"))
if echo: if echo:
try: try:
return input(prompt) return input(prompt)
@ -277,7 +282,13 @@ class Extractor():
password = None password = None
if username: if username:
password = self.config("password") or util.LazyPrompt() password = self.config("password")
if not password:
if not self._input:
raise exception.StopExtraction(
"User input required (password)")
password = util.LazyPrompt()
elif self.config("netrc", False): elif self.config("netrc", False):
try: try:
info = netrc.netrc().authenticators(self.category) info = netrc.netrc().authenticators(self.category)
@ -298,6 +309,7 @@ class Extractor():
self._retries = self.config("retries", 4) self._retries = self.config("retries", 4)
self._timeout = self.config("timeout", 30) self._timeout = self.config("timeout", 30)
self._verify = self.config("verify", True) self._verify = self.config("verify", True)
self._input = self.config("input")
self._proxies = util.build_proxy_map(self.config("proxy"), self.log) self._proxies = util.build_proxy_map(self.config("proxy"), self.log)
self._interval = util.build_duration_func( self._interval = util.build_duration_func(
self.config("sleep-request", self.request_interval), self.config("sleep-request", self.request_interval),
@ -307,6 +319,11 @@ class Extractor():
self.config("sleep-429", 60), self.config("sleep-429", 60),
) )
if self._input is None:
try:
self._input = sys.stdin.isatty()
except Exception:
self._input = False
if self._retries < 0: if self._retries < 0:
self._retries = float("inf") self._retries = float("inf")
if not self._retry_codes: if not self._retry_codes:

@ -256,6 +256,11 @@ def build_parser():
help=("Download URLs found in FILE. " help=("Download URLs found in FILE. "
"Delete them after they were downloaded successfully."), "Delete them after they were downloaded successfully."),
) )
input.add_argument(
"--no-input",
dest="input", nargs=0, action=ConfigConstAction, const=False,
help=("Do not prompt for passwords/tokens"),
)
output = parser.add_argument_group("Output Options") output = parser.add_argument_group("Output Options")
output.add_argument( output.add_argument(

Loading…
Cancel
Save