From a6943f00f9d3db1df1f736961ec4ad5a65299605 Mon Sep 17 00:00:00 2001
From: Martin Braun <martin.braun@ettus.com>
Date: Mon, 29 Oct 2018 15:37:30 -0700
Subject: 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.
---
 host/utils/uhd_images_downloader.py.in | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

(limited to 'host/utils')

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):
-- 
cgit v1.2.3