WebKitTools/Scripts/webkitpy/common/system/user.py
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     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 logging
       
    30 import os
       
    31 import shlex
       
    32 import subprocess
       
    33 import sys
       
    34 import webbrowser
       
    35 
       
    36 
       
    37 _log = logging.getLogger("webkitpy.common.system.user")
       
    38 
       
    39 
       
    40 try:
       
    41     import readline
       
    42 except ImportError:
       
    43     if sys.platform != "win32":
       
    44         # There is no readline module for win32, not much to do except cry.
       
    45         _log.warn("Unable to import readline.")
       
    46     # FIXME: We could give instructions for non-mac platforms.
       
    47     # Lack of readline results in a very bad user experiance.
       
    48     if sys.platform == "mac":
       
    49         _log.warn("If you're using MacPorts, try running:")
       
    50         _log.warn("  sudo port install py25-readline")
       
    51 
       
    52 
       
    53 class User(object):
       
    54     # FIXME: These are @classmethods because bugzilla.py doesn't have a Tool object (thus no User instance).
       
    55     @classmethod
       
    56     def prompt(cls, message, repeat=1, raw_input=raw_input):
       
    57         response = None
       
    58         while (repeat and not response):
       
    59             repeat -= 1
       
    60             response = raw_input(message)
       
    61         return response
       
    62 
       
    63     @classmethod
       
    64     def prompt_with_list(cls, list_title, list_items):
       
    65         print list_title
       
    66         i = 0
       
    67         for item in list_items:
       
    68             i += 1
       
    69             print "%2d. %s" % (i, item)
       
    70         result = int(cls.prompt("Enter a number: ")) - 1
       
    71         return list_items[result]
       
    72 
       
    73     def edit(self, files):
       
    74         editor = os.environ.get("EDITOR") or "vi"
       
    75         args = shlex.split(editor)
       
    76         # Note: Not thread safe: http://bugs.python.org/issue2320
       
    77         subprocess.call(args + files)
       
    78 
       
    79     def edit_changelog(self, files):
       
    80         edit_application = os.environ.get("CHANGE_LOG_EDIT_APPLICATION")
       
    81         if edit_application and sys.platform == "darwin":
       
    82             # On Mac we support editing ChangeLogs using an application.
       
    83             args = shlex.split(edit_application)
       
    84             print "Using editor in the CHANGE_LOG_EDIT_APPLICATION environment variable."
       
    85             print "Please quit the editor application when done editing."
       
    86             if edit_application.find("Xcode.app"):
       
    87                 print "Instead of using Xcode.app, consider using EDITOR=\"xed --wait\"."
       
    88             subprocess.call(["open", "-W", "-n", "-a"] + args + files)
       
    89             return
       
    90         self.edit(files)
       
    91 
       
    92     def page(self, message):
       
    93         pager = os.environ.get("PAGER") or "less"
       
    94         try:
       
    95             # Note: Not thread safe: http://bugs.python.org/issue2320
       
    96             child_process = subprocess.Popen([pager], stdin=subprocess.PIPE)
       
    97             child_process.communicate(input=message)
       
    98         except IOError, e:
       
    99             pass
       
   100 
       
   101     def confirm(self, message=None):
       
   102         if not message:
       
   103             message = "Continue?"
       
   104         response = raw_input("%s [Y/n]: " % message)
       
   105         return not response or response.lower() == "y"
       
   106 
       
   107     def can_open_url(self):
       
   108         try:
       
   109             webbrowser.get()
       
   110             return True
       
   111         except webbrowser.Error, e:
       
   112             return False
       
   113 
       
   114     def open_url(self, url):
       
   115         if not self.can_open_url():
       
   116             _log.warn("Failed to open %s" % url)
       
   117         webbrowser.open(url)