mirror of
https://git.launchpad.net/ubuntu-dev-tools
synced 2025-03-12 23:51:08 +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
|
||||
# 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):
|
||||
if default:
|
||||
question += " [%i]? " % (default)
|
||||
@ -35,44 +80,3 @@ def input_number(question, min_number, max_number, default=None):
|
||||
print "Please input a number."
|
||||
assert type(selected) == int
|
||||
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
|
||||
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.patch import Patch
|
||||
|
||||
USER_ABORT = 2
|
||||
def user_abort():
|
||||
print "User abort."
|
||||
sys.exit(2)
|
||||
|
||||
def get_source_package_name(bug_task):
|
||||
package = None
|
||||
@ -87,9 +89,9 @@ def strip_epoch(version):
|
||||
return version_without_epoch
|
||||
|
||||
def ask_for_manual_fixing():
|
||||
if not boolean_question("Do you want to resolve this issue manually", True):
|
||||
print "Abort."
|
||||
sys.exit(USER_ABORT)
|
||||
question = Question(["yes", "no"], False)
|
||||
if question.ask("Do you want to resolve this issue manually", True) == "no":
|
||||
user_abort()
|
||||
|
||||
def get_patch_or_branch(bug):
|
||||
patch = None
|
||||
@ -447,14 +449,13 @@ def main(bug_number, update, build, edit, keyid, upload, workdir, builder,
|
||||
"file://%s" % (task.package, new_version,
|
||||
debdiff_filename, lintian_filename,
|
||||
build_log)
|
||||
answer = yes_edit_no_question("Do you want to upload the " \
|
||||
"package to the official " \
|
||||
"Ubuntu archive", "yes")
|
||||
question = Question(["yes", "edit", "no"])
|
||||
answer = question.ask("Do you want to upload the package to " \
|
||||
"the official Ubuntu archive", "yes")
|
||||
if answer == "edit":
|
||||
continue
|
||||
elif answer == "no":
|
||||
print "Abort."
|
||||
sys.exit(USER_ABORT)
|
||||
user_abort()
|
||||
cmd = ["dput", "--force", upload, changes_file]
|
||||
Logger.command(cmd)
|
||||
if subprocess.call(cmd) != 0:
|
||||
|
Loading…
x
Reference in New Issue
Block a user