1 # Copyright (c) 2009, Google Inc. All rights reserved. 2 # 3 # Redistribution and use in source and binary forms, with or without 4 # modification, are permitted provided that the following conditions are 5 # met: 6 # 7 # * Redistributions of source code must retain the above copyright 8 # notice, this list of conditions and the following disclaimer. 9 # * Redistributions in binary form must reproduce the above 10 # copyright notice, this list of conditions and the following disclaimer 11 # in the documentation and/or other materials provided with the 12 # distribution. 13 # * Neither the name of Google Inc. nor the names of its 14 # contributors may be used to endorse or promote products derived from 15 # this software without specific prior written permission. 16 # 17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29 import getpass 30 import logging 31 import os 32 import re 33 import shlex 34 import subprocess 35 import sys 36 import webbrowser 37 38 39 _log = logging.getLogger("webkitpy.common.system.user") 40 41 42 try: 43 import readline 44 except ImportError: 45 if sys.platform != "win32": 46 # There is no readline module for win32, not much to do except cry. 47 _log.warn("Unable to import readline.") 48 # FIXME: We could give instructions for non-mac platforms. 49 # Lack of readline results in a very bad user experiance. 50 if sys.platform == "darwin": 51 _log.warn("If you're using MacPorts, try running:") 52 _log.warn(" sudo port install py25-readline") 53 54 55 class User(object): 56 DEFAULT_NO = 'n' 57 DEFAULT_YES = 'y' 58 59 # FIXME: These are @classmethods because bugzilla.py doesn't have a Tool object (thus no User instance). 60 @classmethod 61 def prompt(cls, message, repeat=1, raw_input=raw_input): 62 response = None 63 while (repeat and not response): 64 repeat -= 1 65 response = raw_input(message) 66 return response 67 68 @classmethod 69 def prompt_password(cls, message, repeat=1): 70 return cls.prompt(message, repeat=repeat, raw_input=getpass.getpass) 71 72 @classmethod 73 def prompt_with_list(cls, list_title, list_items, can_choose_multiple=False, raw_input=raw_input): 74 print list_title 75 i = 0 76 for item in list_items: 77 i += 1 78 print "%2d. %s" % (i, item) 79 80 # Loop until we get valid input 81 while True: 82 if can_choose_multiple: 83 response = cls.prompt("Enter one or more numbers (comma-separated), or \"all\": ", raw_input=raw_input) 84 if not response.strip() or response == "all": 85 return list_items 86 try: 87 indices = [int(r) - 1 for r in re.split("\s*,\s*", response)] 88 except ValueError, err: 89 continue 90 return [list_items[i] for i in indices] 91 else: 92 try: 93 result = int(cls.prompt("Enter a number: ", raw_input=raw_input)) - 1 94 except ValueError, err: 95 continue 96 return list_items[result] 97 98 def edit(self, files): 99 editor = os.environ.get("EDITOR") or "vi" 100 args = shlex.split(editor) 101 # Note: Not thread safe: http://bugs.python.org/issue2320 102 subprocess.call(args + files) 103 104 def _warn_if_application_is_xcode(self, edit_application): 105 if "Xcode" in edit_application: 106 print "Instead of using Xcode.app, consider using EDITOR=\"xed --wait\"." 107 108 def edit_changelog(self, files): 109 edit_application = os.environ.get("CHANGE_LOG_EDIT_APPLICATION") 110 if edit_application and sys.platform == "darwin": 111 # On Mac we support editing ChangeLogs using an application. 112 args = shlex.split(edit_application) 113 print "Using editor in the CHANGE_LOG_EDIT_APPLICATION environment variable." 114 print "Please quit the editor application when done editing." 115 self._warn_if_application_is_xcode(edit_application) 116 subprocess.call(["open", "-W", "-n", "-a"] + args + files) 117 return 118 self.edit(files) 119 120 def page(self, message): 121 pager = os.environ.get("PAGER") or "less" 122 try: 123 # Note: Not thread safe: http://bugs.python.org/issue2320 124 child_process = subprocess.Popen([pager], stdin=subprocess.PIPE) 125 child_process.communicate(input=message) 126 except IOError, e: 127 pass 128 129 def confirm(self, message=None, default=DEFAULT_YES, raw_input=raw_input): 130 if not message: 131 message = "Continue?" 132 choice = {'y': 'Y/n', 'n': 'y/N'}[default] 133 response = raw_input("%s [%s]: " % (message, choice)) 134 if not response: 135 response = default 136 return response.lower() == 'y' 137 138 def can_open_url(self): 139 try: 140 webbrowser.get() 141 return True 142 except webbrowser.Error, e: 143 return False 144 145 def open_url(self, url): 146 if not self.can_open_url(): 147 _log.warn("Failed to open %s" % url) 148 webbrowser.open(url) 149