changeset 11511:3110bea9a6b0

mx: add helper method for yes/no question
author Gilles Duboscq <duboscq@ssw.jku.at>
date Mon, 02 Sep 2013 11:40:24 +0200
parents 231958c9ddf9
children 38acec26d535
files mx/commands.py mxtool/mx.py
diffstat 2 files changed, 20 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/mx/commands.py	Mon Sep 02 20:44:49 2013 +0200
+++ b/mx/commands.py	Mon Sep 02 11:40:24 2013 +0200
@@ -95,8 +95,7 @@
         items = [k for k in _vmChoices.keys() if _vmChoices[k] is not None]
         descriptions = [_vmChoices[k] for k in _vmChoices.keys() if _vmChoices[k] is not None]
         vm = mx.select_items(items, descriptions, allowMultiple=False)
-        answer = raw_input('Persist this choice by adding "DEFAULT_VM=' + vm + '" to ' + envPath + '? [Yn]: ')
-        if not answer.lower().startswith('n'):
+        if mx.ask_yes_no('Persist this choice by adding "DEFAULT_VM=' + vm + '" to ' + envPath, 'y'):
             with open(envPath, 'a') as fp:
                 print >> fp, 'DEFAULT_VM=' + vm
     _vm = vm
@@ -281,8 +280,7 @@
 def _handle_missing_VM(bld, vm):
     mx.log('The ' + bld + ' ' + vm + ' VM has not been created')
     if sys.stdout.isatty():
-        answer = raw_input('Build it now? [Yn]: ')
-        if not answer.lower().startswith('n'):
+        if mx.ask_yes_no('Build it now', 'y'):
             with VM(vm, bld):
                 build([])
             return
--- a/mxtool/mx.py	Mon Sep 02 20:44:49 2013 +0200
+++ b/mxtool/mx.py	Mon Sep 02 11:40:24 2013 +0200
@@ -919,8 +919,7 @@
             break
     
     envPath = join(_mainSuite.dir, 'mx', 'env')
-    answer = raw_input('Persist this setting by adding "JAVA_HOME=' + javaHome + '" to ' + envPath + '? [Yn]: ')
-    if not answer.lower().startswith('n'):
+    if ask_yes_no('Persist this setting by adding "JAVA_HOME=' + javaHome + '" to ' + envPath, 'y'):
         with open(envPath, 'a') as fp:
             print >> fp, 'JAVA_HOME=' + javaHome
             
@@ -2928,7 +2927,7 @@
                 projectConfigFiles = frozenset(['.classpath', 'nbproject'])
                 indicators = projectConfigFiles.intersection(files)
                 if len(indicators) != 0:
-                    if not sys.stdout.isatty() or raw_input(currentDir + ' looks like a removed project -- delete it? [yn]: ') == 'y':
+                    if not sys.stdout.isatty() or ask_yes_no(currentDir + ' looks like a removed project -- delete it', 'n'):
                         shutil.rmtree(currentDir)
                         log('Deleted ' + currentDir)
 
@@ -3409,6 +3408,22 @@
             log(projectsFile)
             for p in s.projects:
                 log('\t' + p.name)
+                
+def ask_yes_no(question, default=None):
+    """"""
+    assert not default or default == 'y' or default == 'n'
+    if not sys.stdout.isatty():
+        if default:
+            return default
+        else:
+            abort("Can not answer '" + question + "?' if stdout is not a tty")
+    questionMark = '? [yn]: '
+    if default:
+        questionMark = questionMark.replace(default, default.upper())
+    answer = raw_input(question + questionMark) or default
+    while not answer:
+        answer = raw_input(question + questionMark)
+    return answer.lower().startswith('y')
 
 def add_argument(*args, **kwargs):
     """