diff options
author | Martin Braun <martin.braun@ettus.com> | 2018-10-29 15:37:30 -0700 |
---|---|---|
committer | Brent Stapleton <bstapleton@g.hmc.edu> | 2018-11-13 12:12:09 -0800 |
commit | a6943f00f9d3db1df1f736961ec4ad5a65299605 (patch) | |
tree | a0452d4c08febe96ecc36c8b27d36653feb15c56 | |
parent | 1e6d22398f447e41f718313c596a833c8b9c7f14 (diff) | |
download | uhd-a6943f00f9d3db1df1f736961ec4ad5a65299605.tar.gz uhd-a6943f00f9d3db1df1f736961ec4ad5a65299605.tar.bz2 uhd-a6943f00f9d3db1df1f736961ec4ad5a65299605.zip |
utils: uhd_images_downloader: Add --yes option
This will answer 'yes' to all questions that are asked and allows to
script the downloader. Example:
$ uhd_images_downloader -t sdimg -y
Any interaction that the script will have with the user will be skipped.
-rw-r--r-- | host/utils/uhd_images_downloader.py.in | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/host/utils/uhd_images_downloader.py.in b/host/utils/uhd_images_downloader.py.in index ebfdd9669..6655274dd 100644 --- a/host/utils/uhd_images_downloader.py.in +++ b/host/utils/uhd_images_downloader.py.in @@ -22,6 +22,10 @@ try: from urllib.parse import urljoin # Python 3 except ImportError: from urlparse import urljoin # Python 2 +try: + from builtins import input +except ImportError: + input = raw_input from six import iteritems import requests @@ -43,6 +47,7 @@ _LOG_LEVELS = {"TRACE": 1, "WARN": 4, "ERROR": 5} _LOG_LEVEL = _LOG_LEVELS["INFO"] +_YES = False def log(level, message): @@ -81,6 +86,8 @@ def parse_args(): help="Keep the downloaded images archives in the image directory") parser.add_argument("-T", "--test", action="store_true", default=False, help="Verify the downloaded archives before extracting them") + parser.add_argument("-y", "--yes", action="store_true", default=False, + help="Answer all questions with 'yes' (for scripting purposes).") parser.add_argument("-n", "--dry-run", action="store_true", default=False, help="Print selected target without actually downloading them.") parser.add_argument("--refetch", action="store_true", default=False, @@ -93,6 +100,24 @@ def parse_args(): return parser.parse_args() +def ask_permission(question, default_no=True): + """ + Ask the question, and have the user type y or n on the keyboard. If the + global variable _YES is true, this always returns True without asking the + question. Otherwise, return True if the answer is 'yes', 'y', or something + similar. + """ + if _YES: + log("DEBUG", "Assuming the answer is 'yes' for this question: " + + question) + return True + postfix = "[y/N]" if default_no else "[Y/n]" + answer = input(question + " " + postfix) + if answer and answer[0].lower() == 'y': + return True + return False + + class TemporaryDirectory: """Class to create a temporary directory""" def __enter__(self): |