[util] Add --force option to get-toolchain.py

If the user calls get-toolchain.py with the --update option, the
user is prompted before deleting the target directory and proceeding
with the install. If the --force option is used, only a warning is shown
instead.
diff --git a/util/get-toolchain.py b/util/get-toolchain.py
index f8d6451..d5e45c1 100755
--- a/util/get-toolchain.py
+++ b/util/get-toolchain.py
@@ -12,6 +12,7 @@
 import shutil
 import sys
 import tempfile
+import time
 from urllib.request import urlopen, urlretrieve
 
 ASSET_PREFIX = "lowrisc-toolchain-gcc-rv32imc-"
@@ -22,6 +23,18 @@
 TOOLCHAIN_VERSION = 'latest'
 
 
+def prompt_yes_no(msg):
+    while True:
+        print(msg, end=" ")
+        response = input().lower()
+        if response in ('yes', 'y'):
+            return True
+        elif response in ('no', 'n'):
+            return False
+        else:
+            print('Invalid response. Valid options are "yes" or "no"')
+
+
 def get_release_info(toolchain_version):
     if toolchain_version == 'latest':
         releases_url = '%s/%s' % (RELEASES_URL_BASE, toolchain_version)
@@ -52,7 +65,7 @@
         Release tag string if available, otherwise None.
     """
     with open(buildinfo_file, 'r') as f:
-        match = re.match(r"Version:\n(?P<version>\d+-\d+)", f.read(), re.M)
+        match = re.match(r"Version:\n(?P<version>\d+(-\d+)?)", f.read(), re.M)
     if not match:
         return None
     return match.group("version")
@@ -65,9 +78,7 @@
     return tmpfile
 
 
-def install(archive_file, target_dir, update):
-    if os.path.exists(target_dir) and update:
-        shutil.rmtree(target_dir)
+def install(archive_file, target_dir):
     os.makedirs(target_dir)
 
     cmd = [
@@ -98,6 +109,14 @@
         default=False,
         action='store_true',
         help="Set to update to target version if needed (default: %(default)s)")
+    parser.add_argument(
+        '--force',
+        '-f',
+        required=False,
+        default=False,
+        action='store_true',
+        help="Set to skip directory erase prompt when --update is set "
+            "(default: %(default)s)")
     args = parser.parse_args()
 
     target_dir = Path(args.target_dir)
@@ -116,18 +135,26 @@
                 'directory and try again.' % buildinfo_file)
         current_release_tag = get_release_tag_from_file(buildinfo_file)
         if not current_release_tag:
-            sys.exit('Unable to extract current toolchain version from %s.'
+            sys.exit('Unable to extract current toolchain version from %s. '
                 'Delete target directory and try again.' % buildinfo_file)
         if get_release_tag(release_info) == current_release_tag:
             print('Toolchain version %s already installed at %s. Skipping '
                 'install.' % (current_release_tag, target_dir))
             return
-        print('Cleaning target_dir before attempting install.')
 
     download_url = get_download_url(release_info)
     try:
         archive_file = download(download_url)
-        install(archive_file, target_dir, args.update)
+        if args.update and os.path.exists(args.target_dir):
+            # Warn the user before deleting the target directory.
+            warning_msg = 'WARNING: Removing directory: %s.' % target_dir
+            if not args.force:
+                if not prompt_yes_no(warning_msg + ' Continue [yes/no]:'):
+                    sys.exit('Aborting update.')
+            else:
+                print(warning_msg)
+            shutil.rmtree(target_dir)
+        install(archive_file, target_dir)
     finally:
         os.remove(archive_file)