From 90541738bca7b921b86dfbba1e80de3c26d8fc8d Mon Sep 17 00:00:00 2001 From: "maruel@chromium.org" Date: Fri, 1 Apr 2011 17:54:18 +0000 Subject: [PATCH] Wrap raw_input() around a try/except to reduce the number of false-positive in breakpad R=dpranke@chromium.org BUG= TEST= Review URL: http://codereview.chromium.org/6706007 git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@80182 0039d316-1c4b-4281-b951-d872f2087c98 --- gclient_scm.py | 27 ++++++++++++++++++--------- git_cl.py | 19 ++++++++++++++----- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/gclient_scm.py b/gclient_scm.py index 38656431a..30e604dd7 100644 --- a/gclient_scm.py +++ b/gclient_scm.py @@ -8,6 +8,7 @@ import logging import os import posixpath import re +import sys import time import scm @@ -49,6 +50,14 @@ class DiffFilterer(object): print(line) +def ask_for_data(prompt): + try: + return raw_input(prompt) + except KeyboardInterrupt: + # Hide the exception. + sys.exit(1) + + ### SCM abstraction layer # Factory Method for SCM wrapper creation @@ -353,11 +362,11 @@ class GitWrapper(SCMWrapper): while True: try: # TODO(maruel): That can't work with --jobs. - action = str(raw_input("Cannot fast-forward merge, attempt to " - "rebase? (y)es / (q)uit / (s)kip : ")) + action = ask_for_data( + 'Cannot fast-forward merge, attempt to rebase? ' + '(y)es / (q)uit / (s)kip : ') except ValueError: - gclient_utils.Error('Invalid Character') - continue + raise gclient_utils.Error('Invalid Character') if re.match(r'yes|y', action, re.I): self._AttemptRebase(upstream_branch, files, options, printed_path=printed_path) @@ -540,11 +549,11 @@ class GitWrapper(SCMWrapper): re.match(r'cannot rebase: your index contains uncommitted changes', e.stderr)): while True: - rebase_action = str(raw_input("Cannot rebase because of unstaged " - "changes.\n'git reset --hard HEAD' ?\n" - "WARNING: destroys any uncommitted " - "work in your current branch!" - " (y)es / (q)uit / (s)how : ")) + rebase_action = ask_for_data( + 'Cannot rebase because of unstaged changes.\n' + '\'git reset --hard HEAD\' ?\n' + 'WARNING: destroys any uncommitted work in your current branch!' + ' (y)es / (q)uit / (s)how : ') if re.match(r'yes|y', rebase_action, re.I): self._Run(['reset', '--hard', 'HEAD'], options) # Should this be recursive? diff --git a/git_cl.py b/git_cl.py index 53258c675..e7569e266 100755 --- a/git_cl.py +++ b/git_cl.py @@ -43,6 +43,7 @@ DEFAULT_SERVER = 'http://codereview.appspot.com' POSTUPSTREAM_HOOK_PATTERN = '.git/hooks/post-cl-%s' DESCRIPTION_BACKUP_FILE = '~/.git_cl_description_backup' + def DieWithError(message): print >> sys.stderr, message sys.exit(1) @@ -98,6 +99,14 @@ def usage(more): return hook +def ask_for_data(prompt): + try: + return raw_input(prompt) + except KeyboardInterrupt: + # Hide the exception. + sys.exit(1) + + def FixUrl(server): """Fix a server url to defaults protocol to http:// if none is specified.""" if not server: @@ -526,7 +535,7 @@ def GetCodereviewSettingsInteractively(): server = settings.GetDefaultServerUrl(error_ok=True) prompt = 'Rietveld server (host[:port])' prompt += ' [%s]' % (server or DEFAULT_SERVER) - newserver = raw_input(prompt + ': ') + newserver = ask_for_data(prompt + ':') if not server and not newserver: newserver = DEFAULT_SERVER if newserver and newserver != server: @@ -536,7 +545,7 @@ def GetCodereviewSettingsInteractively(): prompt = caption if initial: prompt += ' ("x" to clear) [%s]' % initial - new_val = raw_input(prompt + ': ') + new_val = ask_for_data(prompt + ':') if new_val == 'x': RunGit(['config', '--unset-all', 'rietveld.' + name], error_ok=True) elif new_val and new_val != initial: @@ -1113,7 +1122,7 @@ def SendUpstream(parser, args, cmd): branches = [base_branch, cl.GetBranchRef()] if not options.force: subprocess.call(['git', 'diff', '--stat'] + branches) - raw_input("About to commit; enter to confirm.") + ask_for_data('About to commit; enter to confirm.') # We want to squash all this branch's commits into one commit with the # proper description. @@ -1198,7 +1207,7 @@ to run 'git cl push' instead. Choose wisely, if you get this wrong, your commit might appear to succeed but will instead be silently ignored.""" print(message) - raw_input('[Press enter to dcommit or ctrl-C to quit]') + ask_for_data('[Press enter to dcommit or ctrl-C to quit]') return SendUpstream(parser, args, 'dcommit') @@ -1208,7 +1217,7 @@ def CMDpush(parser, args): if settings.GetIsGitSvn(): print('This appears to be an SVN repository.') print('Are you sure you didn\'t mean \'git cl dcommit\'?') - raw_input('[Press enter to push or ctrl-C to quit]') + ask_for_data('[Press enter to push or ctrl-C to quit]') return SendUpstream(parser, args, 'push')