mirror of
https://git.launchpad.net/ubuntu-dev-tools
synced 2025-03-16 01:21:07 +00:00
ubuntutools/question.py: Create a universal Question object.
This commit is contained in:
parent
9e6a13f5a9
commit
4178bf7476
@ -15,6 +15,51 @@
|
|||||||
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
|
||||||
|
class Question(object):
|
||||||
|
def __init__(self, options, show_help=True):
|
||||||
|
assert len(options) >= 2
|
||||||
|
self.options = map(lambda s: s.lower(), options)
|
||||||
|
self.show_help = show_help
|
||||||
|
|
||||||
|
def get_options(self):
|
||||||
|
if len(self.options) == 2:
|
||||||
|
options = self.options[0] + " or " + self.options[1]
|
||||||
|
else:
|
||||||
|
options = ", ".join(self.options[:-1]) + ", or " + self.options[-1]
|
||||||
|
return options
|
||||||
|
|
||||||
|
def ask(self, question, default=None):
|
||||||
|
if default is None:
|
||||||
|
default = self.options[0]
|
||||||
|
assert default in self.options
|
||||||
|
|
||||||
|
separator = " ["
|
||||||
|
for option in self.options:
|
||||||
|
if option == default:
|
||||||
|
question += separator + option[0].upper()
|
||||||
|
else:
|
||||||
|
question += separator + option[0]
|
||||||
|
separator = "|"
|
||||||
|
if self.show_help:
|
||||||
|
question += "|?"
|
||||||
|
question += "]? "
|
||||||
|
|
||||||
|
selected = None
|
||||||
|
while selected not in self.options:
|
||||||
|
selected = raw_input(question).strip().lower()
|
||||||
|
if selected == "":
|
||||||
|
selected = default
|
||||||
|
else:
|
||||||
|
for option in self.options:
|
||||||
|
# Example: User typed "y" instead of "yes".
|
||||||
|
if selected == option[0]:
|
||||||
|
selected = option
|
||||||
|
if selected not in self.options:
|
||||||
|
print "Please answer the question with " + \
|
||||||
|
self.get_options() + "."
|
||||||
|
return selected
|
||||||
|
|
||||||
|
|
||||||
def input_number(question, min_number, max_number, default=None):
|
def input_number(question, min_number, max_number, default=None):
|
||||||
if default:
|
if default:
|
||||||
question += " [%i]? " % (default)
|
question += " [%i]? " % (default)
|
||||||
@ -35,44 +80,3 @@ def input_number(question, min_number, max_number, default=None):
|
|||||||
print "Please input a number."
|
print "Please input a number."
|
||||||
assert type(selected) == int
|
assert type(selected) == int
|
||||||
return selected
|
return selected
|
||||||
|
|
||||||
def boolean_question(question, default):
|
|
||||||
if default is True:
|
|
||||||
question += " [Y/n]? "
|
|
||||||
else:
|
|
||||||
question += " [y/N]? "
|
|
||||||
selected = None
|
|
||||||
while type(selected) != bool:
|
|
||||||
selected = raw_input(question).strip().lower()
|
|
||||||
if selected == "":
|
|
||||||
selected = default
|
|
||||||
elif selected in ("y", "yes"):
|
|
||||||
selected = True
|
|
||||||
elif selected in ("n", "no"):
|
|
||||||
selected = False
|
|
||||||
else:
|
|
||||||
print "Please answer the question with yes or no."
|
|
||||||
return selected
|
|
||||||
|
|
||||||
def yes_edit_no_question(question, default):
|
|
||||||
assert default in ("yes", "edit", "no")
|
|
||||||
if default == "yes":
|
|
||||||
question += " [Y/e/n]? "
|
|
||||||
elif default == "edit":
|
|
||||||
question += " [y/E/n]? "
|
|
||||||
else:
|
|
||||||
question += " [y/e/N]? "
|
|
||||||
selected = None
|
|
||||||
while selected not in ("yes", "edit", "no"):
|
|
||||||
selected = raw_input(question).strip().lower()
|
|
||||||
if selected == "":
|
|
||||||
selected = default
|
|
||||||
elif selected in ("y", "yes"):
|
|
||||||
selected = "yes"
|
|
||||||
elif selected in ("e", "edit"):
|
|
||||||
selected = "edit"
|
|
||||||
elif selected in ("n", "no"):
|
|
||||||
selected = "no"
|
|
||||||
else:
|
|
||||||
print "Please answer the question with yes, edit, or no."
|
|
||||||
return selected
|
|
||||||
|
@ -28,12 +28,14 @@ import launchpadlib.launchpad
|
|||||||
|
|
||||||
import ubuntutools.update_maintainer
|
import ubuntutools.update_maintainer
|
||||||
from ubuntutools.logger import Logger
|
from ubuntutools.logger import Logger
|
||||||
from ubuntutools.question import input_number, boolean_question, yes_edit_no_question
|
from ubuntutools.question import Question, input_number
|
||||||
|
|
||||||
from ubuntutools.sponsor_patch.bugtask import BugTask
|
from ubuntutools.sponsor_patch.bugtask import BugTask
|
||||||
from ubuntutools.sponsor_patch.patch import Patch
|
from ubuntutools.sponsor_patch.patch import Patch
|
||||||
|
|
||||||
USER_ABORT = 2
|
def user_abort():
|
||||||
|
print "User abort."
|
||||||
|
sys.exit(2)
|
||||||
|
|
||||||
def get_source_package_name(bug_task):
|
def get_source_package_name(bug_task):
|
||||||
package = None
|
package = None
|
||||||
@ -87,9 +89,9 @@ def strip_epoch(version):
|
|||||||
return version_without_epoch
|
return version_without_epoch
|
||||||
|
|
||||||
def ask_for_manual_fixing():
|
def ask_for_manual_fixing():
|
||||||
if not boolean_question("Do you want to resolve this issue manually", True):
|
question = Question(["yes", "no"], False)
|
||||||
print "Abort."
|
if question.ask("Do you want to resolve this issue manually", True) == "no":
|
||||||
sys.exit(USER_ABORT)
|
user_abort()
|
||||||
|
|
||||||
def get_patch_or_branch(bug):
|
def get_patch_or_branch(bug):
|
||||||
patch = None
|
patch = None
|
||||||
@ -447,14 +449,13 @@ def main(bug_number, update, build, edit, keyid, upload, workdir, builder,
|
|||||||
"file://%s" % (task.package, new_version,
|
"file://%s" % (task.package, new_version,
|
||||||
debdiff_filename, lintian_filename,
|
debdiff_filename, lintian_filename,
|
||||||
build_log)
|
build_log)
|
||||||
answer = yes_edit_no_question("Do you want to upload the " \
|
question = Question(["yes", "edit", "no"])
|
||||||
"package to the official " \
|
answer = question.ask("Do you want to upload the package to " \
|
||||||
"Ubuntu archive", "yes")
|
"the official Ubuntu archive", "yes")
|
||||||
if answer == "edit":
|
if answer == "edit":
|
||||||
continue
|
continue
|
||||||
elif answer == "no":
|
elif answer == "no":
|
||||||
print "Abort."
|
user_abort()
|
||||||
sys.exit(USER_ABORT)
|
|
||||||
cmd = ["dput", "--force", upload, changes_file]
|
cmd = ["dput", "--force", upload, changes_file]
|
||||||
Logger.command(cmd)
|
Logger.command(cmd)
|
||||||
if subprocess.call(cmd) != 0:
|
if subprocess.call(cmd) != 0:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user